Type:
Bug
Priority:
Minor
Resolution:
Unresolved
Affects Version/s:
5.1.67 , 5.2.14 , 5.3.12 , 5.5.36 , 10.0.9
Component/s:
None
The following test case shows that if join optimization pruning heuristics
is ON (the default behavior in all MariaDB/MySQL) version, the optimizer
misses the optimal query plan and chooses a very inefficient query plan.
CREATE TABLE t1 (a INT , KEY (a));
INSERT INTO t1 VALUES (7),(5),(1),(204),(224),(9),(5),(0),(3);
INSERT INTO t1 SELECT al1.* FROM t1 al1, t1 al2;
-- default prune level is 1
set optimizer_prune_level=1;
EXPLAIN SELECT MAX (al1.a) FROM t1 AS al4, t1 AS al3, t1 AS al2, t1 AS al1 WHERE al4.a = al3.a;
+ ----+-------------+-------+-------+---------------+------+---------+-------------+------+--------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+ ----+-------------+-------+-------+---------------+------+---------+-------------+------+--------------------------------+
| 1 | SIMPLE | al4 | index | a | a | 5 | NULL | 90 | Using index |
| 1 | SIMPLE | al3 | ref | a | a | 5 | md321.al4.a | 10 | Using where ; Using index |
| 1 | SIMPLE | al2 | index | NULL | a | 5 | NULL | 90 | Using index ; Using join buffer |
| 1 | SIMPLE | al1 | index | NULL | a | 5 | NULL | 90 | Using index ; Using join buffer |
+ ----+-------------+-------+-------+---------------+------+---------+-------------+------+--------------------------------+
SHOW SESSION STATUS LIKE 'Last_query_cost' ;
+ -----------------+----------------+
| Variable_name | Value |
+ -----------------+----------------+
| Last_query_cost | 1531029.266998 |
+ -----------------+----------------+
Above, if we order the tables in the FROM clause according to their optimal order, the optimizer
finds the optimal plan.
Below, if the tables are in reverse order, then the optimizer misses the optimal order due to pruning:
EXPLAIN SELECT MAX (al1.a) FROM t1 AS al1, t1 AS al2, t1 AS al3, t1 AS al4 WHERE al4.a = al3.a;
+ ----+-------------+-------+-------+---------------+------+---------+-------------+------+--------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+ ----+-------------+-------+-------+---------------+------+---------+-------------+------+--------------------------------+
| 1 | SIMPLE | al1 | index | NULL | a | 5 | NULL | 90 | Using index |
| 1 | SIMPLE | al2 | index | NULL | a | 5 | NULL | 90 | Using index ; Using join buffer |
| 1 | SIMPLE | al3 | index | a | a | 5 | NULL | 90 | Using index ; Using join buffer |
| 1 | SIMPLE | al4 | ref | a | a | 5 | md321.al3.a | 10 | Using where ; Using index |
+ ----+-------------+-------+-------+---------------+------+---------+-------------+------+--------------------------------+
SHOW SESSION STATUS LIKE 'Last_query_cost' ;
+ -----------------+----------------+
| Variable_name | Value |
+ -----------------+----------------+
| Last_query_cost | 2420964.599961 |
+ -----------------+----------------+
As shown below, if we switch off the pruning heuristics, the join optimizer finds the optimal
plan in all cases.
-- switch off pruning heuristics
set optimizer_prune_level=0;
EXPLAIN SELECT MAX (al1.a) FROM t1 AS al4, t1 AS al3, t1 AS al2, t1 AS al1 WHERE al4.a = al3.a;
+ ----+-------------+-------+-------+---------------+------+---------+-------------+------+--------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+ ----+-------------+-------+-------+---------------+------+---------+-------------+------+--------------------------------+
| 1 | SIMPLE | al4 | index | a | a | 5 | NULL | 90 | Using index |
| 1 | SIMPLE | al3 | ref | a | a | 5 | md321.al4.a | 10 | Using where ; Using index |
| 1 | SIMPLE | al2 | index | NULL | a | 5 | NULL | 90 | Using index ; Using join buffer |
| 1 | SIMPLE | al1 | index | NULL | a | 5 | NULL | 90 | Using index ; Using join buffer |
+ ----+-------------+-------+-------+---------------+------+---------+-------------+------+--------------------------------+
SHOW SESSION STATUS LIKE 'Last_query_cost' ;
+ -----------------+----------------+
| Variable_name | Value |
+ -----------------+----------------+
| Last_query_cost | 1531029.266998 |
+ -----------------+----------------+
EXPLAIN SELECT MAX (al1.a) FROM t1 AS al1, t1 AS al2, t1 AS al3, t1 AS al4 WHERE al4.a = al3.a;
+ ----+-------------+-------+-------+---------------+------+---------+-------------+------+--------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+ ----+-------------+-------+-------+---------------+------+---------+-------------+------+--------------------------------+
| 1 | SIMPLE | al3 | index | a | a | 5 | NULL | 90 | Using index |
| 1 | SIMPLE | al4 | ref | a | a | 5 | md321.al3.a | 10 | Using where ; Using index |
| 1 | SIMPLE | al2 | index | NULL | a | 5 | NULL | 90 | Using index ; Using join buffer |
| 1 | SIMPLE | al1 | index | NULL | a | 5 | NULL | 90 | Using index ; Using join buffer |
+ ----+-------------+-------+-------+---------------+------+---------+-------------+------+--------------------------------+
SHOW SESSION STATUS LIKE 'Last_query_cost' ;
+ -----------------+----------------+
| Variable_name | Value |
+ -----------------+----------------+
| Last_query_cost | 1531029.266998 |
+ -----------------+----------------+
Notice that since al3 and al4 are exactly the same, the order <al3, al4> is the same as <al4, al3>.
{"report":{"fcp":2220.199999809265,"ttfb":466,"pageVisibility":"visible","entityId":20154,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":32,"apdex":0.5,"journeyId":"370be331-dbbf-470e-986f-b27348da6b62","navigationType":0,"readyForUser":2361.7999997138977,"redirectCount":0,"resourceLoadedEnd":1828.2999997138977,"resourceLoadedStart":498.40000009536743,"resourceTiming":[{"duration":289,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":498.40000009536743,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":498.40000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":787.4000000953674,"responseStart":0,"secureConnectionStart":0},{"duration":289.09999990463257,"initiatorType":"link","name":"https://jira.mariadb.org/s/7ebd35e77e471bc30ff0eba799ebc151-CDN/lu2cib/820016/12ta74/494e4c556ecbb29f90a3d3b4f09cb99c/_/download/contextbatch/css/jira.browse.project,project.issue.navigator,jira.view.issue,jira.general,jira.global,atl.general,-_super/batch.css?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&slack-enabled=true&whisper-enabled=true","startTime":498.59999990463257,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":498.59999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":787.6999998092651,"responseStart":0,"secureConnectionStart":0},{"duration":1053.9000000953674,"initiatorType":"script","name":"https://jira.mariadb.org/s/0917945aaa57108d00c5076fea35e069-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":498.7999997138977,"connectEnd":498.7999997138977,"connectStart":498.7999997138977,"domainLookupEnd":498.7999997138977,"domainLookupStart":498.7999997138977,"fetchStart":498.7999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":849.7999997138977,"responseEnd":1552.6999998092651,"responseStart":937.9000000953674,"secureConnectionStart":498.7999997138977},{"duration":1329.2999997138977,"initiatorType":"script","name":"https://jira.mariadb.org/s/2d8175ec2fa4c816e8023260bd8c1786-CDN/lu2cib/820016/12ta74/494e4c556ecbb29f90a3d3b4f09cb99c/_/download/contextbatch/js/jira.browse.project,project.issue.navigator,jira.view.issue,jira.general,jira.global,atl.general,-_super/batch.js?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&locale=en&slack-enabled=true&whisper-enabled=true","startTime":499,"connectEnd":499,"connectStart":499,"domainLookupEnd":499,"domainLookupStart":499,"fetchStart":499,"redirectEnd":0,"redirectStart":0,"requestStart":849.9000000953674,"responseEnd":1828.2999997138977,"responseStart":926.6999998092651,"secureConnectionStart":499},{"duration":549.8000001907349,"initiatorType":"script","name":"https://jira.mariadb.org/s/a9324d6758d385eb45c462685ad88f1d-CDN/lu2cib/820016/12ta74/c92c0caa9a024ae85b0ebdbed7fb4bd7/_/download/contextbatch/js/atl.global,-_super/batch.js?locale=en","startTime":499.09999990463257,"connectEnd":499.09999990463257,"connectStart":499.09999990463257,"domainLookupEnd":499.09999990463257,"domainLookupStart":499.09999990463257,"fetchStart":499.09999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":849.9000000953674,"responseEnd":1048.9000000953674,"responseStart":938.4000000953674,"secureConnectionStart":499.09999990463257},{"duration":556.7000002861023,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-en/jira.webresources:calendar-en.js","startTime":499.2999997138977,"connectEnd":499.2999997138977,"connectStart":499.2999997138977,"domainLookupEnd":499.2999997138977,"domainLookupStart":499.2999997138977,"fetchStart":499.2999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":850.1999998092651,"responseEnd":1056,"responseStart":940.0999999046326,"secureConnectionStart":499.2999997138977},{"duration":550.3000001907349,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-localisation-moment/jira.webresources:calendar-localisation-moment.js","startTime":500.19999980926514,"connectEnd":500.19999980926514,"connectStart":500.19999980926514,"domainLookupEnd":500.19999980926514,"domainLookupStart":500.19999980926514,"fetchStart":500.19999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":850,"responseEnd":1050.5,"responseStart":939.1999998092651,"secureConnectionStart":500.19999980926514},{"duration":292.40000009536743,"initiatorType":"link","name":"https://jira.mariadb.org/s/b04b06a02d1959df322d9cded3aeecc1-CDN/lu2cib/820016/12ta74/a2ff6aa845ffc9a1d22fe23d9ee791fc/_/download/contextbatch/css/jira.global.look-and-feel,-_super/batch.css","startTime":502.19999980926514,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":502.19999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":794.5999999046326,"responseStart":0,"secureConnectionStart":0},{"duration":553.6999998092651,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":502.5,"connectEnd":502.5,"connectStart":502.5,"domainLookupEnd":502.5,"domainLookupStart":502.5,"fetchStart":502.5,"redirectEnd":0,"redirectStart":0,"requestStart":850.1999998092651,"responseEnd":1056.1999998092651,"responseStart":1051,"secureConnectionStart":502.5},{"duration":292.09999990463257,"initiatorType":"link","name":"https://jira.mariadb.org/s/3ac36323ba5e4eb0af2aa7ac7211b4bb-CDN/lu2cib/820016/12ta74/d176f0986478cc64f24226b3d20c140d/_/download/contextbatch/css/com.atlassian.jira.projects.sidebar.init,-_super,-project.issue.navigator,-jira.view.issue/batch.css?jira.create.linked.issue=true","startTime":502.69999980926514,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":502.69999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":794.7999997138977,"responseStart":0,"secureConnectionStart":0},{"duration":554.2000002861023,"initiatorType":"script","name":"https://jira.mariadb.org/s/5d5e8fe91fbc506585e83ea3b62ccc4b-CDN/lu2cib/820016/12ta74/d176f0986478cc64f24226b3d20c140d/_/download/contextbatch/js/com.atlassian.jira.projects.sidebar.init,-_super,-project.issue.navigator,-jira.view.issue/batch.js?jira.create.linked.issue=true&locale=en","startTime":502.7999997138977,"connectEnd":502.7999997138977,"connectStart":502.7999997138977,"domainLookupEnd":502.7999997138977,"domainLookupStart":502.7999997138977,"fetchStart":502.7999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":850.2999997138977,"responseEnd":1057,"responseStart":1053.2999997138977,"secureConnectionStart":502.7999997138977},{"duration":1315.0999999046326,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-js/jira.webresources:bigpipe-js.js","startTime":508.59999990463257,"connectEnd":508.59999990463257,"connectStart":508.59999990463257,"domainLookupEnd":508.59999990463257,"domainLookupStart":508.59999990463257,"fetchStart":508.59999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":1399.4000000953674,"responseEnd":1823.6999998092651,"responseStart":1801.6999998092651,"secureConnectionStart":508.59999990463257},{"duration":1286,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-init/jira.webresources:bigpipe-init.js","startTime":537.9000000953674,"connectEnd":537.9000000953674,"connectStart":537.9000000953674,"domainLookupEnd":537.9000000953674,"domainLookupStart":537.9000000953674,"fetchStart":537.9000000953674,"redirectEnd":0,"redirectStart":0,"requestStart":1403.1999998092651,"responseEnd":1823.9000000953674,"responseStart":1803.2999997138977,"secureConnectionStart":537.9000000953674},{"duration":275.59999990463257,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":1722.4000000953674,"connectEnd":1722.4000000953674,"connectStart":1722.4000000953674,"domainLookupEnd":1722.4000000953674,"domainLookupStart":1722.4000000953674,"fetchStart":1722.4000000953674,"redirectEnd":0,"redirectStart":0,"requestStart":1855.5999999046326,"responseEnd":1998,"responseStart":1991.2999997138977,"secureConnectionStart":1722.4000000953674},{"duration":392.1000003814697,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":2130.7999997138977,"connectEnd":2130.7999997138977,"connectStart":2130.7999997138977,"domainLookupEnd":2130.7999997138977,"domainLookupStart":2130.7999997138977,"fetchStart":2130.7999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":2476,"responseEnd":2522.9000000953674,"responseStart":2521.9000000953674,"secureConnectionStart":2130.7999997138977},{"duration":296.5,"initiatorType":"script","name":"https://www.google-analytics.com/analytics.js","startTime":2199.5,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":2199.5,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":2496,"responseStart":0,"secureConnectionStart":0}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":119,"responseStart":466,"responseEnd":537,"domLoading":496,"domInteractive":2563,"domContentLoadedEventStart":2563,"domContentLoadedEventEnd":2647,"domComplete":3369,"loadEventStart":3369,"loadEventEnd":3369,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":2459.5},{"name":"bigPipe.sidebar-id.end","time":2460.4000000953674},{"name":"bigPipe.activity-panel-pipe-id.start","time":2460.5999999046326},{"name":"bigPipe.activity-panel-pipe-id.end","time":2466.7999997138977},{"name":"activityTabFullyLoaded","time":2679.4000000953674}],"measures":[],"correlationId":"91b0e8abceb434","effectiveType":"4g","downlink":10,"rtt":0,"serverDuration":98,"dbReadsTimeInMs":8,"dbConnsTimeInMs":16,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}
Re: Optimizer pruning chooses very inefficient join plan when all joined tables are similar
Test case:
EXPLAIN
EXPLAIN
EXPLAIN
EXPLAIN