Bug #121915 False HAVING conjunct fails to skip Cartesian aggregate
Description:
With the hypergraph optimizer enabled, a false execution-constant scalar-subquery conjunct in HAVING does not prevent evaluation of a separate aggregate-dependent conjunct. MySQL reads and aggregates the entire Cartesian join even though the scalar subquery makes the HAVING condition false.
The two queries below both returned an empty result. With two 10,000-row input tables, the literal-FALSE baseline took 11.8, 11.5, and 11.2 ms. The equivalent query containing the scalar-subquery conjunct took 2978.4, 2899.4, and 3119.5 ms: 251.6x, 251.6x, and 277.4x slower.
EXPLAIN FORMAT=TREE for the slow query included:
-> Filter: ((count(0) >= 0) and <cache>((0 = (select #2)))) (cost=19.7e+6..19.7e+6 rows=1)
-> Aggregate: count(0), count(0) (cost=19.7e+6..19.7e+6 rows=1)
-> Inner hash join (no condition) (cost=5806..2.01e+6 rows=88e+6)
-> Covering index scan on l using PRIMARY (cost=0.249..2190 rows=8802)
-> Hash
-> Covering index scan on r using PRIMARY (cost=0.249..2488 rows=10000)
-> Select #2 (subquery in condition; run only once)
-> Aggregate: count(0) (cost=0.645..0.645 rows=1)
-> Covering index scan on t3 using PRIMARY (cost=0.249..0.249 rows=1)
Because t3 contains one row, SELECT COUNT(*) FROM t3 returns 1 and 0 = 1 is false. The Cartesian aggregate therefore cannot affect the empty result.
The related issues are #35368475, which also concerns constant-false FilterIterator behavior, but does not establish this HAVING conjunction case where an aggregate-dependent leaf suppresses early evaluation of an independent false leaf; and #111671, which concerns a general query-performance regression and was closed as Not a Bug, but does not cover this reproducible HAVING/scalar-subquery conjunction.
How to repeat:
Run as root in the mysql client:
CREATE DATABASE db1;
USE db1;
CREATE TABLE t1 (k INT NOT NULL PRIMARY KEY) ENGINE=InnoDB;
CREATE TABLE t2 (k INT NOT NULL PRIMARY KEY) ENGINE=InnoDB;
CREATE TABLE t3 (k INT NOT NULL PRIMARY KEY) ENGINE=InnoDB;
INSERT INTO t1 (k)
WITH RECURSIVE
digits AS (
SELECT 0 AS d UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3
UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7
UNION ALL SELECT 8 UNION ALL SELECT 9
)
SELECT a.d + 10*b.d + 100*c.d + 1000*d.d + 1
FROM digits AS a
CROSS JOIN digits AS b
CROSS JOIN digits AS c
CROSS JOIN digits AS d;
INSERT INTO t2 SELECT * FROM t1;
INSERT INTO t3 VALUES (1);
ANALYZE TABLE t1, t2, t3;
SELECT /*+ SET_VAR(optimizer_switch='hypergraph_optimizer=on') */ COUNT(*) AS result
FROM t1 AS l
CROSS JOIN t2 AS r
HAVING FALSE;
-- Empty result.
SELECT /*+ SET_VAR(optimizer_switch='hypergraph_optimizer=on') */ COUNT(*) AS result
FROM t1 AS l
CROSS JOIN t2 AS r
HAVING COUNT(*) >= 0
AND 0 = (SELECT COUNT(*) FROM t3);
-- Same empty result.
EXPLAIN FORMAT=TREE
SELECT /*+ SET_VAR(optimizer_switch='hypergraph_optimizer=on') */ COUNT(*) AS result
FROM t1 AS l
CROSS JOIN t2 AS r
HAVING COUNT(*) >= 0
AND 0 = (SELECT COUNT(*) FROM t3);
Suggested fix:
Determine pre-evaluation safety separately for each conjunction leaf, so an aggregate-dependent HAVING leaf does not prevent early evaluation of an independent execution-constant false leaf before the source iterator is initialized.
Portions of this website are copyright © 2001, 2002 The PHP Group
Page generated in 0.039 sec. using MySQL 8.4.7-u6-cloud
Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the
respective copyright holders.
It is not reviewed in advance
by Oracle and does not necessarily represent the opinion of
Oracle or any other party.
Description: With the hypergraph optimizer enabled, a false execution-constant scalar-subquery conjunct in HAVING does not prevent evaluation of a separate aggregate-dependent conjunct. MySQL reads and aggregates the entire Cartesian join even though the scalar subquery makes the HAVING condition false. The two queries below both returned an empty result. With two 10,000-row input tables, the literal-FALSE baseline took 11.8, 11.5, and 11.2 ms. The equivalent query containing the scalar-subquery conjunct took 2978.4, 2899.4, and 3119.5 ms: 251.6x, 251.6x, and 277.4x slower. EXPLAIN FORMAT=TREE for the slow query included: -> Filter: ((count(0) >= 0) and <cache>((0 = (select #2)))) (cost=19.7e+6..19.7e+6 rows=1) -> Aggregate: count(0), count(0) (cost=19.7e+6..19.7e+6 rows=1) -> Inner hash join (no condition) (cost=5806..2.01e+6 rows=88e+6) -> Covering index scan on l using PRIMARY (cost=0.249..2190 rows=8802) -> Hash -> Covering index scan on r using PRIMARY (cost=0.249..2488 rows=10000) -> Select #2 (subquery in condition; run only once) -> Aggregate: count(0) (cost=0.645..0.645 rows=1) -> Covering index scan on t3 using PRIMARY (cost=0.249..0.249 rows=1) Because t3 contains one row, SELECT COUNT(*) FROM t3 returns 1 and 0 = 1 is false. The Cartesian aggregate therefore cannot affect the empty result. The related issues are #35368475, which also concerns constant-false FilterIterator behavior, but does not establish this HAVING conjunction case where an aggregate-dependent leaf suppresses early evaluation of an independent false leaf; and #111671, which concerns a general query-performance regression and was closed as Not a Bug, but does not cover this reproducible HAVING/scalar-subquery conjunction. How to repeat: Run as root in the mysql client: CREATE DATABASE db1; USE db1; CREATE TABLE t1 (k INT NOT NULL PRIMARY KEY) ENGINE=InnoDB; CREATE TABLE t2 (k INT NOT NULL PRIMARY KEY) ENGINE=InnoDB; CREATE TABLE t3 (k INT NOT NULL PRIMARY KEY) ENGINE=InnoDB; INSERT INTO t1 (k) WITH RECURSIVE digits AS ( SELECT 0 AS d UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 ) SELECT a.d + 10*b.d + 100*c.d + 1000*d.d + 1 FROM digits AS a CROSS JOIN digits AS b CROSS JOIN digits AS c CROSS JOIN digits AS d; INSERT INTO t2 SELECT * FROM t1; INSERT INTO t3 VALUES (1); ANALYZE TABLE t1, t2, t3; SELECT /*+ SET_VAR(optimizer_switch='hypergraph_optimizer=on') */ COUNT(*) AS result FROM t1 AS l CROSS JOIN t2 AS r HAVING FALSE; -- Empty result. SELECT /*+ SET_VAR(optimizer_switch='hypergraph_optimizer=on') */ COUNT(*) AS result FROM t1 AS l CROSS JOIN t2 AS r HAVING COUNT(*) >= 0 AND 0 = (SELECT COUNT(*) FROM t3); -- Same empty result. EXPLAIN FORMAT=TREE SELECT /*+ SET_VAR(optimizer_switch='hypergraph_optimizer=on') */ COUNT(*) AS result FROM t1 AS l CROSS JOIN t2 AS r HAVING COUNT(*) >= 0 AND 0 = (SELECT COUNT(*) FROM t3); Suggested fix: Determine pre-evaluation safety separately for each conjunction leaf, so an aggregate-dependent HAVING leaf does not prevent early evaluation of an independent execution-constant false leaf before the source iterator is initialized.