diff --git a/mysql-test/r/subselect.result b/mysql-test/r/subselect.result index a6ff608ab66..b8b899f4850 100644 --- a/mysql-test/r/subselect.result +++ b/mysql-test/r/subselect.result @@ -328,10 +328,10 @@ patient_uq clinic_uq explain extended select * from t6 where exists (select * from t7 where uq = clinic_uq); id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY t6 ALL NULL NULL NULL NULL 4 Using where -2 DEPENDENT SUBQUERY t7 eq_ref PRIMARY PRIMARY 4 test.t6.clinic_uq 1 +2 DEPENDENT SUBQUERY t7 eq_ref PRIMARY PRIMARY 4 test.t6.clinic_uq 1 Using index Warnings: Note 1276 Field or reference 'clinic_uq' of SELECT #2 was resolved in SELECT #1 -Note 1003 select test.t6.patient_uq AS `patient_uq`,test.t6.clinic_uq AS `clinic_uq` from test.t6 where exists(select test.t7.uq AS `uq`,test.t7.name AS `name` from test.t7 where (test.t7.uq = test.t6.clinic_uq)) +Note 1003 select test.t6.patient_uq AS `patient_uq`,test.t6.clinic_uq AS `clinic_uq` from test.t6 where exists(select 1 AS `Not_used` from test.t7 where (test.t7.uq = test.t6.clinic_uq)) select * from t1 where a= (select a from t2,t4 where t2.b=t4.b); ERROR 23000: Column: 'a' in field list is ambiguous drop table t1,t2,t3; @@ -1817,3 +1817,17 @@ a 1 3 DROP TABLE t1; +create table t1 (a int, b int); +insert into t1 values (1,2),(3,4); +select * from t1 up where exists (select * from t1 where t1.a=up.a); +a b +1 2 +3 4 +explain extended select * from t1 up where exists (select * from t1 where t1.a=up.a); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY up ALL NULL NULL NULL NULL 2 Using where +2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using where +Warnings: +Note 1276 Field or reference 'up.a' of SELECT #2 was resolved in SELECT #1 +Note 1003 select test.up.a AS `a`,test.up.b AS `b` from test.t1 up where exists(select 1 AS `Not_used` from test.t1 where (test.t1.a = test.up.a)) +drop table t1; diff --git a/mysql-test/t/subselect.test b/mysql-test/t/subselect.test index bdefc87b3fd..4b2fd33abfd 100644 --- a/mysql-test/t/subselect.test +++ b/mysql-test/t/subselect.test @@ -1158,3 +1158,12 @@ SELECT a FROM t1 WHERE a >= ALL ( SELECT a FROM t1 WHERE b = 2 ); SELECT a FROM t1 WHERE a <= ALL ( SELECT a FROM t1 WHERE b = 2 ); SELECT a FROM t1 WHERE a <> ALL ( SELECT a FROM t1 WHERE b = 2 ); DROP TABLE t1; + +# +# SELECT(EXISTS * ...)optimisation +# +create table t1 (a int, b int); +insert into t1 values (1,2),(3,4); +select * from t1 up where exists (select * from t1 where t1.a=up.a); +explain extended select * from t1 up where exists (select * from t1 where t1.a=up.a); +drop table t1; diff --git a/sql/sql_base.cc b/sql/sql_base.cc index e3fbfb2d0e3..889c95125c5 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -2184,8 +2184,19 @@ int setup_wild(THD *thd, TABLE_LIST *tables, List &fields, !((Item_field*) item)->field) { uint elem= fields.elements; - if (insert_fields(thd,tables,((Item_field*) item)->db_name, - ((Item_field*) item)->table_name, &it)) + Item_subselect *subsel= thd->lex->current_select->master_unit()->item; + if (subsel && + subsel->substype() == Item_subselect::EXISTS_SUBS) + { + /* + It is EXISTS(SELECT * ...) and we can replace * by any constant. + + Item_int do not need fix_fields() because it is basic constant. + */ + it.replace(new Item_int("Not_used", (longlong) 1, 21)); + } + else if (insert_fields(thd,tables,((Item_field*) item)->db_name, + ((Item_field*) item)->table_name, &it)) { if (stmt) thd->restore_backup_item_arena(stmt, &backup); diff --git a/sql/sql_table.cc b/sql/sql_table.cc index 4dd4b85c9fc..aea6140226c 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -1861,6 +1861,7 @@ send_result_message: TABLE_LIST *save_next= table->next; table->next= 0; result_code= mysql_recreate_table(thd, table, 0); + close_thread_tables(thd); if (!result_code) // recreation went ok { if ((table->table= open_ltable(thd, table, lock_type)) &&