Commit aaa86cd5 authored by Sergei Golubchik's avatar Sergei Golubchik

mhnsw: build indexes with the columns of exactly right size

parent daabc81a
...@@ -134,7 +134,7 @@ typedef struct st_maria_create_info ...@@ -134,7 +134,7 @@ typedef struct st_maria_create_info
ulong s3_block_size; ulong s3_block_size;
/* Size of null bitmap at start of row */ /* Size of null bitmap at start of row */
uint null_bytes; uint null_bytes;
uint old_options; uint old_options, rec_reflength;
uint compression_algorithm; uint compression_algorithm;
enum data_file_type org_data_file_type; enum data_file_type org_data_file_type;
uint16 language; uint16 language;
......
...@@ -154,7 +154,7 @@ typedef struct st_mi_create_info ...@@ -154,7 +154,7 @@ typedef struct st_mi_create_info
ulonglong auto_increment; ulonglong auto_increment;
ulonglong data_file_length; ulonglong data_file_length;
ulonglong key_file_length; ulonglong key_file_length;
uint old_options; uint old_options, rec_reflength;
uint16 language; uint16 language;
my_bool with_auto_increment; my_bool with_auto_increment;
} MI_CREATE_INFO; } MI_CREATE_INFO;
......
...@@ -6320,7 +6320,8 @@ int handler::calculate_checksum() ...@@ -6320,7 +6320,8 @@ int handler::calculate_checksum()
****************************************************************************/ ****************************************************************************/
static int ha_create_table_from_share(THD *thd, TABLE_SHARE *share, static int ha_create_table_from_share(THD *thd, TABLE_SHARE *share,
HA_CREATE_INFO *create_info) HA_CREATE_INFO *create_info,
uint *ref_length)
{ {
TABLE table; TABLE table;
bool is_tmp __attribute__((unused)) = bool is_tmp __attribute__((unused)) =
...@@ -6346,6 +6347,7 @@ static int ha_create_table_from_share(THD *thd, TABLE_SHARE *share, ...@@ -6346,6 +6347,7 @@ static int ha_create_table_from_share(THD *thd, TABLE_SHARE *share,
PSI_CALL_drop_table_share(is_tmp, share->db.str, (uint)share->db.length, PSI_CALL_drop_table_share(is_tmp, share->db.str, (uint)share->db.length,
share->table_name.str, (uint)share->table_name.length); share->table_name.str, (uint)share->table_name.length);
} }
*ref_length= table.file->ref_length; // for hlindexes
(void) closefrm(&table); (void) closefrm(&table);
return error; return error;
...@@ -6372,6 +6374,7 @@ int ha_create_table(THD *thd, const char *path, const char *db, ...@@ -6372,6 +6374,7 @@ int ha_create_table(THD *thd, const char *path, const char *db,
LEX_CUSTRING *frm, bool skip_frm_file) LEX_CUSTRING *frm, bool skip_frm_file)
{ {
int error= 1; int error= 1;
uint ref_length;
TABLE_SHARE share; TABLE_SHARE share;
Abort_on_warning_instant_set old_abort_on_warning(thd, 0); Abort_on_warning_instant_set old_abort_on_warning(thd, 0);
DBUG_ENTER("ha_create_table"); DBUG_ENTER("ha_create_table");
...@@ -6399,7 +6402,7 @@ int ha_create_table(THD *thd, const char *path, const char *db, ...@@ -6399,7 +6402,7 @@ int ha_create_table(THD *thd, const char *path, const char *db,
goto err; goto err;
} }
if ((error= ha_create_table_from_share(thd, &share, create_info))) if ((error= ha_create_table_from_share(thd, &share, create_info, &ref_length)))
goto err; goto err;
/* create secondary tables for high level indexes */ /* create secondary tables for high level indexes */
...@@ -6421,11 +6424,14 @@ int ha_create_table(THD *thd, const char *path, const char *db, ...@@ -6421,11 +6424,14 @@ int ha_create_table(THD *thd, const char *path, const char *db,
my_snprintf(path_end, HLINDEX_BUF_LEN, HLINDEX_TEMPLATE, i); my_snprintf(path_end, HLINDEX_BUF_LEN, HLINDEX_TEMPLATE, i);
init_tmp_table_share(thd, &index_share, db, 0, table_name, file_name, 1); init_tmp_table_share(thd, &index_share, db, 0, table_name, file_name, 1);
index_share.db_plugin= share.db_plugin; index_share.db_plugin= share.db_plugin;
LEX_CSTRING sql= mhnsw_hlindex_table_def(thd, ref_length);
if ((error= index_share.init_from_sql_statement_string(thd, false, if ((error= index_share.init_from_sql_statement_string(thd, false,
mhnsw_hlindex_table.str, mhnsw_hlindex_table.length))) sql.str, sql.length)))
break; break;
if ((error= ha_create_table_from_share(thd, &index_share, &index_cinfo))) uint unused;
if ((error= ha_create_table_from_share(thd, &index_share, &index_cinfo,
&unused)))
break; break;
} }
free_table_share(&index_share); free_table_share(&index_share);
......
...@@ -9853,8 +9853,9 @@ int TABLE::hlindex_open(uint nr) ...@@ -9853,8 +9853,9 @@ int TABLE::hlindex_open(uint nr)
path, false); path, false);
share->db_plugin= s->db_plugin; share->db_plugin= s->db_plugin;
LEX_CSTRING sql= mhnsw_hlindex_table_def(in_use, file->ref_length);
if (share->init_from_sql_statement_string(in_use, false, if (share->init_from_sql_statement_string(in_use, false,
mhnsw_hlindex_table.str, mhnsw_hlindex_table.length)) sql.str, sql.length))
{ {
free_table_share(share); free_table_share(share);
return 1; return 1;
......
...@@ -21,15 +21,6 @@ ...@@ -21,15 +21,6 @@
#include "key.h" #include "key.h"
#include <scope.h> #include <scope.h>
const LEX_CSTRING mhnsw_hlindex_table={STRING_WITH_LEN("\
CREATE TABLE i ( \
layer int not null, \
src varbinary(255) not null, \
neighbors blob not null, \
index (layer, src)) \
")};
class MHNSW_Context; class MHNSW_Context;
class FVector: public Sql_alloc class FVector: public Sql_alloc
...@@ -614,3 +605,17 @@ int mhnsw_next(TABLE *table) ...@@ -614,3 +605,17 @@ int mhnsw_next(TABLE *table)
} }
return HA_ERR_END_OF_FILE; return HA_ERR_END_OF_FILE;
} }
const LEX_CSTRING mhnsw_hlindex_table_def(THD *thd, uint ref_length)
{
const char templ[]="CREATE TABLE i ( "
" layer int not null, "
" src varbinary(%u) not null, "
" neighbors varbinary(%u) not null,"
" index (layer, src)) ";
size_t len= sizeof(templ) + 32;
char *s= thd->alloc(len);
len= my_snprintf(s, len, templ, ref_length, 2 * ref_length *
thd->variables.hnsw_max_connection_per_layer);
return {s, len};
}
...@@ -21,8 +21,7 @@ ...@@ -21,8 +21,7 @@
#include "structs.h" #include "structs.h"
#include "table.h" #include "table.h"
extern const LEX_CSTRING mhnsw_hlindex_table; const LEX_CSTRING mhnsw_hlindex_table_def(THD *thd, uint ref_length);
int mhnsw_insert(TABLE *table, KEY *keyinfo); int mhnsw_insert(TABLE *table, KEY *keyinfo);
int mhnsw_first(TABLE *table, KEY *keyinfo, Item *dist, ulonglong limit); int mhnsw_first(TABLE *table, KEY *keyinfo, Item *dist, ulonglong limit);
int mhnsw_next(TABLE *table); int mhnsw_next(TABLE *table);
...@@ -3411,6 +3411,7 @@ int ha_maria::create(const char *name, TABLE *table_arg, ...@@ -3411,6 +3411,7 @@ int ha_maria::create(const char *name, TABLE *table_arg,
&create_info, create_flags); &create_info, create_flags);
my_free(recinfo); my_free(recinfo);
ref_length= create_info.rec_reflength;
DBUG_RETURN(error); DBUG_RETURN(error);
} }
......
...@@ -745,7 +745,7 @@ int maria_create(const char *name, enum data_file_type datafile_type, ...@@ -745,7 +745,7 @@ int maria_create(const char *name, enum data_file_type datafile_type,
share.state.sortkey= (ushort) ~0; share.state.sortkey= (ushort) ~0;
share.state.auto_increment=ci->auto_increment; share.state.auto_increment=ci->auto_increment;
share.options=options; share.options=options;
share.base.rec_reflength=pointer; share.base.rec_reflength= ci->rec_reflength= pointer;
share.base.block_size= maria_block_size; share.base.block_size= maria_block_size;
share.base.language= (ci->language ? ci->language : share.base.language= (ci->language ? ci->language :
default_charset_info->number); default_charset_info->number);
......
...@@ -2304,6 +2304,7 @@ int ha_myisam::create(const char *name, TABLE *table_arg, ...@@ -2304,6 +2304,7 @@ int ha_myisam::create(const char *name, TABLE *table_arg,
record_count, recinfo, record_count, recinfo,
0, (MI_UNIQUEDEF*) 0, 0, (MI_UNIQUEDEF*) 0,
&create_info, create_flags); &create_info, create_flags);
ref_length= create_info.rec_reflength;
my_free(recinfo); my_free(recinfo);
DBUG_RETURN(error); DBUG_RETURN(error);
} }
......
...@@ -528,7 +528,7 @@ int mi_create(const char *name,uint keys,MI_KEYDEF *keydefs, ...@@ -528,7 +528,7 @@ int mi_create(const char *name,uint keys,MI_KEYDEF *keydefs,
share.state.sortkey= (ushort) ~0; share.state.sortkey= (ushort) ~0;
share.state.auto_increment=ci->auto_increment; share.state.auto_increment=ci->auto_increment;
share.options=options; share.options=options;
share.base.rec_reflength=pointer; share.base.rec_reflength= ci->rec_reflength= pointer;
/* Get estimate for index file length (this may be wrong for FT keys) */ /* Get estimate for index file length (this may be wrong for FT keys) */
tmp= (tot_length + max_key_block_length * keys * tmp= (tot_length + max_key_block_length * keys *
MI_INDEX_BLOCK_MARGIN) / MI_MIN_KEY_BLOCK_LENGTH; MI_INDEX_BLOCK_MARGIN) / MI_MIN_KEY_BLOCK_LENGTH;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment