mariadb/mysql-test/main/in_subq_cond_pushdown.result

3802 lines
86 KiB
Text
Raw Normal View History

MDEV-12387 Push conditions into materialized subqueries The logic and the implementation scheme are similar with the MDEV-9197 Pushdown conditions into non-mergeable views/derived tables How the push down is made on the example: select * from t1 where a>3 and b>10 and (a,b) in (select x,max(y) from t2 group by x); --> select * from t1 where a>3 and b>10 and (a,b) in (select x,max(y) from t2 where x>3 group by x having max(y)>10); The implementation scheme: 1. Search for the condition cond that depends only on the fields from the left part of the IN subquery (left_part) 2. Find fields F_group in the select of the right part of the IN subquery (right_part) that are used in the GROUP BY 3. Extract from the cond condition cond_where that depends only on the fields from the left_part that stay at the same places in the left_part (have the same indexes) as the F_group fields in the projection of the right_part 4. Transform cond_where so it can be pushed into the WHERE clause of the right_part and delete cond_where from the cond 5. Transform cond so it can be pushed into the HAVING clause of the right_part The optimization is made in the Item_in_subselect::pushdown_cond_for_in_subquery() and is controlled by the variable condition_pushdown_for_subquery. New test file in_subq_cond_pushdown.test is created. There are also some changes made for setup_jtbm_semi_joins(). Now it is decomposed into the 2 procedures: setup_degenerate_jtbm_semi_joins() that is called before optimize_cond() for cond and setup_jtbm_semi_joins() that is called after optimize_cond(). New setup_jtbm_semi_joins() is made in the way so that the result of its work is the same as if it was called before optimize_cond(). The code that is common for pushdown into materialized derived and into materialized IN subqueries is factored out into pushdown_cond_for_derived(), Item_in_subselect::pushdown_cond_for_in_subquery() and st_select_lex::pushdown_cond_into_where_clause().
2018-05-15 23:45:59 +02:00
CREATE TABLE t1 (a INT, b INT, c INT, d INT);
CREATE TABLE t2 (e INT, f INT, g INT);
CREATE TABLE t3 (x INT, y INT);
INSERT INTO t1 VALUES
(1,1,18,1), (2,1,25,1), (1,3,40,1), (2,3,40,4),
(4,2,24,4), (3,2,23,1), (1,2,40,2), (3,4,17,2),
(5,5,65,1), (2,3,70,3), (1,4,35,3), (2,3,25,3),
(2,2,40,4), (1,4,55,1), (5,3,72,4), (1,2,70,5);
INSERT INTO t2 VALUES
(1,2,38), (2,3,15), (1,3,40), (1,4,35),
(2,2,70), (3,4,23), (5,5,12), (5,4,17),
(3,3,17), (4,2,24), (2,5,25), (5,1,65);
INSERT INTO t3 VALUES
(1,25), (1,18), (2,15), (4,24),
(1,35), (3,23), (3,17), (2,15);
CREATE VIEW v1 AS
(
SELECT t3.x AS v1_x, t3.y AS v1_y FROM t3 WHERE t3.x<=3
);
CREATE VIEW v2 AS
(
SELECT t2.e, t2.f, MAX(t2.g) AS max_g
FROM t2
GROUP BY t2.e
HAVING max_g>25
);
# conjunctive subformula : pushing into HAVING
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT * FROM t1
WHERE t1.c<25 AND
(t1.a,t1.c) IN (SELECT t2.e,MAX(t2.g) FROM t2 WHERE t2.e<5 GROUP BY t2.e);
a b c d
4 2 24 4
3 2 23 1
SELECT * FROM t1
WHERE t1.c<25 AND
(t1.a,t1.c) IN (SELECT t2.e,MAX(t2.g) FROM t2 WHERE t2.e<5 GROUP BY t2.e);
a b c d
4 2 24 4
3 2 23 1
EXPLAIN SELECT * FROM t1
WHERE t1.c<25 AND
(t1.a,t1.c) IN (SELECT t2.e,MAX(t2.g) FROM t2 WHERE t2.e<5 GROUP BY t2.e);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 16 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 test.t1.a,test.t1.c 1
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 12 Using where; Using temporary
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE t1.c<25 AND
(t1.a,t1.c) IN (SELECT t2.e,MAX(t2.g) FROM t2 WHERE t2.e<5 GROUP BY t2.e);
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100,
"attached_condition": "t1.c < 25 and t1.a is not null and t1.c is not null"
},
"table": {
"table_name": "<subquery2>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "8",
"used_key_parts": ["e", "MAX(t2.g)"],
"ref": ["test.t1.a", "test.t1.c"],
"rows": 1,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
"having_condition": "`MAX(t2.g)` < 25",
"temporary_table": {
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows": 12,
"filtered": 100,
"attached_condition": "t2.e < 5"
}
}
}
}
}
}
}
# extracted AND formula : pushing into HAVING
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT * FROM t1
WHERE t1.c>55 AND t1.b<4 AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d
2 3 70 3
SELECT * FROM t1
WHERE t1.c>55 AND t1.b<4 AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d
2 3 70 3
EXPLAIN SELECT * FROM t1
WHERE t1.c>55 AND t1.b<4 AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 16 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 12 test.t1.a,test.t1.b,test.t1.c 1
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 12 Using where; Using temporary
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE t1.c>55 AND t1.b<4 AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100,
"attached_condition": "t1.c > 55 and t1.b < 4 and t1.a is not null and t1.b is not null and t1.c is not null"
},
"table": {
"table_name": "<subquery2>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "12",
"used_key_parts": ["e", "f", "MAX(t2.g)"],
"ref": ["test.t1.a", "test.t1.b", "test.t1.c"],
"rows": 1,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
"having_condition": "`MAX(t2.g)` > 55 and t2.f < 4",
"temporary_table": {
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows": 12,
"filtered": 100,
"attached_condition": "t2.e < 5"
}
}
}
}
}
}
}
# extracted OR formula : pushing into HAVING
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT * FROM t1
WHERE (t1.c>60 OR t1.c<25) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d
4 2 24 4
2 3 70 3
SELECT * FROM t1
WHERE (t1.c>60 OR t1.c<25) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d
4 2 24 4
2 3 70 3
EXPLAIN SELECT * FROM t1
WHERE (t1.c>60 OR t1.c<25) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 16 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 12 test.t1.a,test.t1.b,test.t1.c 1
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 12 Using where; Using temporary
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE (t1.c>60 OR t1.c<25) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100,
"attached_condition": "(t1.c > 60 or t1.c < 25) and t1.a is not null and t1.b is not null and t1.c is not null"
},
"table": {
"table_name": "<subquery2>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "12",
"used_key_parts": ["e", "f", "MAX(t2.g)"],
"ref": ["test.t1.a", "test.t1.b", "test.t1.c"],
"rows": 1,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
"having_condition": "`MAX(t2.g)` > 60 or `MAX(t2.g)` < 25",
"temporary_table": {
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows": 12,
"filtered": 100,
"attached_condition": "t2.e < 5"
}
}
}
}
}
}
}
# extracted AND-OR formula : pushing into HAVING
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT * FROM t1
WHERE ((t1.c>60 OR t1.c<25) AND t1.b>2) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d
2 3 70 3
SELECT * FROM t1
WHERE ((t1.c>60 OR t1.c<25) AND t1.b>2) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d
2 3 70 3
EXPLAIN SELECT * FROM t1
WHERE ((t1.c>60 OR t1.c<25) AND t1.b>2) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 16 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 12 test.t1.a,test.t1.b,test.t1.c 1
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 12 Using where; Using temporary
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE ((t1.c>60 OR t1.c<25) AND t1.b>2) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100,
"attached_condition": "(t1.c > 60 or t1.c < 25) and t1.b > 2 and t1.a is not null and t1.b is not null and t1.c is not null"
},
"table": {
"table_name": "<subquery2>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "12",
"used_key_parts": ["e", "f", "MAX(t2.g)"],
"ref": ["test.t1.a", "test.t1.b", "test.t1.c"],
"rows": 1,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
"having_condition": "(`MAX(t2.g)` > 60 or `MAX(t2.g)` < 25) and t2.f > 2",
"temporary_table": {
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows": 12,
"filtered": 100,
"attached_condition": "t2.e < 5"
}
}
}
}
}
}
}
# conjunctive subformula : pushing into HAVING
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT * FROM t1
WHERE ((t1.a<2 OR t1.d>3) AND t1.b>1) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d
4 2 24 4
1 2 40 2
SELECT * FROM t1
WHERE ((t1.a<2 OR t1.d>3) AND t1.b>1) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d
4 2 24 4
1 2 40 2
EXPLAIN SELECT * FROM t1
WHERE ((t1.a<2 OR t1.d>3) AND t1.b>1) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 16 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 12 test.t1.a,test.t1.b,test.t1.c 1
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 12 Using where; Using temporary
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE ((t1.a<2 OR t1.d>3) AND t1.b>1) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100,
"attached_condition": "(t1.a < 2 or t1.d > 3) and t1.b > 1 and t1.a is not null and t1.b is not null and t1.c is not null"
},
"table": {
"table_name": "<subquery2>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "12",
"used_key_parts": ["e", "f", "MAX(t2.g)"],
"ref": ["test.t1.a", "test.t1.b", "test.t1.c"],
"rows": 1,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
"having_condition": "t2.f > 1",
"temporary_table": {
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows": 12,
"filtered": 100,
"attached_condition": "t2.e < 5"
}
}
}
}
}
}
}
# using view IN subquery defINition : pushing into HAVING
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT * FROM t1
WHERE t1.c>20 AND
(t1.a,t1.c) IN
(
SELECT v1_x,MAX(v1_y)
FROM v1
WHERE v1_x>1
GROUP BY v1_x
)
;
a b c d
3 2 23 1
SELECT * FROM t1
WHERE t1.c>20 AND
(t1.a,t1.c) IN
(
SELECT v1_x,MAX(v1_y)
FROM v1
WHERE v1_x>1
GROUP BY v1_x
)
;
a b c d
3 2 23 1
EXPLAIN SELECT * FROM t1
WHERE t1.c>20 AND
(t1.a,t1.c) IN
(
SELECT v1_x,MAX(v1_y)
FROM v1
WHERE v1_x>1
GROUP BY v1_x
)
;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 16 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 test.t1.a,test.t1.c 1
2 MATERIALIZED t3 ALL NULL NULL NULL NULL 8 Using where; Using temporary
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE t1.c>20 AND
(t1.a,t1.c) IN
(
SELECT v1_x,MAX(v1_y)
FROM v1
WHERE v1_x>1
GROUP BY v1_x
)
;
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100,
"attached_condition": "t1.c > 20 and t1.a is not null and t1.c is not null"
},
"table": {
"table_name": "<subquery2>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "8",
"used_key_parts": ["v1_x", "MAX(v1_y)"],
"ref": ["test.t1.a", "test.t1.c"],
"rows": 1,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
"having_condition": "`MAX(v1_y)` > 20",
"temporary_table": {
"table": {
"table_name": "t3",
"access_type": "ALL",
"rows": 8,
"filtered": 100,
"attached_condition": "t3.x > 1 and t3.x <= 3"
}
}
}
}
}
}
}
# using equality : pushing into WHERE
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT * FROM t1,v1
WHERE t1.c>20 AND t1.c=v1_y AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d v1_x v1_y
3 2 23 1 3 23
SELECT * FROM t1,v1
WHERE t1.c>20 AND t1.c=v1_y AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d v1_x v1_y
3 2 23 1 3 23
EXPLAIN SELECT * FROM t1,v1
WHERE t1.c>20 AND t1.c=v1_y AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t3 ALL NULL NULL NULL NULL 8 Using where
1 PRIMARY t1 ALL NULL NULL NULL NULL 16 Using where; Using join buffer (flat, BNL join)
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 test.t1.a,test.t3.y 1
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 12 Using where; Using temporary
EXPLAIN FORMAT=JSON SELECT * FROM t1,v1
WHERE t1.c>20 AND t1.c=v1_y AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t3",
"access_type": "ALL",
"rows": 8,
"filtered": 100,
"attached_condition": "t3.y > 20 and t3.x <= 3 and t3.y is not null"
},
"block-nl-join": {
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100
},
"buffer_type": "flat",
"buffer_size": "256Kb",
"join_type": "BNL",
"attached_condition": "t1.c = t3.y and t1.a is not null"
},
"table": {
"table_name": "<subquery2>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "8",
"used_key_parts": ["e", "MAX(t2.g)"],
"ref": ["test.t1.a", "test.t3.y"],
"rows": 1,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
"having_condition": "`MAX(t2.g)` > 20",
"temporary_table": {
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows": 12,
"filtered": 100,
"attached_condition": "t2.e < 5"
}
}
}
}
}
}
}
# conjunctive subformula : pushing into WHERE
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT * FROM t1
WHERE t1.a<2 AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d
1 3 40 1
1 2 40 2
SELECT * FROM t1
WHERE t1.a<2 AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d
1 3 40 1
1 2 40 2
EXPLAIN SELECT * FROM t1
WHERE t1.a<2 AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 16 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 test.t1.a,test.t1.c 1
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 12 Using where; Using temporary
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE t1.a<2 AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100,
"attached_condition": "t1.a < 2 and t1.a is not null and t1.c is not null"
},
"table": {
"table_name": "<subquery2>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "8",
"used_key_parts": ["e", "MAX(t2.g)"],
"ref": ["test.t1.a", "test.t1.c"],
"rows": 1,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
"temporary_table": {
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows": 12,
"filtered": 100,
"attached_condition": "t2.e < 5 and t2.e < 2"
}
}
}
}
}
}
}
# extracted AND formula : pushing into WHERE
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT * FROM t1
WHERE t1.a>2 AND t1.a<5 AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d
4 2 24 4
3 2 23 1
SELECT * FROM t1
WHERE t1.a>2 AND t1.a<5 AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d
4 2 24 4
3 2 23 1
EXPLAIN SELECT * FROM t1
WHERE t1.a>2 AND t1.a<5 AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 16 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 test.t1.a,test.t1.c 1
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 12 Using where; Using temporary
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE t1.a>2 AND t1.a<5 AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100,
"attached_condition": "t1.a > 2 and t1.a < 5 and t1.a is not null and t1.c is not null"
},
"table": {
"table_name": "<subquery2>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "8",
"used_key_parts": ["e", "MAX(t2.g)"],
"ref": ["test.t1.a", "test.t1.c"],
"rows": 1,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
"temporary_table": {
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows": 12,
"filtered": 100,
"attached_condition": "t2.e < 5 and t2.e > 2 and t2.e < 5"
}
}
}
}
}
}
}
# extracted OR formula : pushing into WHERE
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT * FROM t1
WHERE (t1.a<2 OR t1.a>=4) AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d
1 3 40 1
4 2 24 4
1 2 40 2
SELECT * FROM t1
WHERE (t1.a<2 OR t1.a>=4) AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d
1 3 40 1
4 2 24 4
1 2 40 2
EXPLAIN SELECT * FROM t1
WHERE (t1.a<2 OR t1.a>=4) AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 16 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 test.t1.a,test.t1.c 1
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 12 Using where; Using temporary
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE (t1.a<2 OR t1.a>=4) AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100,
"attached_condition": "(t1.a < 2 or t1.a >= 4) and t1.a is not null and t1.c is not null"
},
"table": {
"table_name": "<subquery2>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "8",
"used_key_parts": ["e", "MAX(t2.g)"],
"ref": ["test.t1.a", "test.t1.c"],
"rows": 1,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
"temporary_table": {
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows": 12,
"filtered": 100,
"attached_condition": "t2.e < 5 and (t2.e < 2 or t2.e >= 4)"
}
}
}
}
}
}
}
# extracted AND-OR formula : pushing into WHERE
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT * FROM t1
WHERE ((t1.a<2 OR t1.a=5) AND t1.b>3) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e,t2.f
)
;
a b c d
1 4 35 3
SELECT * FROM t1
WHERE ((t1.a<2 OR t1.a=5) AND t1.b>3) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e,t2.f
)
;
a b c d
1 4 35 3
EXPLAIN SELECT * FROM t1
WHERE ((t1.a<2 OR t1.a=5) AND t1.b>3) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e,t2.f
)
;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 16 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 12 test.t1.a,test.t1.b,test.t1.c 1
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 12 Using where; Using temporary
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE ((t1.a<2 OR t1.a=5) AND t1.b>3) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e,t2.f
)
;
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100,
"attached_condition": "(t1.a < 2 or t1.a = 5) and t1.b > 3 and t1.a is not null and t1.b is not null and t1.c is not null"
},
"table": {
"table_name": "<subquery2>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "12",
"used_key_parts": ["e", "f", "MAX(t2.g)"],
"ref": ["test.t1.a", "test.t1.b", "test.t1.c"],
"rows": 1,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
"temporary_table": {
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows": 12,
"filtered": 100,
"attached_condition": "t2.e < 5 and (t2.e < 2 or t2.e = 5) and t2.f > 3"
}
}
}
}
}
}
}
# extracted AND-OR formula : pushing into WHERE
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT * FROM t1
WHERE ((t1.a<2 OR t1.a=5) AND t1.b>3) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e,t2.f
)
;
a b c d
1 4 35 3
SELECT * FROM t1
WHERE ((t1.a<2 OR t1.a=5) AND t1.b>3) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e,t2.f
)
;
a b c d
1 4 35 3
EXPLAIN SELECT * FROM t1
WHERE ((t1.a<2 OR t1.a=5) AND t1.b>3) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e,t2.f
)
;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 16 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 12 test.t1.a,test.t1.b,test.t1.c 1
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 12 Using where; Using temporary
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE ((t1.a<2 OR t1.a=5) AND t1.b>3) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e,t2.f
)
;
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100,
"attached_condition": "(t1.a < 2 or t1.a = 5) and t1.b > 3 and t1.a is not null and t1.b is not null and t1.c is not null"
},
"table": {
"table_name": "<subquery2>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "12",
"used_key_parts": ["e", "f", "MAX(t2.g)"],
"ref": ["test.t1.a", "test.t1.b", "test.t1.c"],
"rows": 1,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
"temporary_table": {
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows": 12,
"filtered": 100,
"attached_condition": "t2.e < 5 and (t2.e < 2 or t2.e = 5) and t2.f > 3"
}
}
}
}
}
}
}
# conjunctive subformula : pushing into WHERE
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT * FROM t1
WHERE ((t1.b<3 OR t1.d>2) AND t1.a<2) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d
1 2 40 2
SELECT * FROM t1
WHERE ((t1.b<3 OR t1.d>2) AND t1.a<2) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d
1 2 40 2
EXPLAIN SELECT * FROM t1
WHERE ((t1.b<3 OR t1.d>2) AND t1.a<2) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 16 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 12 test.t1.a,test.t1.b,test.t1.c 1
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 12 Using where; Using temporary
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE ((t1.b<3 OR t1.d>2) AND t1.a<2) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100,
"attached_condition": "(t1.b < 3 or t1.d > 2) and t1.a < 2 and t1.a is not null and t1.b is not null and t1.c is not null"
},
"table": {
"table_name": "<subquery2>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "12",
"used_key_parts": ["e", "f", "MAX(t2.g)"],
"ref": ["test.t1.a", "test.t1.b", "test.t1.c"],
"rows": 1,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
"temporary_table": {
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows": 12,
"filtered": 100,
"attached_condition": "t2.e < 5 and t2.e < 2"
}
}
}
}
}
}
}
# using equalities : pushing into WHERE
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT * FROM t1
WHERE t1.d=1 AND t1.a=t1.d AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d
1 3 40 1
SELECT * FROM t1
WHERE t1.d=1 AND t1.a=t1.d AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d
1 3 40 1
EXPLAIN SELECT * FROM t1
WHERE t1.d=1 AND t1.a=t1.d AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 16 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 const,test.t1.c 1
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 12 Using where
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE t1.d=1 AND t1.a=t1.d AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100,
"attached_condition": "t1.a = 1 and t1.d = 1 and t1.c is not null"
},
"table": {
"table_name": "<subquery2>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "8",
"used_key_parts": ["e", "MAX(t2.g)"],
"ref": ["const", "test.t1.c"],
"rows": 1,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows": 12,
"filtered": 100,
"attached_condition": "t2.e = 1"
}
}
}
}
}
}
# using equality : pushing into WHERE
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT * FROM t1
WHERE t1.d>1 AND t1.a=t1.d AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d
4 2 24 4
SELECT * FROM t1
WHERE t1.d>1 AND t1.a=t1.d AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d
4 2 24 4
EXPLAIN SELECT * FROM t1
WHERE t1.d>1 AND t1.a=t1.d AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 16 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 test.t1.a,test.t1.c 1
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 12 Using where; Using temporary
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE t1.d>1 AND t1.a=t1.d AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100,
"attached_condition": "t1.d = t1.a and t1.a > 1 and t1.a is not null and t1.c is not null"
},
"table": {
"table_name": "<subquery2>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "8",
"used_key_parts": ["e", "MAX(t2.g)"],
"ref": ["test.t1.a", "test.t1.c"],
"rows": 1,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
"temporary_table": {
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows": 12,
"filtered": 100,
"attached_condition": "t2.e < 5 and t2.e > 1"
}
}
}
}
}
}
}
# using view IN subquery definition : pushing into WHERE
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT * FROM t1
WHERE t1.a<3 AND
(t1.a,t1.c) IN
(
SELECT v1_x,MAX(v1_y)
FROM v1
WHERE v1_x>1
GROUP BY v1_x
)
;
a b c d
SELECT * FROM t1
WHERE t1.a<3 AND
(t1.a,t1.c) IN
(
SELECT v1_x,MAX(v1_y)
FROM v1
WHERE v1_x>1
GROUP BY v1_x
)
;
a b c d
EXPLAIN SELECT * FROM t1
WHERE t1.a<3 AND
(t1.a,t1.c) IN
(
SELECT v1_x,MAX(v1_y)
FROM v1
WHERE v1_x>1
GROUP BY v1_x
)
;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 16 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 test.t1.a,test.t1.c 1
2 MATERIALIZED t3 ALL NULL NULL NULL NULL 8 Using where; Using temporary
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE t1.a<3 AND
(t1.a,t1.c) IN
(
SELECT v1_x,MAX(v1_y)
FROM v1
WHERE v1_x>1
GROUP BY v1_x
)
;
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100,
"attached_condition": "t1.a < 3 and t1.a is not null and t1.c is not null"
},
"table": {
"table_name": "<subquery2>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "8",
"used_key_parts": ["v1_x", "MAX(v1_y)"],
"ref": ["test.t1.a", "test.t1.c"],
"rows": 1,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
"temporary_table": {
"table": {
"table_name": "t3",
"access_type": "ALL",
"rows": 8,
"filtered": 100,
"attached_condition": "t3.x > 1 and t3.x <= 3 and t3.x < 3"
}
}
}
}
}
}
}
# using equality : pushing into WHERE
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT * FROM t1,v1
WHERE t1.a=v1_x AND v1_x<2 AND v1_y>30 AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d v1_x v1_y
1 3 40 1 1 35
1 2 40 2 1 35
SELECT * FROM t1,v1
WHERE t1.a=v1_x AND v1_x<2 AND v1_y>30 AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d v1_x v1_y
1 3 40 1 1 35
1 2 40 2 1 35
EXPLAIN SELECT * FROM t1,v1
WHERE t1.a=v1_x AND v1_x<2 AND v1_y>30 AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t3 ALL NULL NULL NULL NULL 8 Using where
1 PRIMARY t1 ALL NULL NULL NULL NULL 16 Using where; Using join buffer (flat, BNL join)
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 test.t3.x,test.t1.c 1
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 12 Using where; Using temporary
EXPLAIN FORMAT=JSON SELECT * FROM t1,v1
WHERE t1.a=v1_x AND v1_x<2 AND v1_y>30 AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t3",
"access_type": "ALL",
"rows": 8,
"filtered": 100,
"attached_condition": "t3.x < 2 and t3.y > 30 and t3.x <= 3 and t3.x is not null"
},
"block-nl-join": {
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100
},
"buffer_type": "flat",
"buffer_size": "256Kb",
"join_type": "BNL",
"attached_condition": "t1.a = t3.x and t1.c is not null"
},
"table": {
"table_name": "<subquery2>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "8",
"used_key_parts": ["e", "MAX(t2.g)"],
"ref": ["test.t3.x", "test.t1.c"],
"rows": 1,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
"temporary_table": {
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows": 12,
"filtered": 100,
"attached_condition": "t2.e < 5 and t2.e <= 3"
}
}
}
}
}
}
}
# conjunctive subformula : pushing into WHERE
# extracted OR formula : pushing into HAVING
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT * FROM t1
WHERE ((t1.b<3 OR t1.b=4) AND t1.a<3) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d
1 2 40 2
SELECT * FROM t1
WHERE ((t1.b<3 OR t1.b=4) AND t1.a<3) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d
1 2 40 2
EXPLAIN SELECT * FROM t1
WHERE ((t1.b<3 OR t1.b=4) AND t1.a<3) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 16 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 12 test.t1.a,test.t1.b,test.t1.c 1
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 12 Using where; Using temporary
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE ((t1.b<3 OR t1.b=4) AND t1.a<3) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100,
"attached_condition": "(t1.b < 3 or t1.b = 4) and t1.a < 3 and t1.a is not null and t1.b is not null and t1.c is not null"
},
"table": {
"table_name": "<subquery2>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "12",
"used_key_parts": ["e", "f", "MAX(t2.g)"],
"ref": ["test.t1.a", "test.t1.b", "test.t1.c"],
"rows": 1,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
"having_condition": "t2.f < 3 or t2.f = 4",
"temporary_table": {
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows": 12,
"filtered": 100,
"attached_condition": "t2.e < 5 and t2.e < 3"
}
}
}
}
}
}
}
# conjunctive subformula using addition : pushing into HAVING
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT * FROM t1
WHERE (t1.a+t1.c>41) AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d
2 3 70 3
SELECT * FROM t1
WHERE (t1.a+t1.c>41) AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d
2 3 70 3
EXPLAIN SELECT * FROM t1
WHERE (t1.a+t1.c>41) AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 16 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 test.t1.a,test.t1.c 1
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 12 Using where; Using temporary
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE (t1.a+t1.c>41) AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100,
"attached_condition": "t1.a + t1.c > 41 and t1.a is not null and t1.c is not null"
},
"table": {
"table_name": "<subquery2>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "8",
"used_key_parts": ["e", "MAX(t2.g)"],
"ref": ["test.t1.a", "test.t1.c"],
"rows": 1,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
"having_condition": "t2.e + `MAX(t2.g)` > 41",
"temporary_table": {
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows": 12,
"filtered": 100,
"attached_condition": "t2.e < 5"
}
}
}
}
}
}
}
# conjunctive subformula using substitution : pushing into HAVING
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT * FROM t1
WHERE (t1.c-t1.a<35) AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d
4 2 24 4
3 2 23 1
SELECT * FROM t1
WHERE (t1.c-t1.a<35) AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d
4 2 24 4
3 2 23 1
EXPLAIN SELECT * FROM t1
WHERE (t1.c-t1.a<35) AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 16 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 test.t1.a,test.t1.c 1
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 12 Using where; Using temporary
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE (t1.c-t1.a<35) AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100,
"attached_condition": "t1.c - t1.a < 35 and t1.a is not null and t1.c is not null"
},
"table": {
"table_name": "<subquery2>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "8",
"used_key_parts": ["e", "MAX(t2.g)"],
"ref": ["test.t1.a", "test.t1.c"],
"rows": 1,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
"having_condition": "`MAX(t2.g)` - t2.e < 35",
"temporary_table": {
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows": 12,
"filtered": 100,
"attached_condition": "t2.e < 5"
}
}
}
}
}
}
}
# conjunctive subformula using multiplication : pushing into HAVING
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT * FROM t1
WHERE (t1.c*t1.a>100) AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d
2 3 70 3
SELECT * FROM t1
WHERE (t1.c*t1.a>100) AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d
2 3 70 3
EXPLAIN SELECT * FROM t1
WHERE (t1.c*t1.a>100) AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 16 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 test.t1.a,test.t1.c 1
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 12 Using where; Using temporary
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE (t1.c*t1.a>100) AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100,
"attached_condition": "t1.c * t1.a > 100 and t1.a is not null and t1.c is not null"
},
"table": {
"table_name": "<subquery2>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "8",
"used_key_parts": ["e", "MAX(t2.g)"],
"ref": ["test.t1.a", "test.t1.c"],
"rows": 1,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
"having_condition": "`MAX(t2.g)` * t2.e > 100",
"temporary_table": {
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows": 12,
"filtered": 100,
"attached_condition": "t2.e < 5"
}
}
}
}
}
}
}
# conjunctive subformula using division : pushing into HAVING
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT * FROM t1
WHERE (t1.c/t1.a>30) AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d
1 3 40 1
1 2 40 2
2 3 70 3
SELECT * FROM t1
WHERE (t1.c/t1.a>30) AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d
1 3 40 1
1 2 40 2
2 3 70 3
EXPLAIN SELECT * FROM t1
WHERE (t1.c/t1.a>30) AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 16 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 test.t1.a,test.t1.c 1
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 12 Using where; Using temporary
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE (t1.c/t1.a>30) AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100,
"attached_condition": "t1.c / t1.a > 30 and t1.a is not null and t1.c is not null"
},
"table": {
"table_name": "<subquery2>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "8",
"used_key_parts": ["e", "MAX(t2.g)"],
"ref": ["test.t1.a", "test.t1.c"],
"rows": 1,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
"having_condition": "`MAX(t2.g)` / t2.e > 30",
"temporary_table": {
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows": 12,
"filtered": 100,
"attached_condition": "t2.e < 5"
}
}
}
}
}
}
}
# conjunctive subformula using BETWEEN : pushing into HAVING
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT * FROM t1
WHERE (t1.c BETWEEN 50 AND 100) AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d
2 3 70 3
SELECT * FROM t1
WHERE (t1.c BETWEEN 50 AND 100) AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d
2 3 70 3
EXPLAIN SELECT * FROM t1
WHERE (t1.c BETWEEN 50 AND 100) AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 16 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 test.t1.a,test.t1.c 1
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 12 Using where; Using temporary
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE (t1.c BETWEEN 50 AND 100) AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100,
"attached_condition": "t1.c between 50 and 100 and t1.a is not null and t1.c is not null"
},
"table": {
"table_name": "<subquery2>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "8",
"used_key_parts": ["e", "MAX(t2.g)"],
"ref": ["test.t1.a", "test.t1.c"],
"rows": 1,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
"having_condition": "`MAX(t2.g)` between 50 and 100",
"temporary_table": {
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows": 12,
"filtered": 100,
"attached_condition": "t2.e < 5"
}
}
}
}
}
}
}
# conjunctive subformula using addition : pushing into WHERE
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT * FROM t1
WHERE (t1.a+t1.b > 5) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e,t2.f
)
;
a b c d
4 2 24 4
SELECT * FROM t1
WHERE (t1.a+t1.b > 5) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e,t2.f
)
;
a b c d
4 2 24 4
EXPLAIN SELECT * FROM t1
WHERE (t1.a+t1.b > 5) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e,t2.f
)
;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 16 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 12 test.t1.a,test.t1.b,test.t1.c 1
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 12 Using where; Using temporary
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE (t1.a+t1.b > 5) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e,t2.f
)
;
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100,
"attached_condition": "t1.a + t1.b > 5 and t1.a is not null and t1.b is not null and t1.c is not null"
},
"table": {
"table_name": "<subquery2>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "12",
"used_key_parts": ["e", "f", "MAX(t2.g)"],
"ref": ["test.t1.a", "test.t1.b", "test.t1.c"],
"rows": 1,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
"temporary_table": {
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows": 12,
"filtered": 100,
"attached_condition": "t2.e < 5 and t2.e + t2.f > 5"
}
}
}
}
}
}
}
# conjunctive subformula using substitution : pushing into WHERE
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT * FROM t1
WHERE (t1.a-t1.b > 0) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e,t2.f
)
;
a b c d
4 2 24 4
SELECT * FROM t1
WHERE (t1.a-t1.b > 0) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e,t2.f
)
;
a b c d
4 2 24 4
EXPLAIN SELECT * FROM t1
WHERE (t1.a-t1.b > 0) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e,t2.f
)
;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 16 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 12 test.t1.a,test.t1.b,test.t1.c 1
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 12 Using where; Using temporary
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE (t1.a-t1.b > 0) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e,t2.f
)
;
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100,
"attached_condition": "t1.a - t1.b > 0 and t1.a is not null and t1.b is not null and t1.c is not null"
},
"table": {
"table_name": "<subquery2>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "12",
"used_key_parts": ["e", "f", "MAX(t2.g)"],
"ref": ["test.t1.a", "test.t1.b", "test.t1.c"],
"rows": 1,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
"temporary_table": {
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows": 12,
"filtered": 100,
"attached_condition": "t2.e < 5 and t2.e - t2.f > 0"
}
}
}
}
}
}
}
# conjunctive subformula using multiplication : pushing into WHERE
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT * FROM t1
WHERE (t1.a*t1.b > 6) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e,t2.f
)
;
a b c d
4 2 24 4
SELECT * FROM t1
WHERE (t1.a*t1.b > 6) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e,t2.f
)
;
a b c d
4 2 24 4
EXPLAIN SELECT * FROM t1
WHERE (t1.a*t1.b > 6) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e,t2.f
)
;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 16 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 12 test.t1.a,test.t1.b,test.t1.c 1
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 12 Using where; Using temporary
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE (t1.a*t1.b > 6) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e,t2.f
)
;
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100,
"attached_condition": "t1.a * t1.b > 6 and t1.a is not null and t1.b is not null and t1.c is not null"
},
"table": {
"table_name": "<subquery2>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "12",
"used_key_parts": ["e", "f", "MAX(t2.g)"],
"ref": ["test.t1.a", "test.t1.b", "test.t1.c"],
"rows": 1,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
"temporary_table": {
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows": 12,
"filtered": 100,
"attached_condition": "t2.e < 5 and t2.e * t2.f > 6"
}
}
}
}
}
}
}
# conjunctive subformula using division : pushing into WHERE
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT * FROM t1
WHERE (t1.b/t1.a > 2) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e,t2.f
)
;
a b c d
1 3 40 1
1 4 35 3
SELECT * FROM t1
WHERE (t1.b/t1.a > 2) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e,t2.f
)
;
a b c d
1 3 40 1
1 4 35 3
EXPLAIN SELECT * FROM t1
WHERE (t1.b/t1.a > 2) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e,t2.f
)
;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 16 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 12 test.t1.a,test.t1.b,test.t1.c 1
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 12 Using where; Using temporary
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE (t1.b/t1.a > 2) AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e,t2.f
)
;
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100,
"attached_condition": "t1.b / t1.a > 2 and t1.a is not null and t1.b is not null and t1.c is not null"
},
"table": {
"table_name": "<subquery2>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "12",
"used_key_parts": ["e", "f", "MAX(t2.g)"],
"ref": ["test.t1.a", "test.t1.b", "test.t1.c"],
"rows": 1,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
"temporary_table": {
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows": 12,
"filtered": 100,
"attached_condition": "t2.e < 5 and t2.f / t2.e > 2"
}
}
}
}
}
}
}
# conjunctive subformula using BETWEEN : pushing into WHERE
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT * FROM t1
WHERE (t1.a BETWEEN 1 AND 3) AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d
1 3 40 1
3 2 23 1
1 2 40 2
2 3 70 3
SELECT * FROM t1
WHERE (t1.a BETWEEN 1 AND 3) AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
a b c d
1 3 40 1
3 2 23 1
1 2 40 2
2 3 70 3
EXPLAIN SELECT * FROM t1
WHERE (t1.a BETWEEN 1 AND 3) AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 16 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 test.t1.a,test.t1.c 1
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 12 Using where; Using temporary
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE (t1.a BETWEEN 1 AND 3) AND
(t1.a,t1.c) IN
(
SELECT t2.e,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
;
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100,
"attached_condition": "t1.a between 1 and 3 and t1.a is not null and t1.c is not null"
},
"table": {
"table_name": "<subquery2>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "8",
"used_key_parts": ["e", "MAX(t2.g)"],
"ref": ["test.t1.a", "test.t1.c"],
"rows": 1,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
"temporary_table": {
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows": 12,
"filtered": 100,
"attached_condition": "t2.e < 5 and t2.e between 1 and 3"
}
}
}
}
}
}
}
# conjunctive subformula : pushing into HAVING of the IN subquery
# conjunctive subformula : pushing into WHERE of the view from the IN subquery
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT * FROM t1
WHERE t1.c>3 AND
(t1.a,t1.b,t1.c) IN
(
SELECT v2.e,MAX(v2.f),v2.max_g
FROM v2
WHERE v2.e<5
GROUP BY v2.e
)
;
a b c d
1 2 40 2
2 3 70 3
SELECT * FROM t1
WHERE t1.c>3 AND
(t1.a,t1.b,t1.c) IN
(
SELECT v2.e,MAX(v2.f),v2.max_g
FROM v2
WHERE v2.e<5
GROUP BY v2.e
)
;
a b c d
1 2 40 2
2 3 70 3
EXPLAIN SELECT * FROM t1
WHERE t1.c>3 AND
(t1.a,t1.b,t1.c) IN
(
SELECT v2.e,MAX(v2.f),v2.max_g
FROM v2
WHERE v2.e<5
GROUP BY v2.e
)
;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 16 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 12 test.t1.a,test.t1.b,test.t1.c 1
2 MATERIALIZED <derived3> ALL NULL NULL NULL NULL 12 Using where; Using temporary
3 DERIVED t2 ALL NULL NULL NULL NULL 12 Using temporary; Using filesort
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE t1.c>3 AND
(t1.a,t1.b,t1.c) IN
(
SELECT v2.e,MAX(v2.f),v2.max_g
FROM v2
WHERE v2.e<5
GROUP BY v2.e
)
;
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100,
"attached_condition": "t1.c > 3 and t1.a is not null and t1.b is not null and t1.c is not null"
},
"table": {
"table_name": "<subquery2>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "12",
"used_key_parts": ["e", "MAX(v2.f)", "max_g"],
"ref": ["test.t1.a", "test.t1.b", "test.t1.c"],
"rows": 1,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
"having_condition": "v2.max_g > 3",
"temporary_table": {
"table": {
"table_name": "<derived3>",
"access_type": "ALL",
"rows": 12,
"filtered": 100,
"attached_condition": "v2.e < 5",
"materialized": {
"query_block": {
"select_id": 3,
"having_condition": "max_g > 25",
"filesort": {
"sort_key": "t2.e",
"temporary_table": {
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows": 12,
"filtered": 100
}
}
}
}
}
}
}
}
}
}
}
}
# conjunctive subformula : pushing into WHERE of the IN subquery
# conjunctive subformula : pushing into WHERE of the view
# from the IN subquery
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT * FROM t1
WHERE t1.a>1 AND
(t1.a,t1.b,t1.c) IN
(
SELECT v2.e,MAX(v2.f),v2.max_g
FROM v2
WHERE v2.e<5
GROUP BY v2.e
)
;
a b c d
2 3 70 3
SELECT * FROM t1
WHERE t1.a>1 AND
(t1.a,t1.b,t1.c) IN
(
SELECT v2.e,MAX(v2.f),v2.max_g
FROM v2
WHERE v2.e<5
GROUP BY v2.e
)
;
a b c d
2 3 70 3
EXPLAIN SELECT * FROM t1
WHERE t1.a>1 AND
(t1.a,t1.b,t1.c) IN
(
SELECT v2.e,MAX(v2.f),v2.max_g
FROM v2
WHERE v2.e<5
GROUP BY v2.e
)
;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 16 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 12 test.t1.a,test.t1.b,test.t1.c 1
2 MATERIALIZED <derived3> ALL NULL NULL NULL NULL 12 Using where; Using temporary
3 DERIVED t2 ALL NULL NULL NULL NULL 12 Using temporary; Using filesort
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE t1.a>1 AND
(t1.a,t1.b,t1.c) IN
(
SELECT v2.e,MAX(v2.f),v2.max_g
FROM v2
WHERE v2.e<5
GROUP BY v2.e
)
;
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100,
"attached_condition": "t1.a > 1 and t1.a is not null and t1.b is not null and t1.c is not null"
},
"table": {
"table_name": "<subquery2>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "12",
"used_key_parts": ["e", "MAX(v2.f)", "max_g"],
"ref": ["test.t1.a", "test.t1.b", "test.t1.c"],
"rows": 1,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
"temporary_table": {
"table": {
"table_name": "<derived3>",
"access_type": "ALL",
"rows": 12,
"filtered": 100,
"attached_condition": "v2.e < 5 and v2.e > 1",
"materialized": {
"query_block": {
"select_id": 3,
"having_condition": "max_g > 25",
"filesort": {
"sort_key": "t2.e",
"temporary_table": {
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows": 12,
"filtered": 100
}
}
}
}
}
}
}
}
}
}
}
}
# conjunctive subformula : pushing into WHERE and HAVING
# of the IN subquery
# conjunctive subformula : pushing into WHERE of the view
# from the IN subquery
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT * FROM t1
WHERE t1.a>1 AND t1.c<100 AND
(t1.a,t1.b,t1.c) IN
(
SELECT v2.e,MAX(v2.f),v2.max_g
FROM v2
WHERE v2.e<5
GROUP BY v2.e
)
;
a b c d
2 3 70 3
SELECT * FROM t1
WHERE t1.a>1 AND t1.c<100 AND
(t1.a,t1.b,t1.c) IN
(
SELECT v2.e,MAX(v2.f),v2.max_g
FROM v2
WHERE v2.e<5
GROUP BY v2.e
)
;
a b c d
2 3 70 3
EXPLAIN SELECT * FROM t1
WHERE t1.a>1 AND t1.c<100 AND
(t1.a,t1.b,t1.c) IN
(
SELECT v2.e,MAX(v2.f),v2.max_g
FROM v2
WHERE v2.e<5
GROUP BY v2.e
)
;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 16 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 12 test.t1.a,test.t1.b,test.t1.c 1
2 MATERIALIZED <derived3> ALL NULL NULL NULL NULL 12 Using where; Using temporary
3 DERIVED t2 ALL NULL NULL NULL NULL 12 Using temporary; Using filesort
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE t1.a>1 AND t1.c<100 AND
(t1.a,t1.b,t1.c) IN
(
SELECT v2.e,MAX(v2.f),v2.max_g
FROM v2
WHERE v2.e<5
GROUP BY v2.e
)
;
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100,
"attached_condition": "t1.a > 1 and t1.c < 100 and t1.a is not null and t1.b is not null and t1.c is not null"
},
"table": {
"table_name": "<subquery2>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "12",
"used_key_parts": ["e", "MAX(v2.f)", "max_g"],
"ref": ["test.t1.a", "test.t1.b", "test.t1.c"],
"rows": 1,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
"having_condition": "v2.max_g < 100",
"temporary_table": {
"table": {
"table_name": "<derived3>",
"access_type": "ALL",
"rows": 12,
"filtered": 100,
"attached_condition": "v2.e < 5 and v2.e > 1",
"materialized": {
"query_block": {
"select_id": 3,
"having_condition": "max_g > 25",
"filesort": {
"sort_key": "t2.e",
"temporary_table": {
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows": 12,
"filtered": 100
}
}
}
}
}
}
}
}
}
}
}
}
# conjunctive subformula : pushing into WHERE of the IN subquery
# extracted AND formula : pushing into HAVING of the derived table
# from the IN subquery
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT * FROM t1
WHERE t1.a>1 AND
(t1.a,t1.b,t1.c) IN
(
SELECT d_tab.e,MAX(d_tab.f),d_tab.max_g
FROM
(
SELECT t2.e, t2.f, MAX(t2.g) AS max_g
FROM t2
GROUP BY t2.f
HAVING max_g>25
) as d_tab
WHERE d_tab.e<5
GROUP BY d_tab.e
)
;
a b c d
2 3 40 4
SELECT * FROM t1
WHERE t1.a>1 AND
(t1.a,t1.b,t1.c) IN
(
SELECT d_tab.e,MAX(d_tab.f),d_tab.max_g
FROM
(
SELECT t2.e, t2.f, MAX(t2.g) AS max_g
FROM t2
GROUP BY t2.f
HAVING max_g>25
) as d_tab
WHERE d_tab.e<5
GROUP BY d_tab.e
)
;
a b c d
2 3 40 4
EXPLAIN SELECT * FROM t1
WHERE t1.a>1 AND
(t1.a,t1.b,t1.c) IN
(
SELECT d_tab.e,MAX(d_tab.f),d_tab.max_g
FROM
(
SELECT t2.e, t2.f, MAX(t2.g) AS max_g
FROM t2
GROUP BY t2.f
HAVING max_g>25
) as d_tab
WHERE d_tab.e<5
GROUP BY d_tab.e
)
;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 16 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 12 test.t1.a,test.t1.b,test.t1.c 1
2 MATERIALIZED <derived3> ALL NULL NULL NULL NULL 12 Using where; Using temporary
3 DERIVED t2 ALL NULL NULL NULL NULL 12 Using temporary; Using filesort
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE t1.a>1 AND
(t1.a,t1.b,t1.c) IN
(
SELECT d_tab.e,MAX(d_tab.f),d_tab.max_g
FROM
(
SELECT t2.e, t2.f, MAX(t2.g) AS max_g
FROM t2
GROUP BY t2.f
HAVING max_g>25
) as d_tab
WHERE d_tab.e<5
GROUP BY d_tab.e
)
;
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100,
"attached_condition": "t1.a > 1 and t1.a is not null and t1.b is not null and t1.c is not null"
},
"table": {
"table_name": "<subquery2>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "12",
"used_key_parts": ["e", "MAX(d_tab.f)", "max_g"],
"ref": ["test.t1.a", "test.t1.b", "test.t1.c"],
"rows": 1,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
"temporary_table": {
"table": {
"table_name": "<derived3>",
"access_type": "ALL",
"rows": 12,
"filtered": 100,
"attached_condition": "d_tab.e < 5 and d_tab.e > 1",
"materialized": {
"query_block": {
"select_id": 3,
"having_condition": "max_g > 25",
"filesort": {
"sort_key": "t2.f",
"temporary_table": {
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows": 12,
"filtered": 100
}
}
}
}
}
}
}
}
}
}
}
}
# conjunctive subformula : pushing into HAVING of the derived table
# conjunctive subformula : pushing into WHERE of the IN subquery from
# the derived table
SELECT *
FROM t3,
(
SELECT t1.a,t1.b,max(t1.c) as max_c
FROM t1
WHERE t1.a>1 AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.e<5
GROUP BY t2.e
)
GROUP BY t1.a
) AS d_tab
WHERE d_tab.a=t3.x AND d_tab.b>2;
x y a b max_c
2 15 2 3 70
2 15 2 3 70
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT * FROM t1
WHERE t1.a>1 AND
(t1.a,t1.b,t1.c) IN
(
SELECT d_tab.e,MAX(d_tab.f),d_tab.max_g
FROM
(
SELECT t2.e, t2.f, MAX(t2.g) AS max_g
FROM t2
GROUP BY t2.f
HAVING max_g>25
) as d_tab
WHERE d_tab.e<5
GROUP BY d_tab.e
)
;
a b c d
2 3 40 4
SELECT * FROM t1
WHERE t1.a>1 AND
(t1.a,t1.b,t1.c) IN
(
SELECT d_tab.e,MAX(d_tab.f),d_tab.max_g
FROM
(
SELECT t2.e, t2.f, MAX(t2.g) AS max_g
FROM t2
GROUP BY t2.f
HAVING max_g>25
) as d_tab
WHERE d_tab.e<5
GROUP BY d_tab.e
)
;
a b c d
2 3 40 4
EXPLAIN SELECT * FROM t1
WHERE t1.a>1 AND
(t1.a,t1.b,t1.c) IN
(
SELECT d_tab.e,MAX(d_tab.f),d_tab.max_g
FROM
(
SELECT t2.e, t2.f, MAX(t2.g) AS max_g
FROM t2
GROUP BY t2.f
HAVING max_g>25
) as d_tab
WHERE d_tab.e<5
GROUP BY d_tab.e
)
;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 16 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 12 test.t1.a,test.t1.b,test.t1.c 1
2 MATERIALIZED <derived3> ALL NULL NULL NULL NULL 12 Using where; Using temporary
3 DERIVED t2 ALL NULL NULL NULL NULL 12 Using temporary; Using filesort
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE t1.a>1 AND
(t1.a,t1.b,t1.c) IN
(
SELECT d_tab.e,MAX(d_tab.f),d_tab.max_g
FROM
(
SELECT t2.e, t2.f, MAX(t2.g) AS max_g
FROM t2
GROUP BY t2.f
HAVING max_g>25
) as d_tab
WHERE d_tab.e<5
GROUP BY d_tab.e
)
;
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100,
"attached_condition": "t1.a > 1 and t1.a is not null and t1.b is not null and t1.c is not null"
},
"table": {
"table_name": "<subquery2>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "12",
"used_key_parts": ["e", "MAX(d_tab.f)", "max_g"],
"ref": ["test.t1.a", "test.t1.b", "test.t1.c"],
"rows": 1,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
"temporary_table": {
"table": {
"table_name": "<derived3>",
"access_type": "ALL",
"rows": 12,
"filtered": 100,
"attached_condition": "d_tab.e < 5 and d_tab.e > 1",
"materialized": {
"query_block": {
"select_id": 3,
"having_condition": "max_g > 25",
"filesort": {
"sort_key": "t2.f",
"temporary_table": {
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows": 12,
"filtered": 100
}
}
}
}
}
}
}
}
}
}
}
}
# conjunctive subformula : pushing into WHERE of the derived table
# extracted AND formula : pushing into WHERE of the IN subquery from
# the derived table
SELECT *
FROM t3,
(
SELECT t1.a,t1.b,max(t1.c) as max_c
FROM t1
WHERE t1.a>1 AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
GROUP BY t2.e
HAVING t2.f<5
)
GROUP BY t1.a
) AS d_tab
WHERE d_tab.a=t3.x AND d_tab.a<5;
x y a b max_c
2 15 2 3 70
4 24 4 2 24
2 15 2 3 70
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT * FROM t1
WHERE t1.a>1 AND
(t1.a,t1.b,t1.c) IN
(
SELECT d_tab.e,MAX(d_tab.f),d_tab.max_g
FROM
(
SELECT t2.e, t2.f, MAX(t2.g) AS max_g
FROM t2
GROUP BY t2.f
HAVING max_g>25
) as d_tab
WHERE d_tab.e<5
GROUP BY d_tab.e
)
;
a b c d
2 3 40 4
SELECT * FROM t1
WHERE t1.a>1 AND
(t1.a,t1.b,t1.c) IN
(
SELECT d_tab.e,MAX(d_tab.f),d_tab.max_g
FROM
(
SELECT t2.e, t2.f, MAX(t2.g) AS max_g
FROM t2
GROUP BY t2.f
HAVING max_g>25
) as d_tab
WHERE d_tab.e<5
GROUP BY d_tab.e
)
;
a b c d
2 3 40 4
EXPLAIN SELECT * FROM t1
WHERE t1.a>1 AND
(t1.a,t1.b,t1.c) IN
(
SELECT d_tab.e,MAX(d_tab.f),d_tab.max_g
FROM
(
SELECT t2.e, t2.f, MAX(t2.g) AS max_g
FROM t2
GROUP BY t2.f
HAVING max_g>25
) as d_tab
WHERE d_tab.e<5
GROUP BY d_tab.e
)
;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 16 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 12 test.t1.a,test.t1.b,test.t1.c 1
2 MATERIALIZED <derived3> ALL NULL NULL NULL NULL 12 Using where; Using temporary
3 DERIVED t2 ALL NULL NULL NULL NULL 12 Using temporary; Using filesort
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE t1.a>1 AND
(t1.a,t1.b,t1.c) IN
(
SELECT d_tab.e,MAX(d_tab.f),d_tab.max_g
FROM
(
SELECT t2.e, t2.f, MAX(t2.g) AS max_g
FROM t2
GROUP BY t2.f
HAVING max_g>25
) as d_tab
WHERE d_tab.e<5
GROUP BY d_tab.e
)
;
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100,
"attached_condition": "t1.a > 1 and t1.a is not null and t1.b is not null and t1.c is not null"
},
"table": {
"table_name": "<subquery2>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "12",
"used_key_parts": ["e", "MAX(d_tab.f)", "max_g"],
"ref": ["test.t1.a", "test.t1.b", "test.t1.c"],
"rows": 1,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
"temporary_table": {
"table": {
"table_name": "<derived3>",
"access_type": "ALL",
"rows": 12,
"filtered": 100,
"attached_condition": "d_tab.e < 5 and d_tab.e > 1",
"materialized": {
"query_block": {
"select_id": 3,
"having_condition": "max_g > 25",
"filesort": {
"sort_key": "t2.f",
"temporary_table": {
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows": 12,
"filtered": 100
}
}
}
}
}
}
}
}
}
}
}
}
# conjunctive subformula : pushing into WHERE and HAVING
# of the derived table
# extracted AND formula : pushing into WHERE of the IN subquery
# from the derived table
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT *
FROM t3,
(
SELECT t1.a,t1.b,max(t1.c) as max_c
FROM t1
WHERE t1.a>1 AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
GROUP BY t2.e
HAVING t2.f<5
)
GROUP BY t1.a
) AS d_tab
WHERE d_tab.a=t3.x AND d_tab.a<5 AND d_tab.max_c<70;
x y a b max_c
4 24 4 2 24
SELECT *
FROM t3,
(
SELECT t1.a,t1.b,max(t1.c) as max_c
FROM t1
WHERE t1.a>1 AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
GROUP BY t2.e
HAVING t2.f<5
)
GROUP BY t1.a
) AS d_tab
WHERE d_tab.a=t3.x AND d_tab.a<5 AND d_tab.max_c<70;
x y a b max_c
4 24 4 2 24
EXPLAIN SELECT *
FROM t3,
(
SELECT t1.a,t1.b,max(t1.c) as max_c
FROM t1
WHERE t1.a>1 AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
GROUP BY t2.e
HAVING t2.f<5
)
GROUP BY t1.a
) AS d_tab
WHERE d_tab.a=t3.x AND d_tab.a<5 AND d_tab.max_c<70;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t3 ALL NULL NULL NULL NULL 8 Using where
1 PRIMARY <derived2> ref key0 key0 5 test.t3.x 2 Using where
2 DERIVED t1 ALL NULL NULL NULL NULL 16 Using where; Using temporary; Using filesort
2 DERIVED <subquery3> eq_ref distinct_key distinct_key 12 test.t1.a,test.t1.b,test.t1.c 1
3 MATERIALIZED t2 ALL NULL NULL NULL NULL 12 Using where; Using temporary
EXPLAIN FORMAT=JSON SELECT *
FROM t3,
(
SELECT t1.a,t1.b,max(t1.c) as max_c
FROM t1
WHERE t1.a>1 AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
GROUP BY t2.e
HAVING t2.f<5
)
GROUP BY t1.a
) AS d_tab
WHERE d_tab.a=t3.x AND d_tab.a<5 AND d_tab.max_c<70;
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t3",
"access_type": "ALL",
"rows": 8,
"filtered": 100,
"attached_condition": "t3.x < 5 and t3.x is not null"
},
"table": {
"table_name": "<derived2>",
"access_type": "ref",
"possible_keys": ["key0"],
"key": "key0",
"key_length": "5",
"used_key_parts": ["a"],
"ref": ["test.t3.x"],
"rows": 2,
"filtered": 100,
"attached_condition": "d_tab.max_c < 70",
"materialized": {
"query_block": {
"select_id": 2,
"having_condition": "max_c < 70",
"filesort": {
"sort_key": "t1.a",
"temporary_table": {
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100,
"attached_condition": "t1.a > 1 and t1.a < 5 and t1.a is not null and t1.b is not null and t1.c is not null"
},
"table": {
"table_name": "<subquery3>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "12",
"used_key_parts": ["e", "f", "MAX(t2.g)"],
"ref": ["test.t1.a", "test.t1.b", "test.t1.c"],
"rows": 1,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 3,
"having_condition": "t2.f < 5",
"temporary_table": {
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows": 12,
"filtered": 100,
"attached_condition": "t2.e > 1 and t2.e < 5"
}
}
}
}
}
}
}
}
}
}
}
}
# conjunctive subformula : pushing into WHERE of the derived table
# conjunctive subformula : pushing into HAVING of the IN subquery from
# the derived table
SELECT *
FROM t3,
(
SELECT t1.a,t1.b,max(t1.c) as max_c
FROM t1
WHERE (t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
WHERE t2.f<4
GROUP BY t2.f
)
GROUP BY t1.a
HAVING t1.b<5
) AS d_tab
WHERE d_tab.a=t3.x AND d_tab.a<5;
x y a b max_c
1 25 1 2 70
1 18 1 2 70
2 15 2 3 40
1 35 1 2 70
2 15 2 3 40
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT *
FROM t3,
(
SELECT t1.a,t1.b,max(t1.c) as max_c
FROM t1
WHERE t1.a>1 AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
GROUP BY t2.e
HAVING t2.f<5
)
GROUP BY t1.a
) AS d_tab
WHERE d_tab.a=t3.x AND d_tab.a<5 AND d_tab.max_c<70;
x y a b max_c
4 24 4 2 24
SELECT *
FROM t3,
(
SELECT t1.a,t1.b,max(t1.c) as max_c
FROM t1
WHERE t1.a>1 AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
GROUP BY t2.e
HAVING t2.f<5
)
GROUP BY t1.a
) AS d_tab
WHERE d_tab.a=t3.x AND d_tab.a<5 AND d_tab.max_c<70;
x y a b max_c
4 24 4 2 24
EXPLAIN SELECT *
FROM t3,
(
SELECT t1.a,t1.b,max(t1.c) as max_c
FROM t1
WHERE t1.a>1 AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
GROUP BY t2.e
HAVING t2.f<5
)
GROUP BY t1.a
) AS d_tab
WHERE d_tab.a=t3.x AND d_tab.a<5 AND d_tab.max_c<70;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t3 ALL NULL NULL NULL NULL 8 Using where
1 PRIMARY <derived2> ref key0 key0 5 test.t3.x 2 Using where
2 DERIVED t1 ALL NULL NULL NULL NULL 16 Using where; Using temporary; Using filesort
2 DERIVED <subquery3> eq_ref distinct_key distinct_key 12 test.t1.a,test.t1.b,test.t1.c 1
3 MATERIALIZED t2 ALL NULL NULL NULL NULL 12 Using where; Using temporary
EXPLAIN FORMAT=JSON SELECT *
FROM t3,
(
SELECT t1.a,t1.b,max(t1.c) as max_c
FROM t1
WHERE t1.a>1 AND
(t1.a,t1.b,t1.c) IN
(
SELECT t2.e,t2.f,MAX(t2.g)
FROM t2
GROUP BY t2.e
HAVING t2.f<5
)
GROUP BY t1.a
) AS d_tab
WHERE d_tab.a=t3.x AND d_tab.a<5 AND d_tab.max_c<70;
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t3",
"access_type": "ALL",
"rows": 8,
"filtered": 100,
"attached_condition": "t3.x < 5 and t3.x is not null"
},
"table": {
"table_name": "<derived2>",
"access_type": "ref",
"possible_keys": ["key0"],
"key": "key0",
"key_length": "5",
"used_key_parts": ["a"],
"ref": ["test.t3.x"],
"rows": 2,
"filtered": 100,
"attached_condition": "d_tab.max_c < 70",
"materialized": {
"query_block": {
"select_id": 2,
"having_condition": "max_c < 70",
"filesort": {
"sort_key": "t1.a",
"temporary_table": {
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100,
"attached_condition": "t1.a > 1 and t1.a < 5 and t1.a is not null and t1.b is not null and t1.c is not null"
},
"table": {
"table_name": "<subquery3>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "12",
"used_key_parts": ["e", "f", "MAX(t2.g)"],
"ref": ["test.t1.a", "test.t1.b", "test.t1.c"],
"rows": 1,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 3,
"having_condition": "t2.f < 5",
"temporary_table": {
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows": 12,
"filtered": 100,
"attached_condition": "t2.e > 1 and t2.e < 5"
}
}
}
}
}
}
}
}
}
}
}
}
# conjunctive subformula : pushing into WHERE
# using WINDOW FUNCTIONS : using MAX function
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT * FROM t1
WHERE (t1.b>1) AND
(t1.b, t1.c) IN
(
SELECT t2.f, MAX(t2.g) OVER (PARTITION BY t2.f)
FROM t2
WHERE t2.e<5
)
;
a b c d
1 3 40 1
2 3 40 4
1 4 35 3
1 2 70 5
SELECT * FROM t1
WHERE (t1.b>1) AND
(t1.b, t1.c) IN
(
SELECT t2.f, MAX(t2.g) OVER (PARTITION BY t2.f)
FROM t2
WHERE t2.e<5
)
;
a b c d
1 3 40 1
2 3 40 4
1 4 35 3
1 2 70 5
EXPLAIN SELECT * FROM t1
WHERE (t1.b>1) AND
(t1.b, t1.c) IN
(
SELECT t2.f, MAX(t2.g) OVER (PARTITION BY t2.f)
FROM t2
WHERE t2.e<5
)
;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 16 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 test.t1.b,test.t1.c 1
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 12 Using where; Using temporary
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE (t1.b>1) AND
(t1.b, t1.c) IN
(
SELECT t2.f, MAX(t2.g) OVER (PARTITION BY t2.f)
FROM t2
WHERE t2.e<5
)
;
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100,
"attached_condition": "t1.b > 1 and t1.b is not null and t1.c is not null"
},
"table": {
"table_name": "<subquery2>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "8",
"used_key_parts": ["f", "MAX(t2.g) OVER (PARTITION BY t2.f)"],
"ref": ["test.t1.b", "test.t1.c"],
"rows": 1,
"filtered": 100,
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
"window_functions_computation": {
"sorts": {
"filesort": {
"sort_key": "t2.f"
}
},
"temporary_table": {
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows": 12,
"filtered": 100,
"attached_condition": "t2.e < 5 and t2.f > 1"
}
}
}
}
}
}
}
}
# conjunctive subformula : pushing into WHERE
# using WINDOW FUNCTIONS : using SUM function
SET STATEMENT optimizer_switch='condition_pushdown_for_subquery=off' FOR SELECT * FROM t1
WHERE (t1.b>1) AND
(t1.b, t1.c) IN
(
SELECT t2.f, CAST(SUM(t2.g) OVER (PARTITION BY t2.f) AS INT)
FROM t2
WHERE t2.e<5
)
;
a b c d
5 3 72 4
SELECT * FROM t1
WHERE (t1.b>1) AND
(t1.b, t1.c) IN
(
SELECT t2.f, CAST(SUM(t2.g) OVER (PARTITION BY t2.f) AS INT)
FROM t2
WHERE t2.e<5
)
;
a b c d
5 3 72 4
EXPLAIN SELECT * FROM t1
WHERE (t1.b>1) AND
(t1.b, t1.c) IN
(
SELECT t2.f, CAST(SUM(t2.g) OVER (PARTITION BY t2.f) AS INT)
FROM t2
WHERE t2.e<5
)
;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 16 Using where
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 12 test.t1.b,test.t1.c 1 Using where
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 12 Using where; Using temporary
EXPLAIN FORMAT=JSON SELECT * FROM t1
WHERE (t1.b>1) AND
(t1.b, t1.c) IN
(
SELECT t2.f, CAST(SUM(t2.g) OVER (PARTITION BY t2.f) AS INT)
FROM t2
WHERE t2.e<5
)
;
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 16,
"filtered": 100,
"attached_condition": "t1.b > 1 and t1.b is not null and t1.c is not null"
},
"table": {
"table_name": "<subquery2>",
"access_type": "eq_ref",
"possible_keys": ["distinct_key"],
"key": "distinct_key",
"key_length": "12",
"used_key_parts": ["f", "CAST(SUM(t2.g) OVER (PARTITION BY t2.f) AS INT)"],
"ref": ["test.t1.b", "test.t1.c"],
"rows": 1,
"filtered": 100,
"attached_condition": "t1.c = `<subquery2>`.`CAST(SUM(t2.g) OVER (PARTITION BY t2.f) AS INT)`",
"materialized": {
"unique": 1,
"query_block": {
"select_id": 2,
"window_functions_computation": {
"sorts": {
"filesort": {
"sort_key": "t2.f"
}
},
"temporary_table": {
"table": {
"table_name": "t2",
"access_type": "ALL",
"rows": 12,
"filtered": 100,
"attached_condition": "t2.e < 5 and t2.f > 1"
}
}
}
}
}
}
}
}
DROP TABLE t1,t2,t3;
DROP VIEW v1,v2;