Bug #2904 | Subqueries lack optimization | ||
---|---|---|---|
Submitted: | 20 Feb 2004 7:05 | Modified: | 14 Jan 2020 20:21 |
Reporter: | Joao Santos | Email Updates: | |
Status: | Closed | Impact on me: | |
Category: | MySQL Server: Optimizer | Severity: | S4 (Feature request) |
Version: | 5.0.0-alpha | OS: | Any |
Assigned to: | CPU Architecture: | Any | |
Tags: | performance, subquery |
[20 Feb 2004 7:05]
Joao Santos
[17 Mar 2004 17:10]
Dean Ellis
It seems to be a join order issue, mostly. A test case: CREATE TABLE t1 ( a INT UNSIGNED NOT NULL PRIMARY KEY ); CREATE TABLE t2 LIKE t1; INSERT INTO t1 SELECT a+b+c+d+1 FROM ( SELECT 0 AS a UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 ) a CROSS JOIN ( SELECT 0 AS b UNION SELECT 10 UNION SELECT 20 UNION SELECT 30 UNION SELECT 40 UNION SELECT 50 UNION SELECT 60 UNION SELECT 70 UNION SELECT 80 UNION SELECT 90 ) b CROSS JOIN ( SELECT 0 AS c UNION SELECT 100 UNION SELECT 200 UNION SELECT 300 UNION SELECT 400 UNION SELECT 500 UNION SELECT 600 UNION SELECT 700 UNION SELECT 800 UNION SELECT 900 ) c CROSS JOIN ( SELECT 0 AS d UNION SELECT 1000 UNION SELECT 2000 UNION SELECT 3000 UNION SELECT 4000 UNION SELECT 5000 UNION SELECT 6000 UNION SELECT 7000 UNION SELECT 8000 UNION SELECT 9000 ) d; INSERT INTO t2 SELECT a FROM t1 WHERE a % 2; -- Both of these are fast SELECT STRAIGHT_JOIN COUNT(*) FROM t1 INNER JOIN t2 USING (a); SELECT STRAIGHT_JOIN COUNT(*) FROM t2 INNER JOIN t1 USING (a); -- First is slow; second is fast SELECT STRAIGHT_JOIN COUNT(*) FROM t1 INNER JOIN (SELECT a FROM t1 WHERE a %2) t2 USING (a); SELECT STRAIGHT_JOIN COUNT(*) FROM (SELECT a FROM t1 WHERE a %2) t2 INNER JOIN t1 USING (a);
[14 Jan 2020 20:21]
Roy Lyseng
Posted by developer: All of these operations are now consistently fast, hence I consider this feature implemented. The feature that fixed this problem is probably subquery materialization with index generation. Implemented in 5.7 (or maybe earlier).