MDEV-7487 Semi-join optimization for single-table update/delete statements

This patch allows to use semi-join optimization at the top level of
single-table update and delete statements.
The problem of supporting such optimization became easy to resolve after
processing a single-table update/delete statement started using JOIN
structure. This allowed to use JOIN::prepare() not only for multi-table
updates/deletes but for single-table ones as well. This was done in the
patch for mdev-28883:
Re-design the upper level of handling UPDATE and DELETE statements.

Note that JOIN::prepare() detects all subqueries that can be considered
as candidates for semi-join optimization. The code added by this patch
looks for such candidates at the top level and if such candidates are found
in the processed single-table update/delete the statement is handled in
the same way as a multi-table update/delete.

    Approved by Oleksandr Byelkin <sanja@mariadb.com>
This commit is contained in:
Igor Babaev 2023-01-09 22:39:39 -08:00
parent e2e3524d72
commit 554278e24d
14 changed files with 6198 additions and 35 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,801 @@
--disable_warnings
DROP DATABASE IF EXISTS dbt3_s001;
--enable_warnings
CREATE DATABASE dbt3_s001;
use dbt3_s001;
--disable_query_log
--disable_result_log
--disable_warnings
--source include/dbt3_s001.inc
--enable_warnings
--enable_result_log
--enable_query_log
create index i_n_name on nation(n_name);
analyze table nation;
--echo # Pullout
--echo # =======
let $c1=
o_orderDATE between '1992-01-01' and '1992-06-30' and
o_custkey in (select c_custkey from customer
where c_nationkey in (select n_nationkey from nation
where n_name='PERU'));
eval
explain
select o_orderkey, o_totalprice from orders where $c1;
eval
explain format=json
select o_orderkey, o_totalprice from orders where $c1;
--sorted_result
eval
select o_orderkey, o_totalprice from orders where $c1;
eval
create table t as
select * from orders where $c1;
eval
explain
delete from orders where $c1;
eval
explain format=json
delete from orders where $c1;
eval
delete from orders where $c1;
eval
select o_orderkey, o_totalprice from orders where $c1;
insert into orders select * from t;
--sorted_result
eval
select o_orderkey, o_totalprice from orders where $c1;
drop table t;
let $c2=
(ps_partkey, ps_suppkey) in
(select p_partkey, s_suppkey from part, supplier
where p_retailprice between 901 and 910 and
s_nationkey in (select n_nationkey from nation
where n_name='PERU'));
eval
explain
select ps_partkey, ps_suppkey, ps_supplycost from partsupp where $c2;
--sorted_result
eval
select ps_partkey, ps_suppkey, ps_supplycost from partsupp where $c2;
eval
create table t as
select * from partsupp where $c2;
eval
explain
delete from partsupp where $c2;
eval
delete from partsupp where $c2;
eval
select ps_partkey, ps_suppkey, ps_supplycost from partsupp where $c2;
insert into partsupp select * from t;
--sorted_result
eval
select ps_partkey, ps_suppkey, ps_supplycost from partsupp where $c2;
drop table t;
let $c3=
ps_partkey in (select p_partkey from part
where p_retailprice between 901 and 910) and
ps_suppkey in (select s_suppkey from supplier
where s_nationkey in (select n_nationkey from nation
where n_name='PERU'));
eval
explain
select ps_partkey, ps_suppkey, ps_supplycost from partsupp where $c3;
--sorted_result
eval
select ps_partkey, ps_suppkey, ps_supplycost from partsupp where $c3;
eval
create table t as
select * from partsupp where $c3;
eval
explain
delete from partsupp where $c3;
eval
delete from partsupp where $c3;
eval
select ps_partkey, ps_suppkey, ps_supplycost from partsupp where $c3;
insert into partsupp select * from t;
--sorted_result
eval
select ps_partkey, ps_suppkey, ps_supplycost from partsupp where $c3;
drop table t;
let $c4=
l_orderkey in (select o_orderkey from orders
where o_custkey in
(select c_custkey from customer
where c_nationkey in
(select n_nationkey from nation
where n_name='PERU'))
and
o_orderDATE between '1992-06-30' and '1992-12-31')
and
(l_partkey, l_suppkey) in
(select p_partkey, s_suppkey from part, supplier
where p_retailprice between 901 and 1000 and
s_nationkey in (select n_nationkey from nation
where n_name='PERU'));
eval
explain
select l_orderkey, l_linenumber, l_tax from lineitem where $c4;
--sorted_result
eval
select l_orderkey, l_linenumber, l_tax from lineitem where $c4;
eval
create table t as
select * from lineitem where $c4;
eval
explain
delete from lineitem where $c4;
eval
delete from lineitem where $c4;
eval
select l_orderkey, l_linenumber, l_tax from lineitem where $c4;
insert into lineitem select * from t;
--sorted_result
eval
select l_orderkey, l_linenumber, l_tax from lineitem where $c4;
drop table t;
--echo # FirstMatch
--echo # ==========
set optimizer_switch='materialization=off';
let $c5=
c_nationkey in (select n_nationkey from nation
where n_regionkey in (1,2))
and
c_custkey in (select o_custkey from orders
where o_orderDATE between '1992-10-09' and '1993-06-08');
eval
explain
select c_name, c_acctbal from customer where $c5;
eval
explain format=json
select c_name, c_acctbal from customer where $c5;
--sorted_result
eval
select c_name, c_acctbal from customer where $c5;
eval
create table t as
select * from customer where $c5;
eval
explain
delete from customer where $c5;
eval
explain format=json
delete from customer where $c5;
eval
delete from customer where $c5;
eval
select c_name, c_acctbal from customer where $c5;
insert into customer select * from t;
--sorted_result
eval
select c_name, c_acctbal from customer where $c5;
drop table t;
set optimizer_switch='materialization=default';
let $c6=
c_nationkey in (select n_nationkey from nation where n_name='PERU')
and
c_custkey in (select o_custkey from orders
where o_orderDATE between "1992-01-09" and "1993-01-08");
eval
explain
select c_name, c_acctbal from customer where $c6;
--sorted_result
eval
select c_name, c_acctbal from customer where $c6;
eval
create table t as
select * from customer where $c6;
eval
explain
delete from customer where $c6;
eval
delete from customer where $c6;
eval
select c_name, c_acctbal from customer where $c6;
insert into customer select * from t;
--sorted_result
eval
select c_name, c_acctbal from customer where $c6;
drop table t;
--echo # Materialization
--echo # ===============
let $c7=
c_custkey in (select o_custkey from orders
where o_orderDATE between '1992-01-09' and '1992-03-08');
eval
explain
select c_name, c_acctbal from customer where $c7;
eval
explain format=json
select c_name, c_acctbal from customer where $c7;
--sorted_result
eval
select c_name, c_acctbal from customer where $c7;
eval
create table t as
select * from customer where $c7;
eval
explain
delete from customer where $c7;
eval
explain format=json
delete from customer where $c7;
eval
delete from customer where $c7;
eval
select c_name, c_acctbal from customer where $c7;
insert into customer select * from t;
--sorted_result
eval
select c_name, c_acctbal from customer where $c7;
drop table t;
let $c8=
c_custkey in (select o_custkey from orders
where o_orderDATE between '1992-06-09' and '1993-01-08');
eval
explain
select c_name, c_acctbal from customer where $c8;
--sorted_result
eval
select c_name, c_acctbal from customer where $c8;
eval
create table t as
select * from customer where $c8;
eval
explain
delete from customer where $c8;
eval
delete from customer where $c8;
eval
select c_name, c_acctbal from customer where $c8;
insert into customer select * from t;
--sorted_result
eval
select c_name, c_acctbal from customer where $c8;
drop table t;
--echo # Materialization SJM
--echo # ===================
let $c9=
c_custkey in (select o_custkey from orders
where o_orderDATE between '1992-01-09' and '1992-03-08'
group by o_custkey having count(o_custkey) > 1);
eval
explain
select c_name, c_acctbal from customer where $c9;
eval
explain format=json
select c_name, c_acctbal from customer where $c9;
--sorted_result
eval
select c_name, c_acctbal from customer where $c9;
eval
create table t as
select * from customer where $c9;
eval
explain
delete from customer where $c9;
eval
explain format=json
delete from customer where $c9;
eval
delete from customer where $c9;
eval
select c_name, c_acctbal from customer where $c9;
insert into customer select * from t;
--sorted_result
eval
select c_name, c_acctbal from customer where $c9;
drop table t;
let $c10=
c_custkey in (select o_custkey from orders
where o_orderDATE between '1992-01-09' and '1993-03-08'
group by o_custkey having count(o_custkey) > 5);
eval
explain
select c_name, c_acctbal from customer where $c10;
--sorted_result
eval
select c_name, c_acctbal from customer where $c10;
eval
create table t as
select * from customer where $c10;
eval
explain
delete from customer where $c10;
eval
delete from customer where $c10;
eval
select c_name, c_acctbal from customer where $c10;
insert into customer select * from t;
--sorted_result
eval
select c_name, c_acctbal from customer where $c10;
drop table t;
--echo # Pullout PS
--echo # ==========
eval
prepare stmt from "
delete from orders where $c1;
";
--sorted_result
eval
select o_orderkey, o_totalprice from orders where $c1;
eval
create table t as
select * from orders where $c1;
execute stmt;
--sorted_result
eval
select o_orderkey, o_totalprice from orders where $c1;
insert into orders select * from t;
--sorted_result
eval
select o_orderkey, o_totalprice from orders where $c1;
eval
create table r as
select * from orders where $c1;
execute stmt;
--sorted_result
eval
select o_orderkey, o_totalprice from orders where $c1;
insert into orders select * from r;
--sorted_result
eval
select o_orderkey, o_totalprice from orders where $c1;
drop table t,r;
deallocate prepare stmt;
--echo # FirstMatch PS
--echo # =============
eval
prepare stmt from "
delete from customer where $c5;
";
--sorted_result
eval
select c_name, c_acctbal from customer where $c5;
eval
create table t as
select * from customer where $c5;
execute stmt;
--sorted_result
eval
select c_name, c_acctbal from customer where $c5;
insert into customer select * from t;
--sorted_result
eval
select c_name, c_acctbal from customer where $c5;
eval
create table r as
select * from customer where $c5;
execute stmt;
--sorted_result
eval
select c_name, c_acctbal from customer where $c5;
insert into customer select * from r;
--sorted_result
eval
select c_name, c_acctbal from customer where $c5;
drop table t,r;
deallocate prepare stmt;
--echo # Materialization PS
--echo # ==================
eval
prepare stmt from "
delete from customer where $c7 and c_name like ?;
";
--sorted_result
eval
select c_name, c_acctbal from customer where $c7;
set @a1='Customer#%1_';
eval
create table t as
select * from customer where $c7 and c_name like @a1;
execute stmt using @a1;
--sorted_result
eval
select c_name, c_acctbal from customer where $c7;
insert into customer select * from t;
--sorted_result
eval
select c_name, c_acctbal from customer where $c7;
set @a2='Customer#%3_';
eval
create table r as
select * from customer where $c7 and c_name like @a2;
execute stmt using @a2;
--sorted_result
eval
select c_name, c_acctbal from customer where $c7;
insert into customer select * from r;
--sorted_result
eval
select c_name, c_acctbal from customer where $c7;
drop table t,r;
deallocate prepare stmt;
--echo # Materialization SJM PS
--echo # ======================
eval
prepare stmt from "
delete from customer where $c7 and c_acctbal between ? and ?;
";
--sorted_result
eval
select c_name, c_acctbal from customer where $c9;
set @a1=3500;
set @a2=4000;
eval
create table t as
select * from customer where $c9 and c_acctbal between @a1 and @a2;
execute stmt using @a1, @a2;
--sorted_result
eval
select c_name, c_acctbal from customer where $c9;
insert into customer select * from t;
--sorted_result
eval
select c_name, c_acctbal from customer where $c9;
set @a3=-1000;
set @a4=3500;
eval
create table r as
select * from customer where $c9 and c_acctbal between @a3 and @a4;
execute stmt using @a3, @a4;
--sorted_result
eval
select c_name, c_acctbal from customer where $c9;
insert into customer select * from r;
--sorted_result
eval
select c_name, c_acctbal from customer where $c9;
drop table t,r;
deallocate prepare stmt;
--echo # Pullout SP
--echo # ==========
eval
create procedure p(a1 int, a2 int)
delete from orders where $c1 and o_totalprice between a1 and a2;
--sorted_result
eval
select o_orderkey, o_totalprice from orders where $c1;
eval
create table t as
select * from orders where $c1 and o_totalprice between 150000 and 200000;
call p(150000, 200000);
--sorted_result
eval
select o_orderkey, o_totalprice from orders where $c1;
insert into orders select * from t;
--sorted_result
eval
select o_orderkey, o_totalprice from orders where $c1;
eval
create table r as
select * from orders where $c1 and o_totalprice between 180000 and 210000;
call p(180000, 210000);
--sorted_result
eval
select o_orderkey, o_totalprice from orders where $c1;
insert into orders select * from r;
--sorted_result
eval
select o_orderkey, o_totalprice from orders where $c1;
drop table t,r;
drop procedure p;
--echo # FirstMatch SP
--echo # =============
eval
create procedure p(a int)
delete from customer where $c5 and c_acctbal > a;
--sorted_result
eval
select c_name, c_acctbal from customer where $c5;
eval
create table t as
select * from customer where $c5 and c_acctbal > 4000;
call p(4000);
--sorted_result
eval
select c_name, c_acctbal from customer where $c5;
insert into customer select * from t;
--sorted_result
eval
select c_name, c_acctbal from customer where $c5;
eval
create table r as
select * from customer where $c5 and c_acctbal > 2000;
call p(2000);
--sorted_result
eval
select c_name, c_acctbal from customer where $c5;
insert into customer select * from r;
--sorted_result
eval
select c_name, c_acctbal from customer where $c5;
drop table t,r;
drop procedure p;
--echo # Materialization SP
--echo # ==================
eval
create procedure p()
delete from customer where $c7;
--sorted_result
eval
select c_name, c_acctbal from customer where $c7;
eval
create table t as
select * from customer where $c7;
call p();
--sorted_result
eval
select c_name, c_acctbal from customer where $c7;
insert into customer select * from t;
--sorted_result
eval
select c_name, c_acctbal from customer where $c7;
eval
create table r as
select * from customer where $c7;
call p();
--sorted_result
eval
select c_name, c_acctbal from customer where $c7;
insert into customer select * from r;
--sorted_result
eval
select c_name, c_acctbal from customer where $c7;
drop table t,r;
drop procedure p;
--echo # Materialization SJM SP
--echo # ======================
eval
create procedure p()
delete from customer where $c9;
--sorted_result
eval
select c_name, c_acctbal from customer where $c9;
eval
create table t as
select * from customer where $c9;
call p();
--sorted_result
eval
select c_name, c_acctbal from customer where $c9;
insert into customer select * from t;
--sorted_result
eval
select c_name, c_acctbal from customer where $c9;
eval
create table r as
select * from customer where $c9;
call p();
--sorted_result
eval
select c_name, c_acctbal from customer where $c9;
insert into customer select * from r;
--sorted_result
eval
select c_name, c_acctbal from customer where $c9;
drop table t,r;
drop procedure p;
--echo # Checking limitations
--echo # ====================
--echo # Check for DELETE ... RETURNING with SJ subquery in WHERE
--sorted_result
eval
select c_name from customer where $c7;
eval
create table t as
select * from customer where $c7;
eval
explain
delete from customer where $c7 returning c_name;
--sorted_result
eval
delete from customer where $c7 returning c_name;
--sorted_result
eval
select c_name from customer where $c7;
insert into customer select * from t;
--sorted_result
eval
select c_name from customer where $c7;
drop table t;
--sorted_result
eval
select c_name from customer where $c9;
eval
create table t as
select * from customer where $c9;
eval
explain
delete from customer where $c9 returning c_name;
--sorted_result
eval
delete from customer where $c9 returning c_name;
--sorted_result
eval
select c_name from customer where $c9;
insert into customer select * from t;
--sorted_result
eval
select c_name from customer where $c9;
drop table t;
--echo # Check for DELETE ... RETURNING with SJ subquery in WHERE
--sorted_result
eval
select c_name from customer where $c7;
eval
create table t as
select * from customer where $c7;
eval
explain
delete from customer where $c7 returning c_name;
--sorted_result
eval
delete from customer where $c7 returning c_name;
--sorted_result
eval
select c_name from customer where $c7;
insert into customer select * from t;
drop table t;
--echo # Check for DELETE ... ORDER BY ...LIMIT with SJ subquery in WHERE
let $c11=
o_orderDATE between '1992-01-01' and '1992-06-30' and
o_custkey in (select c_custkey from customer
where c_nationkey in (1,2));
--sorted_result
eval
select o_orderkey, o_totalprice from orders where $c11;
--echo # Should not use semi-join conversion because has ORDER BY ... LIMIT
eval
explain
delete from orders where $c11
order by o_totalprice limit 500;
eval
create table t as
select * from orders where $c11;
select o_orderkey, o_totalprice from t;
eval
delete from orders where $c11
order by o_totalprice limit 500;
--sorted_result
eval
select o_orderkey, o_totalprice from orders where $c11;
insert into orders select * from t;
--sorted_result
eval
select o_orderkey, o_totalprice from orders where $c11;
drop table t;
--echo # Should use semi-join converion
eval
explain
delete from orders where $c11;
eval
create table t as
select * from orders where $c11;
select o_orderkey, o_totalprice from t;
eval
delete from orders where $c11;
--sorted_result
eval
select o_orderkey, o_totalprice from orders where $c11;
insert into orders select * from t;
--sorted_result
eval
select o_orderkey, o_totalprice from orders where $c11;
drop table t;
DROP DATABASE dbt3_s001;

View File

@ -243,7 +243,7 @@ rows_examined sql_text
4 UPDATE t1 SET a=a+sleep(.02) WHERE a>2
8 UPDATE t1 SET a=a+sleep(.02) ORDER BY a DESC
1 UPDATE t2 set b=b+sleep(.02) limit 1
4 UPDATE t1 SET a=a+sleep(.02) WHERE a in (SELECT b from t2)
10 UPDATE t1 SET a=a+sleep(.02) WHERE a in (SELECT b from t2)
6 DELETE FROM t1 WHERE a=a+sleep(.02) ORDER BY a LIMIT 2
disconnect con2;
connection default;

View File

@ -218,14 +218,16 @@ INSERT INTO t2 VALUES (1), (2), (3);
#
EXPLAIN UPDATE t1 SET a = 10 WHERE 1 IN (SELECT 1 FROM t2 WHERE t2.b < 3);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
2 SUBQUERY t2 ALL NULL NULL NULL NULL 3 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
1 PRIMARY t1 ALL NULL NULL NULL NULL 3
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 3 Using where
FLUSH STATUS;
FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t1 SET a = 10 WHERE 1 IN (SELECT 1 FROM t2 WHERE t2.b < 3);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00 Using where
2 SUBQUERY t2 ALL NULL NULL NULL NULL 3 100.00 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 100.00
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 3 100.00 Using where
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@ -246,8 +248,9 @@ Handler_read_key 4
Handler_read_rnd_next 5
# Status of testing query execution:
Variable_name Value
Handler_read_key 4
Handler_read_rnd_next 5
Handler_read_key 5
Handler_read_rnd 3
Handler_read_rnd_next 12
Handler_update 3
DROP TABLE t1, t2;
@ -263,13 +266,13 @@ INSERT INTO t2 VALUES (1), (2), (3);
EXPLAIN UPDATE t1 SET a = 10 WHERE a IN (SELECT b FROM t2 WHERE t1.a < 3);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 3 Using where
1 PRIMARY t2 ALL NULL NULL NULL NULL 3 Using where; FirstMatch(t1)
FLUSH STATUS;
FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t1 SET a = 10 WHERE a IN (SELECT b FROM t2 WHERE t1.a < 3);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00 Using where
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 3 100.00 Using where
1 PRIMARY t2 ALL NULL NULL NULL NULL 3 100.00 Using where; FirstMatch(t1)
Warnings:
Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
# Status of EXPLAIN EXTENDED query
@ -900,14 +903,16 @@ INSERT INTO t2 VALUES (1), (2), (3), (1000);
#
EXPLAIN UPDATE t1 SET a = 10 WHERE a IN (SELECT a FROM t2);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 4 Using where
1 PRIMARY t1 ALL NULL NULL NULL NULL 3
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 3
FLUSH STATUS;
FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t1 SET a = 10 WHERE a IN (SELECT a FROM t2);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00 Using where
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 4 100.00 Using where
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1 100.00
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 3 100.00
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@ -928,8 +933,8 @@ Handler_read_key 4
Handler_read_rnd_next 9
# Status of testing query execution:
Variable_name Value
Handler_read_key 4
Handler_read_rnd_next 10
Handler_read_key 7
Handler_read_rnd_next 8
Handler_update 3
DROP TABLE t1, t2;
@ -986,14 +991,14 @@ INSERT INTO t1 VALUES (1), (2), (3), (4), (5);
#
EXPLAIN DELETE FROM t1 WHERE a1 IN (SELECT a2 FROM t2 WHERE a2 > 2);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 5 Using where
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 Using where
1 PRIMARY t1 ALL NULL NULL NULL NULL 5
1 PRIMARY t2 ALL NULL NULL NULL NULL 5 Using where; FirstMatch(t1)
FLUSH STATUS;
FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t1 WHERE a1 IN (SELECT a2 FROM t2 WHERE a2 > 2);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 5 100.00 Using where
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 5 100.00 Using where
1 PRIMARY t1 ALL NULL NULL NULL NULL 5 100.00
1 PRIMARY t2 ALL NULL NULL NULL NULL 5 100.00 Using where; FirstMatch(t1)
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
@ -2823,14 +2828,14 @@ INSERT INTO t2 VALUES (1), (2), (3);
EXPLAIN UPDATE t1 SET a = 10 WHERE a IN (SELECT * FROM (SELECT b FROM t2 ORDER BY b LIMIT 2,2) x);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
2 DEPENDENT SUBQUERY <derived3> unique_subquery distinct_key distinct_key 5 func 1
1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2 FirstMatch(t1)
3 DERIVED t2 ALL NULL NULL NULL NULL 3 Using filesort
FLUSH STATUS;
FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t1 SET a = 10 WHERE a IN (SELECT * FROM (SELECT b FROM t2 ORDER BY b LIMIT 2,2) x);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00 Using where
2 DEPENDENT SUBQUERY <derived3> unique_subquery distinct_key distinct_key 5 func 1 100.00
1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2 100.00 FirstMatch(t1)
3 DERIVED t2 ALL NULL NULL NULL NULL 3 100.00 Using filesort
# Status of EXPLAIN EXTENDED query
Variable_name Value

View File

@ -4466,6 +4466,16 @@ explain delete t0,t1 from t0, t1 where t0.a=t1.a and t1.a<3 {
}
}
},
{
"table": "t0",
"rowid_filters": [
{
"key": "a",
"build_cost": 0.174715752,
"rows": 3
}
]
},
{
"selectivity_for_indexes": [
{
@ -4531,6 +4541,16 @@ explain delete t0,t1 from t0, t1 where t0.a=t1.a and t1.a<3 {
}
}
},
{
"table": "t1",
"rowid_filters": [
{
"key": "a",
"build_cost": 0.174715752,
"rows": 3
}
]
},
{
"selectivity_for_indexes": [
{

View File

@ -20,9 +20,9 @@ delimiter ;|
--change_user foo
set optimizer_trace="enabled=on";
# --error 1142
# select * from db1.t1;
# select * from information_schema.OPTIMIZER_TRACE;
--error 1142
select * from db1.t1;
select * from information_schema.OPTIMIZER_TRACE;
set optimizer_trace="enabled=off";
--change_user root

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,551 @@
--disable_warnings
DROP DATABASE IF EXISTS dbt3_s001;
--enable_warnings
CREATE DATABASE dbt3_s001;
use dbt3_s001;
--disable_query_log
--disable_result_log
--disable_warnings
--source include/dbt3_s001.inc
--enable_warnings
--enable_result_log
--enable_query_log
create index i_n_name on nation(n_name);
analyze table nation;
--echo # Pullout
--echo # =======
let $c1=
o_orderDATE between '1992-01-01' and '1992-06-30' and
o_custkey in (select c_custkey from customer
where c_nationkey in (select n_nationkey from nation
where n_name='PERU'));
eval
explain
select o_orderkey, o_totalprice from orders where $c1;
eval
explain format=json
select o_orderkey, o_totalprice from orders where $c1;
eval
select o_orderkey, o_totalprice from orders where $c1;
eval
explain
update orders set o_totalprice = o_totalprice-50 where $c1;
eval
explain format=json
update orders set o_totalprice = o_totalprice-50 where $c1;
eval
update orders set o_totalprice = o_totalprice-50 where $c1;
eval
select o_orderkey, o_totalprice from orders where $c1;
eval
update orders set o_totalprice= o_totalprice+50 where $c1;
eval
select o_orderkey, o_totalprice from orders where $c1;
let $c2=
(ps_partkey, ps_suppkey) in
(select p_partkey, s_suppkey from part, supplier
where p_retailprice between 901 and 910 and
s_nationkey in (select n_nationkey from nation
where n_name='PERU'));
eval
explain
select ps_partkey, ps_suppkey, ps_supplycost from partsupp where $c2;
eval
select ps_partkey, ps_suppkey, ps_supplycost from partsupp where $c2;
eval
explain
update partsupp set ps_supplycost = ps_supplycost+2 where $c2;
eval
update partsupp set ps_supplycost = ps_supplycost+2 where $c2;
eval
select ps_partkey, ps_suppkey, ps_supplycost from partsupp where $c2;
eval
update partsupp set ps_supplycost = ps_supplycost-2 where $c2;
eval
select ps_partkey, ps_suppkey, ps_supplycost from partsupp where $c2;
let $c3=
ps_partkey in (select p_partkey from part
where p_retailprice between 901 and 910) and
ps_suppkey in (select s_suppkey from supplier
where s_nationkey in (select n_nationkey from nation
where n_name='PERU'));
eval
explain
select ps_partkey, ps_suppkey, ps_supplycost from partsupp where $c3;
eval
select ps_partkey, ps_suppkey, ps_supplycost from partsupp where $c3;
eval
explain
update partsupp set ps_supplycost = ps_supplycost+10 where $c3;
eval
update partsupp set ps_supplycost = ps_supplycost+10 where $c3;
eval
select ps_partkey, ps_suppkey, ps_supplycost from partsupp where $c3;
eval
update partsupp set ps_supplycost = ps_supplycost-10 where $c3;
eval
select ps_partkey, ps_suppkey, ps_supplycost from partsupp where $c3;
let $c4=
l_orderkey in (select o_orderkey from orders
where o_custkey in
(select c_custkey from customer
where c_nationkey in
(select n_nationkey from nation
where n_name='PERU'))
and
o_orderDATE between '1992-06-30' and '1992-12-31')
and
(l_partkey, l_suppkey) in
(select p_partkey, s_suppkey from part, supplier
where p_retailprice between 901 and 1000 and
s_nationkey in (select n_nationkey from nation
where n_name='PERU'));
eval
explain
select l_orderkey, l_linenumber, l_tax from lineitem where $c4;
eval
select l_orderkey, l_linenumber, l_tax from lineitem where $c4;
eval
explain
update lineitem set l_tax = (l_tax*100+1)/100 where $c4;
eval
update lineitem set l_tax = (l_tax*100+1)/100 where $c4;
eval
select l_orderkey, l_linenumber, l_tax from lineitem where $c4;
eval
update lineitem set l_tax = (l_tax*100-1)/100 where $c4;
eval
select l_orderkey, l_linenumber, l_tax from lineitem where $c4;
--echo # FirstMatch
--echo # ==========
set optimizer_switch='materialization=off';
let $c5=
c_nationkey in (select n_nationkey from nation
where n_regionkey in (1,2))
and
c_custkey in (select o_custkey from orders
where o_orderDATE between '1992-10-09' and '1993-06-08');
eval
explain
select c_name, c_acctbal from customer where $c5;
eval
explain format=json
select c_name, c_acctbal from customer where $c5;
eval
select c_name, c_acctbal from customer where $c5;
eval
explain
update customer set c_acctbal = c_acctbal+10 where $c5;
eval
explain format=json
update customer set c_acctbal = c_acctbal+10 where $c5;
eval
update customer set c_acctbal = c_acctbal+10 where $c5;
eval
select c_name, c_acctbal from customer where $c5;
eval
update customer set c_acctbal = c_acctbal-10 where $c5;
eval
select c_name, c_acctbal from customer where $c5;
set optimizer_switch='materialization=default';
let $c6=
c_nationkey in (select n_nationkey from nation where n_name='PERU')
and
c_custkey in (select o_custkey from orders
where o_orderDATE between "1992-01-09" and "1993-01-08");
eval
explain
select c_name, c_acctbal from customer where $c6;
eval
select c_name, c_acctbal from customer where $c6;
eval
explain
update customer set c_acctbal = c_acctbal+20 where $c6;
eval
update customer set c_acctbal = c_acctbal+20 where $c6;
eval
select c_name, c_acctbal from customer where $c6;
eval
update customer set c_acctbal = c_acctbal-20 where $c6;
eval
select c_name, c_acctbal from customer where $c6;
--echo # Materialization
--echo # ===============
let $c7=
c_custkey in (select o_custkey from orders
where o_orderDATE between '1992-01-09' and '1992-03-08');
eval
explain
select c_name, c_acctbal from customer where $c7;
eval
explain format=json
select c_name, c_acctbal from customer where $c7;
eval
select c_name, c_acctbal from customer where $c7;
eval
explain
update customer set c_acctbal = c_acctbal+5 where $c7;
eval
explain format=json
update customer set c_acctbal = c_acctbal+5 where $c7;
eval
update customer set c_acctbal = c_acctbal+5 where $c7;
eval
select c_name, c_acctbal from customer where $c7;
eval
update customer set c_acctbal = c_acctbal-5 where $c7;
eval
select c_name, c_acctbal from customer where $c7;
let $c8=
c_custkey in (select o_custkey from orders
where o_orderDATE between '1992-06-09' and '1993-01-08');
eval
explain
select c_name, c_acctbal from customer where $c8;
eval
select c_name, c_acctbal from customer where $c8;
eval
explain
update customer set c_acctbal = c_acctbal+1 where $c8;
eval
update customer set c_acctbal = c_acctbal+1 where $c8;
eval
select c_name, c_acctbal from customer where $c8;
eval
update customer set c_acctbal = c_acctbal-1 where $c8;
eval
select c_name, c_acctbal from customer where $c8;
--echo # Materialization SJM
--echo # ===================
let $c9=
c_custkey in (select o_custkey from orders
where o_orderDATE between '1992-01-09' and '1992-03-08'
group by o_custkey having count(o_custkey) > 1);
eval
explain
select c_name, c_acctbal from customer where $c9;
eval
explain format=json
select c_name, c_acctbal from customer where $c9;
eval
select c_name, c_acctbal from customer where $c9;
eval
explain
update customer set c_acctbal = c_acctbal-5 where $c9;
eval
explain format=json
update customer set c_acctbal = c_acctbal-5 where $c9;
eval
update customer set c_acctbal = c_acctbal-5 where $c9;
eval
select c_name, c_acctbal from customer where $c9;
eval
update customer set c_acctbal = c_acctbal+5 where $c9;
eval
select c_name, c_acctbal from customer where $c9;
let $c10=
c_custkey in (select o_custkey from orders
where o_orderDATE between '1992-01-09' and '1993-03-08'
group by o_custkey having count(o_custkey) > 5);
eval
explain
select c_name, c_acctbal from customer where $c10;
eval
select c_name, c_acctbal from customer where $c10;
eval
explain
update customer set c_acctbal = c_acctbal-1 where $c10;
eval
update customer set c_acctbal = c_acctbal-1 where $c10;
eval
select c_name, c_acctbal from customer where $c10;
eval
update customer set c_acctbal = c_acctbal+1 where $c10;
eval
select c_name, c_acctbal from customer where $c10;
--echo # Pullout PS
--echo # ==========
eval
prepare stmt from "
update orders set o_totalprice = o_totalprice+? where $c1;
";
eval
select o_orderkey, o_totalprice from orders where $c1;
set @a1=-20;
execute stmt using @a1;
eval
select o_orderkey, o_totalprice from orders where $c1;
set @a2=-10;
execute stmt using @a2;
eval
select o_orderkey, o_totalprice from orders where $c1;
execute stmt using -(@a1+@a2);
eval
select o_orderkey, o_totalprice from orders where $c1;
deallocate prepare stmt;
--echo # FirstMatch PS
--echo # =============
eval
prepare stmt from "
update customer set c_acctbal = c_acctbal+? where $c5;
";
eval
select c_name, c_acctbal from customer where $c5;
set @a1=15;
execute stmt using @a1;
eval
select c_name, c_acctbal from customer where $c5;
set @a2=5;
execute stmt using @a2;
eval
select c_name, c_acctbal from customer where $c5;
execute stmt using -(@a1+@a2);
eval
select c_name, c_acctbal from customer where $c5;
deallocate prepare stmt;
--echo # Materialization PS
--echo # ==================
eval
prepare stmt from "
update customer set c_acctbal = c_acctbal+? where $c7;
";
eval
select c_name, c_acctbal from customer where $c7;
set @a1=7;
execute stmt using @a1;
eval
select c_name, c_acctbal from customer where $c7;
set @a2=3;
execute stmt using @a2;
eval
select c_name, c_acctbal from customer where $c7;
execute stmt using -(@a1+@a2);
eval
select c_name, c_acctbal from customer where $c7;
deallocate prepare stmt;
--echo # Materialization SJM PS
--echo # ======================
eval
prepare stmt from "
update customer set c_acctbal = c_acctbal+? where $c9;
";
eval
select c_name, c_acctbal from customer where $c9;
set @a1=-2;
execute stmt using @a1;
eval
select c_name, c_acctbal from customer where $c9;
set @a2=-1;
execute stmt using @a2;
eval
select c_name, c_acctbal from customer where $c9;
execute stmt using -(@a1+@a2);
eval
select c_name, c_acctbal from customer where $c9;
deallocate prepare stmt;
--echo # Pullout SP
--echo # ==========
eval
create procedure p(d int)
update orders set o_totalprice = o_totalprice+d where $c1;
eval
select o_orderkey, o_totalprice from orders where $c1;
call p(-10);
eval
select o_orderkey, o_totalprice from orders where $c1;
call p(-20);
eval
select o_orderkey, o_totalprice from orders where $c1;
call p(10+20);
eval
select o_orderkey, o_totalprice from orders where $c1;
drop procedure p;
--echo # FirstMatch SP
--echo # =============
eval
create procedure p(d int)
update customer set c_acctbal = c_acctbal+d where $c5;
eval
select c_name, c_acctbal from customer where $c5;
call p(5);
eval
select c_name, c_acctbal from customer where $c5;
call p(15);
eval
select c_name, c_acctbal from customer where $c5;
call p(-(5+15));
eval
select c_name, c_acctbal from customer where $c5;
drop procedure p;
--echo # Materialization SP
--echo # ==================
eval
create procedure p(d int)
update customer set c_acctbal = c_acctbal+d where $c7;
eval
select c_name, c_acctbal from customer where $c7;
call p(3);
eval
select c_name, c_acctbal from customer where $c7;
call p(7);
eval
select c_name, c_acctbal from customer where $c7;
call p(-(3+7));
eval
select c_name, c_acctbal from customer where $c7;
drop procedure p;
--echo # Materialization SJM SP
--echo # ======================
eval
create procedure p(d int)
update customer set c_acctbal = c_acctbal+d where $c9;
eval
select c_name, c_acctbal from customer where $c9;
call p(-1);
eval
select c_name, c_acctbal from customer where $c9;
call p(-2);
eval
select c_name, c_acctbal from customer where $c9;
call p(1+2);
eval
select c_name, c_acctbal from customer where $c9;
drop procedure p;
--echo # Checking limitations
--echo # ====================
let $c11=
o_orderDATE between '1992-01-01' and '1992-06-30' and
o_custkey in (select c_custkey from customer
where c_nationkey in (1,2));
eval
select o_orderkey, o_totalprice from orders where $c11;
--echo # Should not use semi-join conversion because has ORDER BY ... LIMIT
eval
explain
update orders set o_totalprice = o_totalprice-50 where $c11
order by o_totalprice limit 500;
eval
update orders set o_totalprice = o_totalprice-50 where $c11
order by o_totalprice limit 500;
eval
select o_orderkey, o_totalprice from orders where $c11;
eval
update orders set o_totalprice = o_totalprice+50 where $c11
order by o_totalprice limit 500;
eval
select o_orderkey, o_totalprice from orders where $c11;
--echo # Should use semi-join converion
eval
explain
update orders set o_totalprice = o_totalprice-50 where $c11;
eval
update orders set o_totalprice = o_totalprice-50 where $c11;
eval
select o_orderkey, o_totalprice from orders where $c11;
eval
update orders set o_totalprice = o_totalprice+50 where $c11;
eval
select o_orderkey, o_totalprice from orders where $c11;
DROP DATABASE dbt3_s001;

View File

@ -567,11 +567,11 @@ bool SELECT_LEX::is_sj_conversion_prohibited(THD *thd)
switch (thd->lex->sql_command) {
case SQLCOM_UPDATE:
return
!((Sql_cmd_update *) cmd)->is_multitable() ||
!((Sql_cmd_update *) cmd)->is_multitable() &&
((Sql_cmd_update *) cmd)->processing_as_multitable_update_prohibited(thd);
case SQLCOM_DELETE:
return
!((Sql_cmd_delete *) cmd)->is_multitable() ||
!((Sql_cmd_delete *) cmd)->is_multitable() &&
((Sql_cmd_delete *) cmd)->processing_as_multitable_delete_prohibited(thd);
default:
return false;

View File

@ -491,8 +491,7 @@ void Opt_trace_start::init(THD *thd,
!list_has_optimizer_trace_table(tbl) &&
!sets_var_optimizer_trace(sql_command, set_vars) &&
!thd->system_thread &&
!ctx->disable_tracing_if_required() &&
!(thd->lex->context_analysis_only & CONTEXT_ANALYSIS_ONLY_PREPARE))
!ctx->disable_tracing_if_required())
{
ctx->start(thd, tbl, sql_command, query, query_length, query_charset,
thd->variables.optimizer_trace_max_mem_size);

View File

@ -1214,7 +1214,7 @@ retry:
}
}
else if (thd->lex->sql_command == SQLCOM_DELETE)
{
{
Sql_cmd_delete *cmd= (Sql_cmd_delete *) (thd->lex->m_sql_cmd);
if (cmd->is_multitable() || derived->derived->outer_select())
materialize= false;

View File

@ -348,7 +348,6 @@ bool Sql_cmd_delete::delete_from_single_table(THD *thd)
query_plan.using_filesort= FALSE;
THD_STAGE_INFO(thd, stage_init_update);
create_explain_query(thd->lex, thd->mem_root);
const bool delete_history= table_list->vers_conditions.delete_history;
DBUG_ASSERT(!(delete_history && table_list->period_conditions.is_set()));
@ -1668,6 +1667,10 @@ bool Sql_cmd_delete::prepare_inner(THD *thd)
{
goto err;
}
if (!multitable &&
select_lex->sj_subselects.elements)
multitable= true;
}
if (multitable)

View File

@ -5972,8 +5972,7 @@ make_join_statistics(JOIN *join, List<TABLE_LIST> &tables_list,
s->needed_reg=select->needed_reg;
select->quick=0;
impossible_range= records == 0 && s->table->reginfo.impossible_range;
if (join->thd->lex->sql_command == SQLCOM_SELECT &&
optimizer_flag(join->thd, OPTIMIZER_SWITCH_USE_ROWID_FILTER))
if (optimizer_flag(join->thd, OPTIMIZER_SWITCH_USE_ROWID_FILTER))
s->table->init_cost_info_for_usable_range_rowid_filters(join->thd);
}
if (!impossible_range)

View File

@ -382,7 +382,6 @@ bool Sql_cmd_update::update_single_table(THD *thd)
DBUG_ENTER("Sql_cmd_update::update_single_table");
THD_STAGE_INFO(thd, stage_init_update);
create_explain_query(thd->lex, thd->mem_root);
thd->table_map_for_update= 0;
@ -2479,6 +2478,8 @@ int multi_update::do_updates()
table = cur_table->table;
if (table == table_to_update)
continue; // Already updated
if (table->file->pushed_rowid_filter)
table->file->disable_pushed_rowid_filter();
org_updated= updated;
tmp_table= tmp_tables[cur_table->shared];
tmp_table->file->extra(HA_EXTRA_CACHE); // Change to read cache
@ -2673,7 +2674,8 @@ int multi_update::do_updates()
check_opt_it.rewind();
while (TABLE *tbl= check_opt_it++)
tbl->file->ha_rnd_end();
if (table->file->save_pushed_rowid_filter)
table->file->enable_pushed_rowid_filter();
}
DBUG_RETURN(0);
@ -2684,6 +2686,8 @@ err:
}
err2:
if (table->file->save_pushed_rowid_filter)
table->file->enable_pushed_rowid_filter();
if (table->file->inited)
(void) table->file->ha_rnd_end();
if (tmp_table->file->inited)
@ -2987,7 +2991,9 @@ bool Sql_cmd_update::prepare_inner(THD *thd)
{
goto err;
}
if (!multitable &&
select_lex->sj_subselects.elements)
multitable= true;
}
if (table_list->has_period())