mhnsw: build indexes with the columns of exactly right size

This commit is contained in:
Sergei Golubchik 2024-06-07 13:50:13 +02:00
parent 1029d2f245
commit 613542dceb
10 changed files with 33 additions and 20 deletions

View file

@ -134,7 +134,7 @@ typedef struct st_maria_create_info
ulong s3_block_size;
/* Size of null bitmap at start of row */
uint null_bytes;
uint old_options;
uint old_options, rec_reflength;
uint compression_algorithm;
enum data_file_type org_data_file_type;
uint16 language;

View file

@ -154,7 +154,7 @@ typedef struct st_mi_create_info
ulonglong auto_increment;
ulonglong data_file_length;
ulonglong key_file_length;
uint old_options;
uint old_options, rec_reflength;
uint16 language;
my_bool with_auto_increment;
} MI_CREATE_INFO;

View file

@ -6321,7 +6321,8 @@ int handler::calculate_checksum()
****************************************************************************/
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;
@ -6342,6 +6343,7 @@ static int ha_create_table_from_share(THD *thd, TABLE_SHARE *share,
share->table_name.str, error);
table.file->print_error(error, MYF(ME_WARNING));
}
*ref_length= table.file->ref_length; // for hlindexes
(void) closefrm(&table);
return error;
@ -6368,6 +6370,7 @@ int ha_create_table(THD *thd, const char *path, const char *db,
LEX_CUSTRING *frm, bool skip_frm_file)
{
int error= 1;
uint ref_length;
TABLE_SHARE share;
Abort_on_warning_instant_set old_abort_on_warning(thd, 0);
bool is_tmp __attribute__((unused)) =
@ -6398,7 +6401,7 @@ int ha_create_table(THD *thd, const char *path, const char *db,
}
share.m_psi= PSI_CALL_get_table_share(is_tmp, &share);
if ((error= ha_create_table_from_share(thd, &share, create_info)))
if ((error= ha_create_table_from_share(thd, &share, create_info, &ref_length)))
{
PSI_CALL_drop_table_share(is_tmp, share.db.str, (uint)share.db.length,
share.table_name.str, (uint)share.table_name.length);
@ -6424,11 +6427,14 @@ int ha_create_table(THD *thd, const char *path, const char *db,
my_snprintf(path_end, HLINDEX_BUF_LEN, HLINDEX_TEMPLATE, i);
init_tmp_table_share(thd, &index_share, db, 0, table_name, file_name, 1);
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,
mhnsw_hlindex_table.str, mhnsw_hlindex_table.length)))
sql.str, sql.length)))
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;
}
free_table_share(&index_share);

View file

@ -9860,8 +9860,9 @@ int TABLE::hlindex_open(uint nr)
path, false);
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,
mhnsw_hlindex_table.str, mhnsw_hlindex_table.length))
sql.str, sql.length))
{
free_table_share(share);
return 1;

View file

@ -21,15 +21,6 @@
#include "key.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 FVector: public Sql_alloc
@ -614,3 +605,17 @@ int mhnsw_read_next(TABLE *table)
}
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};
}

View file

@ -21,8 +21,7 @@
#include "structs.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_read_first(TABLE *table, KEY *keyinfo, Item *dist, ulonglong limit);
int mhnsw_read_next(TABLE *table);

View file

@ -3411,6 +3411,7 @@ int ha_maria::create(const char *name, TABLE *table_arg,
&create_info, create_flags);
my_free(recinfo);
ref_length= create_info.rec_reflength;
DBUG_RETURN(error);
}

View file

@ -745,7 +745,7 @@ int maria_create(const char *name, enum data_file_type datafile_type,
share.state.sortkey= (ushort) ~0;
share.state.auto_increment=ci->auto_increment;
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.language= (ci->language ? ci->language :
default_charset_info->number);

View file

@ -2304,6 +2304,7 @@ int ha_myisam::create(const char *name, TABLE *table_arg,
record_count, recinfo,
0, (MI_UNIQUEDEF*) 0,
&create_info, create_flags);
ref_length= create_info.rec_reflength;
my_free(recinfo);
DBUG_RETURN(error);
}

View file

@ -528,7 +528,7 @@ int mi_create(const char *name,uint keys,MI_KEYDEF *keydefs,
share.state.sortkey= (ushort) ~0;
share.state.auto_increment=ci->auto_increment;
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) */
tmp= (tot_length + max_key_block_length * keys *
MI_INDEX_BLOCK_MARGIN) / MI_MIN_KEY_BLOCK_LENGTH;