diff --git a/mysql-test/t/count_distinct2.test b/mysql-test/t/count_distinct2.test index 2447a7c36118d8eb27b433b29659bb49bcb64146..33d4cf5427897171a96ae4a82c8652e9cd9305fe 100644 --- a/mysql-test/t/count_distinct2.test +++ b/mysql-test/t/count_distinct2.test @@ -45,7 +45,7 @@ select count(distinct n2), n1 from t1 group by n1; drop table t1; # test the converstion from tree to MyISAM -create table t1 (n int); +create table t1 (n int default NULL); let $1=5000; while ($1) { diff --git a/sql/item_sum.cc b/sql/item_sum.cc index 879c27178e5bc9c4addfd1ea4bf04c4357f2a962..914d9f2791cbda7d07a786c85074f9af92407981 100644 --- a/sql/item_sum.cc +++ b/sql/item_sum.cc @@ -839,7 +839,8 @@ int dump_leaf(byte* key, uint32 count __attribute__((unused)), { char* buf = item->table->record[0]; int error; - memset(buf, 0xff, item->rec_offset); // make up for cheating in the tree + // the first item->rec_offset bytes are taken care of with + // restore_record(table,2) in setup() memcpy(buf + item->rec_offset, key, item->tree.size_of_element); if ((error = item->table->file->write_row(buf))) { @@ -888,12 +889,17 @@ bool Item_sum_count_distinct::setup(THD *thd) table->file->extra(HA_EXTRA_NO_ROWS); // Don't update rows table->no_rows=1; + if(table->db_type == DB_TYPE_HEAP) // no blobs, otherwise it would be // MyISAM { qsort_cmp2 compare_key; void* cmp_arg; int key_len; + + // to make things easier for dump_leaf if we ever have to dump to + // MyISAM + restore_record(table,2); if(table->fields == 1) // if we have only one field, which is // the most common use of count(distinct), it is much faster @@ -941,7 +947,7 @@ bool Item_sum_count_distinct::setup(THD *thd) // but this has to be handled - otherwise someone can crash // the server with a DoS attack max_elements_in_tree = (key_len) ? max_heap_table_size/key_len : - max_heap_table_size; + 1; } return 0; diff --git a/sql/item_sum.h b/sql/item_sum.h index 1aa7f78d7861034a6e48803ab3aee84f3f062b7d..6a62614aff8d87ab022c20fed0cd2bfd162bb1f8 100644 --- a/sql/item_sum.h +++ b/sql/item_sum.h @@ -148,19 +148,20 @@ class Item_sum_count_distinct :public Item_sum_int bool fix_fields(THD *thd,TABLE_LIST *tables); TMP_TABLE_PARAM *tmp_table_param; TREE tree; - uint max_elements_in_tree; + // calculated based on max_heap_table_size. If reached, // walk the tree and dump it into MyISAM table + uint max_elements_in_tree; - bool use_tree; // If there are no blobs, we can use a tree, which // is faster than heap table. In that case, we still use the table // to help get things set up, but we insert nothing in it + bool use_tree; - int rec_offset; // the first few bytes of record ( at least one) // are just markers for deleted and NULLs. We want to skip them since // they will just bloat the tree without providing any valuable info + int rec_offset; int tree_to_myisam();