diff --git a/mysql-test/r/insert_select.result b/mysql-test/r/insert_select.result index 434dddb3049..2cfbb51441d 100644 --- a/mysql-test/r/insert_select.result +++ b/mysql-test/r/insert_select.result @@ -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 42S22: Unknown column 't2.x' 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; diff --git a/mysql-test/t/insert_select.test b/mysql-test/t/insert_select.test index 5dd6f338865..0b9a0e86ba9 100644 --- a/mysql-test/t/insert_select.test +++ b/mysql-test/t/insert_select.test @@ -224,4 +224,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 diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 96b196e34b0..a243f22e7ee 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -1068,8 +1068,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); @@ -8814,6 +8815,11 @@ create_tmp_table(THD *thd,TMP_TABLE_PARAM *param,List &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) { @@ -8828,8 +8834,6 @@ create_tmp_table(THD *thd,TMP_TABLE_PARAM *param,List &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->s->max_rows, rows_limit); - param->end_write_records= rows_limit; table->distinct= 1; table->s->keys= 1; if (blob_count)