diff options
author | unknown <serg@serg.mysql.com> | 2002-12-07 22:40:20 +0100 |
---|---|---|
committer | unknown <serg@serg.mysql.com> | 2002-12-07 22:40:20 +0100 |
commit | 6271ea35b83501781cf3ee51a62fd95870b3fb96 (patch) | |
tree | cac4edcace5032716391947982c07af2dba77b24 | |
parent | 23865c0eea7d3d670f4b84a76cbe3511fe241b7f (diff) | |
download | mariadb-git-6271ea35b83501781cf3ee51a62fd95870b3fb96.tar.gz |
bulk insert code optimized
mysql-test/r/distinct.result:
updated
mysql-test/r/fulltext.result:
updated
mysql-test/r/select.result:
updated
mysql-test/r/show_check.result:
updated
mysql-test/t/insert.test:
updated
-rw-r--r-- | include/my_base.h | 3 | ||||
-rw-r--r-- | include/myisam.h | 4 | ||||
-rw-r--r-- | myisam/mi_extra.c | 27 | ||||
-rw-r--r-- | myisam/mi_write.c | 47 | ||||
-rw-r--r-- | myisam/myisamdef.h | 3 | ||||
-rw-r--r-- | mysql-test/r/distinct.result | 8 | ||||
-rw-r--r-- | mysql-test/r/fulltext.result | 4 | ||||
-rw-r--r-- | mysql-test/r/select.result | 4 | ||||
-rw-r--r-- | mysql-test/r/show_check.result | 2 | ||||
-rw-r--r-- | mysql-test/t/insert.test | 2 | ||||
-rw-r--r-- | sql/ha_myisam.cc | 18 | ||||
-rw-r--r-- | sql/ha_myisammrg.cc | 4 | ||||
-rw-r--r-- | sql/sql_insert.cc | 20 |
13 files changed, 73 insertions, 73 deletions
diff --git a/include/my_base.h b/include/my_base.h index 8ef8301b84d..bcb8c8d6a2f 100644 --- a/include/my_base.h +++ b/include/my_base.h @@ -106,9 +106,6 @@ enum ha_extra_function { HA_EXTRA_IGNORE_DUP_KEY, /* Dup keys don't rollback everything*/ HA_EXTRA_NO_IGNORE_DUP_KEY, HA_EXTRA_DONT_USE_CURSOR_TO_UPDATE, /* Cursor will not be used for update */ - HA_EXTRA_BULK_INSERT_BEGIN, - HA_EXTRA_BULK_INSERT_FLUSH, /* Flush one index */ - HA_EXTRA_BULK_INSERT_END, HA_EXTRA_PREPARE_FOR_DELETE, HA_EXTRA_PREPARE_FOR_UPDATE /* Remove read cache if problems */ }; diff --git a/include/myisam.h b/include/myisam.h index 79d18bf8736..94b5d23bba6 100644 --- a/include/myisam.h +++ b/include/myisam.h @@ -433,6 +433,10 @@ void mi_disable_non_unique_index(MI_INFO *info, ha_rows rows); my_bool mi_test_if_sort_rep(MI_INFO *info, ha_rows rows, ulonglong key_map, my_bool force); +int mi_init_bulk_insert(MI_INFO *info, ulong cache_size, ha_rows rows); +void mi_flush_bulk_insert(MI_INFO *info, uint inx); +void mi_end_bulk_insert(MI_INFO *info); + #ifdef __cplusplus } #endif diff --git a/myisam/mi_extra.c b/myisam/mi_extra.c index d7a3aea516d..8429b22dad4 100644 --- a/myisam/mi_extra.c +++ b/myisam/mi_extra.c @@ -358,33 +358,6 @@ int mi_extra(MI_INFO *info, enum ha_extra_function function, void *extra_arg) case HA_EXTRA_QUICK: info->quick_mode=1; break; - case HA_EXTRA_BULK_INSERT_BEGIN: - error=_mi_init_bulk_insert(info, (extra_arg ? *(ulong*) extra_arg : - myisam_bulk_insert_tree_size)); - break; - case HA_EXTRA_BULK_INSERT_FLUSH: - if (info->bulk_insert) - { - uint index_to_flush= *(uint*) extra_arg; - if (is_tree_inited(&info->bulk_insert[index_to_flush])) - reset_tree(&info->bulk_insert[index_to_flush]); - } - break; - case HA_EXTRA_BULK_INSERT_END: - if (info->bulk_insert) - { - uint i; - for (i=0 ; i < share->base.keys ; i++) - { - if (is_tree_inited(& info->bulk_insert[i])) - { - delete_tree(& info->bulk_insert[i]); - } - } - my_free((void *)info->bulk_insert, MYF(0)); - info->bulk_insert=0; - } - break; case HA_EXTRA_NO_ROWS: if (!share->state.header.uniques) info->opt_flag|= OPT_NO_ROWS; diff --git a/myisam/mi_write.c b/myisam/mi_write.c index 70a1bea26bb..d39bbbf5fc7 100644 --- a/myisam/mi_write.c +++ b/myisam/mi_write.c @@ -801,26 +801,27 @@ static int keys_free(uchar *key, TREE_FREE mode, bulk_insert_param *param) } -int _mi_init_bulk_insert(MI_INFO *info, ulong cache_size) +int mi_init_bulk_insert(MI_INFO *info, ulong cache_size, ha_rows rows) { MYISAM_SHARE *share=info->s; MI_KEYDEF *key=share->keyinfo; bulk_insert_param *params; - uint i, num_keys; + uint i, num_keys, total_keylength; ulonglong key_map=0; DBUG_ENTER("_mi_init_bulk_insert"); DBUG_PRINT("enter",("cache_size: %lu", cache_size)); - if (info->bulk_insert) + if (info->bulk_insert || (rows && rows < MI_MIN_ROWS_TO_USE_BULK_INSERT)) DBUG_RETURN(0); - for (i=num_keys=0 ; i < share->base.keys ; i++) + for (i=total_keylength=num_keys=0 ; i < share->base.keys ; i++) { if (!(key[i].flag & HA_NOSAME) && share->base.auto_key != i+1 && test(share->state.key_map & ((ulonglong) 1 << i))) { num_keys++; key_map |=((ulonglong) 1 << i); + total_keylength+=key[i].maxlength+TREE_ELEMENT_EXTRA_SIZE; } } @@ -828,6 +829,11 @@ int _mi_init_bulk_insert(MI_INFO *info, ulong cache_size) num_keys * MI_MIN_SIZE_BULK_INSERT_TREE > cache_size) DBUG_RETURN(0); + if (rows && rows*total_keylength < cache_size) + cache_size=rows; + else + cache_size/=total_keylength*16; + info->bulk_insert=(TREE *) my_malloc((sizeof(TREE)*share->base.keys+ sizeof(bulk_insert_param)*num_keys),MYF(0)); @@ -836,7 +842,7 @@ int _mi_init_bulk_insert(MI_INFO *info, ulong cache_size) DBUG_RETURN(HA_ERR_OUT_OF_MEM); params=(bulk_insert_param *)(info->bulk_insert+share->base.keys); - for (i=0 ; i < share->base.keys ; i++,key++) + for (i=0 ; i < share->base.keys ; i++) { if (test(key_map & ((ulonglong) 1 << i))) { @@ -844,8 +850,8 @@ int _mi_init_bulk_insert(MI_INFO *info, ulong cache_size) params->keynr=i; /* Only allocate a 16'th of the buffer at a time */ init_tree(&info->bulk_insert[i], - cache_size / num_keys / 16 + 10, - cache_size / num_keys, 0, + cache_size * key[i].maxlength, + cache_size * key[i].maxlength, 0, (qsort_cmp2)keys_compare, 0, (tree_element_free) keys_free, (void *)params++); } @@ -855,3 +861,30 @@ int _mi_init_bulk_insert(MI_INFO *info, ulong cache_size) DBUG_RETURN(0); } + +void mi_flush_bulk_insert(MI_INFO *info, uint inx) +{ + if (info->bulk_insert) + { + if (is_tree_inited(&info->bulk_insert[inx])) + reset_tree(&info->bulk_insert[inx]); + } +} + +void mi_end_bulk_insert(MI_INFO *info) +{ + if (info->bulk_insert) + { + uint i; + for (i=0 ; i < info->s->base.keys ; i++) + { + if (is_tree_inited(& info->bulk_insert[i])) + { + delete_tree(& info->bulk_insert[i]); + } + } + my_free((void *)info->bulk_insert, MYF(0)); + info->bulk_insert=0; + } +} + diff --git a/myisam/myisamdef.h b/myisam/myisamdef.h index f0d6f740661..07744e11e5f 100644 --- a/myisam/myisamdef.h +++ b/myisam/myisamdef.h @@ -373,6 +373,7 @@ struct st_myisam_info { #define MI_MIN_KEYBLOCK_LENGTH 50 /* When to split delete blocks */ #define MI_MIN_SIZE_BULK_INSERT_TREE 16384 /* this is per key */ +#define MI_MIN_ROWS_TO_USE_BULK_INSERT 100 /* The UNIQUE check is done with a hashed long key */ @@ -658,8 +659,6 @@ int mi_open_datafile(MI_INFO *info, MYISAM_SHARE *share, File file_to_dup); int mi_open_keyfile(MYISAM_SHARE *share); void mi_setup_functions(register MYISAM_SHARE *share); -int _mi_init_bulk_insert(MI_INFO *info, ulong cache_size); - /* Functions needed by mi_check */ void mi_check_print_error _VARARGS((MI_CHECK *param, const char *fmt,...)); void mi_check_print_warning _VARARGS((MI_CHECK *param, const char *fmt,...)); diff --git a/mysql-test/r/distinct.result b/mysql-test/r/distinct.result index 020d6c6534f..919478c7265 100644 --- a/mysql-test/r/distinct.result +++ b/mysql-test/r/distinct.result @@ -173,9 +173,9 @@ INSERT INTO t2 values (1),(2),(3); INSERT INTO t3 VALUES (1,'1'),(2,'2'),(1,'1'),(2,'2'); explain SELECT distinct t3.a FROM t3,t2,t1 WHERE t3.a=t1.b AND t1.a=t2.a; table type possible_keys key key_len ref rows Extra -t3 index a a 5 NULL 6 Using index; Using temporary -t2 index a a 4 NULL 5 Using index; Distinct -t1 eq_ref PRIMARY PRIMARY 4 t2.a 1 Using where; Distinct +t1 ALL PRIMARY NULL NULL NULL 2 Using temporary +t2 ref a a 4 t1.a 1 Using index +t3 ref a a 5 t1.b 1 Using where; Using index SELECT distinct t3.a FROM t3,t2,t1 WHERE t3.a=t1.b AND t1.a=t2.a; a 1 @@ -190,7 +190,7 @@ insert into t3 select * from t4; explain select distinct t1.a from t1,t3 where t1.a=t3.a; table type possible_keys key key_len ref rows Extra t1 index PRIMARY PRIMARY 4 NULL 2 Using index; Using temporary -t3 ref a a 5 t1.a 10 Using where; Using index; Distinct +t3 ref a a 5 t1.a 1 Using where; Using index; Distinct select distinct t1.a from t1,t3 where t1.a=t3.a; a 1 diff --git a/mysql-test/r/fulltext.result b/mysql-test/r/fulltext.result index edf109fcc93..fc1e0788087 100644 --- a/mysql-test/r/fulltext.result +++ b/mysql-test/r/fulltext.result @@ -135,8 +135,8 @@ id 3 show keys from t2; Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment -t2 1 tig 1 ticket A NULL NULL NULL YES BTREE -t2 1 tix 1 inhalt A NULL 1 NULL YES FULLTEXT +t2 1 tig 1 ticket A 3 NULL NULL YES BTREE +t2 1 tix 1 inhalt A 1 1 NULL YES FULLTEXT show create table t2; Table Create Table t2 CREATE TABLE `t2` ( diff --git a/mysql-test/r/select.result b/mysql-test/r/select.result index a921d75f20a..51cd67ac832 100644 --- a/mysql-test/r/select.result +++ b/mysql-test/r/select.result @@ -3239,8 +3239,8 @@ Field Type Null Key Default Extra Privileges show keys from t2; Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment t2 0 PRIMARY 1 auto A 1199 NULL NULL BTREE -t2 0 fld1 1 fld1 A 1199 NULL NULL BTREE -t2 1 fld3 1 fld3 A NULL NULL NULL BTREE +t2 0 fld1 1 fld1 A 0 NULL NULL BTREE +t2 1 fld3 1 fld3 A 1199 NULL NULL BTREE drop table t4, t3, t2, t1; DO 1; DO benchmark(100,1+1),1,1; diff --git a/mysql-test/r/show_check.result b/mysql-test/r/show_check.result index 2c32d766a38..70548311e26 100644 --- a/mysql-test/r/show_check.result +++ b/mysql-test/r/show_check.result @@ -9,7 +9,7 @@ Table Op Msg_type Msg_text test.t1 check status Table is already up to date check table t1 changed; Table Op Msg_type Msg_text -test.t1 check status OK +test.t1 check status Table is already up to date insert into t1 values (5,5,5); check table t1 changed; Table Op Msg_type Msg_text diff --git a/mysql-test/t/insert.test b/mysql-test/t/insert.test index 0bca8dc5890..9b06b522028 100644 --- a/mysql-test/t/insert.test +++ b/mysql-test/t/insert.test @@ -46,6 +46,8 @@ drop table t1; create table t1 (sid char(20), id int(2) NOT NULL auto_increment, key(sid, id)); insert into t1 values ('skr',NULL),('skr',NULL),('test',NULL); select * from t1; +insert into t1 values ('rts',NULL),('rts',NULL),('test',NULL); +select * from t1; drop table t1; # diff --git a/sql/ha_myisam.cc b/sql/ha_myisam.cc index 0c472ea6a45..0560afdf068 100644 --- a/sql/ha_myisam.cc +++ b/sql/ha_myisam.cc @@ -685,11 +685,14 @@ void ha_myisam::deactivate_non_unique_index(ha_rows rows) /* Only disable old index if the table was empty */ if (file->state->records == 0) mi_disable_non_unique_index(file,rows); - ha_myisam::extra_opt(HA_EXTRA_BULK_INSERT_BEGIN, - current_thd->variables.bulk_insert_buff_size); + else + { + mi_init_bulk_insert(file, + current_thd->variables.bulk_insert_buff_size, rows); table->bulk_insert= 1; } } + } enable_activate_all_index=1; } else @@ -704,7 +707,7 @@ bool ha_myisam::activate_all_index(THD *thd) MYISAM_SHARE* share = file->s; DBUG_ENTER("activate_all_index"); - mi_extra(file, HA_EXTRA_BULK_INSERT_END, 0); + mi_end_bulk_insert(file); table->bulk_insert= 0; if (enable_activate_all_index && share->state.key_map != set_bits(ulonglong, share->base.keys)) @@ -945,13 +948,11 @@ int ha_myisam::extra(enum ha_extra_function operation) } -/* To be used with WRITE_CACHE, EXTRA_CACHE and BULK_INSERT_BEGIN */ +/* To be used with WRITE_CACHE and EXTRA_CACHE */ int ha_myisam::extra_opt(enum ha_extra_function operation, ulong cache_size) { - if ((specialflag & SPECIAL_SAFE_MODE) & - (operation == HA_EXTRA_WRITE_CACHE || - operation == HA_EXTRA_BULK_INSERT_BEGIN)) + if ((specialflag & SPECIAL_SAFE_MODE) & operation == HA_EXTRA_WRITE_CACHE) return 0; return mi_extra(file, operation, (void*) &cache_size); } @@ -1213,8 +1214,7 @@ longlong ha_myisam::get_auto_increment() } if (table->bulk_insert) - mi_extra(file, HA_EXTRA_BULK_INSERT_FLUSH, - (void*) &table->next_number_index); + mi_flush_bulk_insert(file, table->next_number_index); longlong nr; int error; diff --git a/sql/ha_myisammrg.cc b/sql/ha_myisammrg.cc index 07683dca73e..f2c3d1a7747 100644 --- a/sql/ha_myisammrg.cc +++ b/sql/ha_myisammrg.cc @@ -247,9 +247,7 @@ int ha_myisammrg::extra(enum ha_extra_function operation) int ha_myisammrg::extra_opt(enum ha_extra_function operation, ulong cache_size) { - if ((specialflag & SPECIAL_SAFE_MODE) & - (operation == HA_EXTRA_WRITE_CACHE || - operation == HA_EXTRA_BULK_INSERT_BEGIN)) + if ((specialflag & SPECIAL_SAFE_MODE) & operation == HA_EXTRA_WRITE_CACHE) return 0; return myrg_extra(file, operation, (void*) &cache_size); } diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc index 5ca08aa31b0..4d1b2e59f60 100644 --- a/sql/sql_insert.cc +++ b/sql/sql_insert.cc @@ -105,7 +105,7 @@ int mysql_insert(THD *thd,TABLE_LIST *table_list, List<Item> &fields, int error; bool log_on= ((thd->options & OPTION_UPDATE_LOG) || !(thd->master_access & SUPER_ACL)); - bool transactional_table, log_delayed, bulk_insert=0; + bool transactional_table, log_delayed, bulk_insert; uint value_count; ulong counter = 1; ulonglong id; @@ -187,21 +187,16 @@ int mysql_insert(THD *thd,TABLE_LIST *table_list, List<Item> &fields, thd->proc_info="update"; if (duplic == DUP_IGNORE || duplic == DUP_REPLACE) table->file->extra(HA_EXTRA_IGNORE_DUP_KEY); - if ((bulk_insert= (values_list.elements >= MIN_ROWS_TO_USE_BULK_INSERT && - lock_type != TL_WRITE_DELAYED && - !(specialflag & SPECIAL_SAFE_MODE)))) + if ((lock_type != TL_WRITE_DELAYED && !(specialflag & SPECIAL_SAFE_MODE))) { table->file->extra_opt(HA_EXTRA_WRITE_CACHE, min(thd->variables.read_buff_size, table->avg_row_length*values_list.elements)); - if (thd->variables.bulk_insert_buff_size) - table->file->extra_opt(HA_EXTRA_BULK_INSERT_BEGIN, - min(thd->variables.bulk_insert_buff_size, - (table->total_key_length + - table->keys * TREE_ELEMENT_EXTRA_SIZE)* - values_list.elements)); - table->bulk_insert= 1; + table->file->deactivate_non_unique_index(values_list.elements); + bulk_insert=1; } + else + bulk_insert=0; while ((values= its++)) { @@ -278,7 +273,7 @@ int mysql_insert(THD *thd,TABLE_LIST *table_list, List<Item> &fields, error=1; } } - if (table->file->extra(HA_EXTRA_BULK_INSERT_END)) + if (table->file->activate_all_index(thd)) { if (!error) { @@ -286,7 +281,6 @@ int mysql_insert(THD *thd,TABLE_LIST *table_list, List<Item> &fields, error=1; } } - table->bulk_insert= 0; } if (id && values_list.elements != 1) thd->insert_id(id); // For update log |