Commit c5ed7a87 authored by gkodinov@mysql.com's avatar gkodinov@mysql.com

* Bug #9676: INSERT INTO x SELECT .. FROM x LIMIT 1; slows down with big

              tables
Currently in INSERT ... SELECT ... LIMIT ... the compiler uses a 
temporary table to store the results of SELECT ... LIMIT .. and then
uses that table as a source for INSERT. The problem is that in some cases
it actually skips the LIMIT clause in doing that and materializes the 
whole SELECT result set regardless of the LIMIT.
This fix is limiting the process of filling up the temp table with only 
that much rows that will be actually used by propagating the LIMIT value.
parent d4942a26
......@@ -686,3 +686,7 @@ ERROR 42S22: Unknown column 'z' in 'field list'
insert into t1(x,y) select x,z from t2 on duplicate key update x=values(t2.x);
ERROR 42S02: Unknown table 't2' in field list
drop table t1,t2;
CREATE TABLE t1 (a int PRIMARY KEY);
INSERT INTO t1 values (1), (2);
INSERT INTO t1 SELECT a + 2 FROM t1 LIMIT 1;
DROP TABLE t1;
......@@ -226,4 +226,17 @@ insert into t1(x,y) select x,z from t2 on duplicate key update x=values(z);
insert into t1(x,y) select x,z from t2 on duplicate key update x=values(t2.x);
drop table t1,t2;
#
# Bug #9676: INSERT INTO x SELECT .. FROM x LIMIT 1; slows down with big
# tables
#
#Note: not an exsaustive test : just a check of the code path.
CREATE TABLE t1 (a int PRIMARY KEY);
INSERT INTO t1 values (1), (2);
INSERT INTO t1 SELECT a + 2 FROM t1 LIMIT 1;
DROP TABLE t1;
# End of 4.1 tests
......@@ -888,8 +888,9 @@ JOIN::optimize()
group_list ? 0 : select_distinct,
group_list && simple_group,
select_options,
(order == 0 || skip_sort_order) ? select_limit :
HA_POS_ERROR,
(order == 0 || skip_sort_order ||
test(select_options & OPTION_BUFFER_RESULT)) ?
select_limit : HA_POS_ERROR,
(char *) "")))
DBUG_RETURN(1);
......@@ -5530,6 +5531,11 @@ create_tmp_table(THD *thd,TMP_TABLE_PARAM *param,List<Item> &fields,
keyinfo->key_length+= key_part_info->length;
}
}
else
{
set_if_smaller(table->max_rows, rows_limit);
param->end_write_records= rows_limit;
}
if (distinct && field_count != param->hidden_field_count)
{
......@@ -5544,8 +5550,6 @@ create_tmp_table(THD *thd,TMP_TABLE_PARAM *param,List<Item> &fields,
null_pack_length-=hidden_null_pack_length;
keyinfo->key_parts= ((field_count-param->hidden_field_count)+
test(null_pack_length));
set_if_smaller(table->max_rows, rows_limit);
param->end_write_records= rows_limit;
table->distinct=1;
table->keys=1;
if (blob_count)
......
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