Skip to content

Commit ccbe612

Browse files
MDEV-36106 New-style hints: [NO_]DERIVED_CONDITION_PUSHDOWN, [NO_]MERGE
Implements and tests the optimizer hints DERIVED_CONDITION_PUSHDOWN and NO_DERIVED_CONDITION_PUSHDOWN, table-level hints to enable and disable, respectively, the condition pushdown for derived tables which is typically controlled by the condition_pushdown_for_derived optimizer switch. Implements and tests the optimizer hints MERGE and NO_MERGE, table-level hints to enable and disable, respectively, the derived_merge optimization which is typically controlled by the derived_merge optimizer switch. Sometimes hints need to be fixed before TABLE instances are available, but after TABLE_LIST instances have been created (as in the cases of MERGE and NO_MERGE). This commit introduces a new API to allow hint fixing with only the TABLE_LIST, so long as those hints are not index-level.
1 parent 8041255 commit ccbe612

10 files changed

+2711
-40
lines changed

mysql-test/main/opt_hints_derived_condition_pushdown.result

+1,691
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,295 @@
1+
--source include/have_sequence.inc
2+
--disable_view_protocol
3+
create table t1 (a int, b int, c int);
4+
create table t2 (a int, b int, c int, d decimal);
5+
insert into t1 values
6+
(1,21,345), (1,33,7), (8,33,114), (1,21,500), (1,19,107), (5,14,787),
7+
(8,33,123), (9,10,211), (5,16,207), (1,33,988), (5,27,132), (1,21,104),
8+
(6,20,309), (6,20,315), (1,21,101), (8,33,404), (9,10,800), (1,21,123),
9+
(7,11,708), (6,20,214);
10+
create index t1_a on t1 (a);
11+
insert into t2 values
12+
(2,3,207,207.0000), (1,21,909,12.0000), (7,13,312,406.0000),
13+
(8,64,248,107.0000), (6,20,315,279.3333), (1,19,203,107.0000),
14+
(8,80,800,314.0000), (3,12,231,190.0000), (6,23,303,909.0000);
15+
create view v1 as select a, b, max(c) as max_c, avg(c) as avg_c from t1
16+
group by a,b having max_c < 707;
17+
create table t3 select 2*seq as a, 2*seq+1 as b from seq_0_to_1000;
18+
CREATE TABLE t4 (a INT, b INT);
19+
INSERT INTO t4 VALUES (1,2),(2,3),(3,4);
20+
create table t5 select seq as i, 10*seq as j from seq_1_to_10;
21+
create view v2 as select * from t5;
22+
create table t6 (a int primary key);
23+
insert into t6 select * from seq_1_to_50;
24+
create view v6 as select a from t6 where a mod 2 = 1;
25+
26+
set @save_optimizer_switch=@@optimizer_switch;
27+
28+
set session optimizer_switch='condition_pushdown_for_derived=on';
29+
30+
--source include/explain-no-costs.inc
31+
explain format=json select /*+ NO_DERIVED_CONDITION_PUSHDOWN(v1) */ * from v1,t2 where
32+
((v1.max_c>300) and (v1.avg_c>t2.d) and (v1.b=t2.b)) or
33+
((v1.max_c<135) and (v1.max_c<t2.c) and (v1.a=t2.a));
34+
select /*+ NO_DERIVED_CONDITION_PUSHDOWN(v1) */ * from v1,t2 where
35+
((v1.max_c>300) and (v1.avg_c>t2.d) and (v1.b=t2.b)) or
36+
((v1.max_c<135) and (v1.max_c<t2.c) and (v1.a=t2.a));
37+
38+
--source include/explain-no-costs.inc
39+
explain format=json select /*+ NO_DERIVED_CONDITION_PUSHDOWN(dt)*/ * from
40+
(select t3.b as a from t3 group by t3.a) dt where (dt.a=3 or dt.a=5);
41+
select /*+ NO_DERIVED_CONDITION_PUSHDOWN(dt)*/ * from
42+
(select t3.b as a from t3 group by t3.a) dt where (dt.a=3 or dt.a=5);
43+
44+
--source include/explain-no-costs.inc
45+
explain format=json select * from (
46+
select t3.b as a from t3 group by t3.a
47+
) dt join (
48+
select t3.a as a, t3.b as b from t3 where t3.a % 2 = 0 group by t3.b
49+
) du where (dt.a=3 or dt.a=5) and (du.a % 10 <> 0 or du.a % 5 <> 0);
50+
select count(*) from (
51+
select t3.b as a from t3 group by t3.a
52+
) dt join (
53+
select t3.a as a, t3.b as b from t3 where t3.a % 2 = 0 group by t3.b
54+
) du where (dt.a=3 or dt.a=5) and (du.a % 10 <> 0 or du.a % 5 <> 0);
55+
56+
--source include/explain-no-costs.inc
57+
explain format=json select /*+ NO_DERIVED_CONDITION_PUSHDOWN(dt)*/ * from (
58+
select t3.b as a from t3 group by t3.a
59+
) dt join (
60+
select t3.a as a, t3.b as b from t3 where t3.a % 2 = 0 group by t3.b
61+
) du where (dt.a=3 or dt.a=5) and (du.a % 10 <> 0 or du.a % 5 <> 0);
62+
select /*+ NO_DERIVED_CONDITION_PUSHDOWN(dt)*/ count(*) from (
63+
select t3.b as a from t3 group by t3.a
64+
) dt join (
65+
select t3.a as a, t3.b as b from t3 where t3.a % 2 = 0 group by t3.b
66+
) du where (dt.a=3 or dt.a=5) and (du.a % 10 <> 0 or du.a % 5 <> 0);
67+
68+
--source include/explain-no-costs.inc
69+
explain format=json select /*+ NO_DERIVED_CONDITION_PUSHDOWN(du)*/ * from (
70+
select t3.b as a from t3 group by t3.a
71+
) dt join (
72+
select t3.a as a, t3.b as b from t3 where t3.a % 2 = 0 group by t3.b
73+
) du where (dt.a=3 or dt.a=5) and (du.a % 10 <> 0 or du.a % 5 <> 0);
74+
select /*+ NO_DERIVED_CONDITION_PUSHDOWN(du)*/ count(*) from (
75+
select t3.b as a from t3 group by t3.a
76+
) dt join (
77+
select t3.a as a, t3.b as b from t3 where t3.a % 2 = 0 group by t3.b
78+
) du where (dt.a=3 or dt.a=5) and (du.a % 10 <> 0 or du.a % 5 <> 0);
79+
80+
--source include/explain-no-costs.inc
81+
explain format=json select /*+ NO_DERIVED_CONDITION_PUSHDOWN(dt,du)*/ * from (
82+
select t3.b as a from t3 group by t3.a
83+
) dt join (
84+
select t3.a as a, t3.b as b from t3 where t3.a % 2 = 0 group by t3.b
85+
) du where (dt.a=3 or dt.a=5) and (du.a % 10 <> 0 or du.a % 5 <> 0);
86+
select /*+ NO_DERIVED_CONDITION_PUSHDOWN(dt,du)*/ count(*) from (
87+
select t3.b as a from t3 group by t3.a
88+
) dt join (
89+
select t3.a as a, t3.b as b from t3 where t3.a % 2 = 0 group by t3.b
90+
) du where (dt.a=3 or dt.a=5) and (du.a % 10 <> 0 or du.a % 5 <> 0);
91+
92+
--source include/explain-no-costs.inc
93+
explain format=json SELECT /*+ NO_DERIVED_CONDITION_PUSHDOWN(dt) */ * FROM (
94+
SELECT t4.b AS a
95+
FROM t4
96+
GROUP BY t4.a
97+
) dt WHERE (dt.a=2);
98+
SELECT /*+ NO_DERIVED_CONDITION_PUSHDOWN(dt) */ * FROM (
99+
SELECT t4.b AS a
100+
FROM t4
101+
GROUP BY t4.a
102+
) dt WHERE (dt.a=2);
103+
104+
--source include/explain-no-costs.inc
105+
explain format=json SELECT /*+ NO_DERIVED_CONDITION_PUSHDOWN(dt) */ * FROM (
106+
SELECT qb.b AS a
107+
FROM (
108+
select 1*t4.b as b, 1*t4.a as a from t4
109+
) qb
110+
GROUP BY qb.a
111+
) dt WHERE (dt.a=2);
112+
SELECT /*+ NO_DERIVED_CONDITION_PUSHDOWN(dt) */ * FROM (
113+
SELECT qb.b AS a
114+
FROM (
115+
select 1*t4.b as b, 1*t4.a as a from t4
116+
) qb
117+
GROUP BY qb.a
118+
) dt WHERE (dt.a=2);
119+
120+
--source include/explain-no-costs.inc
121+
explain format=json
122+
select /*+ NO_DERIVED_CONDITION_PUSHDOWN(@qb1 dv6) */ * from
123+
(select /*+ QB_NAME(qb4) */ * from
124+
(select /*+ QB_NAME(qb3) */ * from
125+
(select /*+ QB_NAME(qb2) */ * from
126+
(select /*+ QB_NAME(qb1) */ avg_a from
127+
(select avg(a) as avg_a from v6) dv6
128+
) dv6) dv6) dv6) dv6 where avg_a <> 0;
129+
130+
--source include/explain-no-costs.inc
131+
explain format=json
132+
select /*+ NO_DERIVED_CONDITION_PUSHDOWN(@qb2 dv6) */ * from
133+
(select /*+ QB_NAME(qb4) */ * from
134+
(select /*+ QB_NAME(qb3) */ * from
135+
(select /*+ QB_NAME(qb2) */ * from
136+
(select /*+ QB_NAME(qb1) */ avg_a from
137+
(select avg(a) as avg_a from v6) dv6
138+
) dv6) dv6) dv6) dv6 where avg_a <> 0;
139+
140+
--source include/explain-no-costs.inc
141+
explain format=json
142+
select /*+ NO_DERIVED_CONDITION_PUSHDOWN(@qb3 dv6) */ * from
143+
(select /*+ QB_NAME(qb4) */ * from
144+
(select /*+ QB_NAME(qb3) */ * from
145+
(select /*+ QB_NAME(qb2) */ * from
146+
(select /*+ QB_NAME(qb1) */ avg_a from
147+
(select avg(a) as avg_a from v6) dv6
148+
) dv6) dv6) dv6) dv6 where avg_a <> 0;
149+
150+
--source include/explain-no-costs.inc
151+
explain format=json
152+
select /*+ NO_DERIVED_CONDITION_PUSHDOWN(@qb4 dv6) */ * from
153+
(select /*+ QB_NAME(qb4) */ * from
154+
(select /*+ QB_NAME(qb3) */ * from
155+
(select /*+ QB_NAME(qb2) */ * from
156+
(select /*+ QB_NAME(qb1) */ avg_a from
157+
(select avg(a) as avg_a from v6) dv6
158+
) dv6) dv6) dv6) dv6 where avg_a <> 0;
159+
160+
set session optimizer_switch='condition_pushdown_for_derived=off';
161+
162+
--source include/explain-no-costs.inc
163+
explain format=json select /*+ DERIVED_CONDITION_PUSHDOWN(v1) */ * from v1,t2 where
164+
((v1.max_c>300) and (v1.avg_c>t2.d) and (v1.b=t2.b)) or
165+
((v1.max_c<135) and (v1.max_c<t2.c) and (v1.a=t2.a));
166+
select /*+ DERIVED_CONDITION_PUSHDOWN(v1) */ * from v1,t2 where
167+
((v1.max_c>300) and (v1.avg_c>t2.d) and (v1.b=t2.b)) or
168+
((v1.max_c<135) and (v1.max_c<t2.c) and (v1.a=t2.a));
169+
170+
--source include/explain-no-costs.inc
171+
explain format=json select /*+ DERIVED_CONDITION_PUSHDOWN(dt)*/ * from
172+
(select t3.b as a from t3 group by t3.a) dt where (dt.a=3 or dt.a=5);
173+
select /*+ DERIVED_CONDITION_PUSHDOWN(dt)*/ * from
174+
(select t3.b as a from t3 group by t3.a) dt where (dt.a=3 or dt.a=5);
175+
176+
--source include/explain-no-costs.inc
177+
explain format=json SELECT /*+ DERIVED_CONDITION_PUSHDOWN(dt) */ * FROM (
178+
SELECT t4.b AS a
179+
FROM t4
180+
GROUP BY t4.a
181+
) dt WHERE (dt.a=2);
182+
SELECT /*+ DERIVED_CONDITION_PUSHDOWN(dt) */ * FROM (
183+
SELECT t4.b AS a
184+
FROM t4
185+
GROUP BY t4.a
186+
) dt WHERE (dt.a=2);
187+
188+
--source include/explain-no-costs.inc
189+
explain format=json SELECT /*+ DERIVED_CONDITION_PUSHDOWN(dt) */ * FROM (
190+
SELECT qb.b AS a
191+
FROM (
192+
select 1*t4.b as b, 1*t4.a as a from t4
193+
) qb
194+
GROUP BY qb.a
195+
) dt WHERE (dt.a=2);
196+
SELECT /*+ DERIVED_CONDITION_PUSHDOWN(dt) */ * FROM (
197+
SELECT qb.b AS a
198+
FROM (
199+
select 1*t4.b as b, 1*t4.a as a from t4
200+
) qb
201+
GROUP BY qb.a
202+
) dt WHERE (dt.a=2);
203+
204+
--source include/explain-no-costs.inc
205+
explain format=json select * from (
206+
select t3.b as a from t3 group by t3.a
207+
) dt join (
208+
select t3.a as a, t3.b as b from t3 where t3.a % 2 = 0 group by t3.b
209+
) du where (dt.a=3 or dt.a=5) and (du.a % 10 <> 0 or du.a % 5 <> 0);
210+
select count(*) from (
211+
select t3.b as a from t3 group by t3.a
212+
) dt join (
213+
select t3.a as a, t3.b as b from t3 where t3.a % 2 = 0 group by t3.b
214+
) du where (dt.a=3 or dt.a=5) and (du.a % 10 <> 0 or du.a % 5 <> 0);
215+
216+
--source include/explain-no-costs.inc
217+
explain format=json select /*+ DERIVED_CONDITION_PUSHDOWN(dt)*/ * from (
218+
select t3.b as a from t3 group by t3.a
219+
) dt join (
220+
select t3.a as a, t3.b as b from t3 where t3.a % 2 = 0 group by t3.b
221+
) du where (dt.a=3 or dt.a=5) and (du.a % 10 <> 0 or du.a % 5 <> 0);
222+
select /*+ DERIVED_CONDITION_PUSHDOWN(dt)*/ count(*) from (
223+
select t3.b as a from t3 group by t3.a
224+
) dt join (
225+
select t3.a as a, t3.b as b from t3 where t3.a % 2 = 0 group by t3.b
226+
) du where (dt.a=3 or dt.a=5) and (du.a % 10 <> 0 or du.a % 5 <> 0);
227+
228+
--source include/explain-no-costs.inc
229+
explain format=json select /*+ DERIVED_CONDITION_PUSHDOWN(du)*/ * from (
230+
select t3.b as a from t3 group by t3.a
231+
) dt join (
232+
select t3.a as a, t3.b as b from t3 where t3.a % 2 = 0 group by t3.b
233+
) du where (dt.a=3 or dt.a=5) and (du.a % 10 <> 0 or du.a % 5 <> 0);
234+
select /*+ DERIVED_CONDITION_PUSHDOWN(du)*/ count(*) from (
235+
select t3.b as a from t3 group by t3.a
236+
) dt join (
237+
select t3.a as a, t3.b as b from t3 where t3.a % 2 = 0 group by t3.b
238+
) du where (dt.a=3 or dt.a=5) and (du.a % 10 <> 0 or du.a % 5 <> 0);
239+
240+
--source include/explain-no-costs.inc
241+
explain format=json select /*+ DERIVED_CONDITION_PUSHDOWN(dt,du)*/ * from (
242+
select t3.b as a from t3 group by t3.a
243+
) dt join (
244+
select t3.a as a, t3.b as b from t3 where t3.a % 2 = 0 group by t3.b
245+
) du where (dt.a=3 or dt.a=5) and (du.a % 10 <> 0 or du.a % 5 <> 0);
246+
select /*+ DERIVED_CONDITION_PUSHDOWN(dt,du)*/ count(*) from (
247+
select t3.b as a from t3 group by t3.a
248+
) dt join (
249+
select t3.a as a, t3.b as b from t3 where t3.a % 2 = 0 group by t3.b
250+
) du where (dt.a=3 or dt.a=5) and (du.a % 10 <> 0 or du.a % 5 <> 0);
251+
252+
--source include/explain-no-costs.inc
253+
explain format=json
254+
select /*+ DERIVED_CONDITION_PUSHDOWN(@qb1 dv6) */ * from
255+
(select /*+ QB_NAME(qb4) */ * from
256+
(select /*+ QB_NAME(qb3) */ * from
257+
(select /*+ QB_NAME(qb2) */ * from
258+
(select /*+ QB_NAME(qb1) */ avg_a from
259+
(select avg(a) as avg_a from v6) dv6
260+
) dv6) dv6) dv6) dv6 where avg_a <> 0;
261+
262+
--source include/explain-no-costs.inc
263+
explain format=json
264+
select /*+ DERIVED_CONDITION_PUSHDOWN(@qb2 dv6) */ * from
265+
(select /*+ QB_NAME(qb4) */ * from
266+
(select /*+ QB_NAME(qb3) */ * from
267+
(select /*+ QB_NAME(qb2) */ * from
268+
(select /*+ QB_NAME(qb1) */ avg_a from
269+
(select avg(a) as avg_a from v6) dv6
270+
) dv6) dv6) dv6) dv6 where avg_a <> 0;
271+
272+
--source include/explain-no-costs.inc
273+
explain format=json
274+
select /*+ DERIVED_CONDITION_PUSHDOWN(@qb3 dv6) */ * from
275+
(select /*+ QB_NAME(qb4) */ * from
276+
(select /*+ QB_NAME(qb3) */ * from
277+
(select /*+ QB_NAME(qb2) */ * from
278+
(select /*+ QB_NAME(qb1) */ avg_a from
279+
(select avg(a) as avg_a from v6) dv6
280+
) dv6) dv6) dv6) dv6 where avg_a <> 0;
281+
282+
--source include/explain-no-costs.inc
283+
explain format=json
284+
select /*+ DERIVED_CONDITION_PUSHDOWN(@qb4 dv6) */ * from
285+
(select /*+ QB_NAME(qb4) */ * from
286+
(select /*+ QB_NAME(qb3) */ * from
287+
(select /*+ QB_NAME(qb2) */ * from
288+
(select /*+ QB_NAME(qb1) */ avg_a from
289+
(select avg(a) as avg_a from v6) dv6
290+
) dv6) dv6) dv6) dv6 where avg_a <> 0;
291+
292+
set optimizer_switch=@save_optimizer_switch;
293+
drop view v1, v2, v6;
294+
drop table t1, t2, t3, t4, t5, t6;
295+
--enable_view_protocol

0 commit comments

Comments
 (0)