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":1262.3000001907349,"ttfb":300.5,"pageVisibility":"visible","entityId":20154,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":0.5,"journeyId":"1545df5c-954b-4b29-89c3-cfc80223ff6f","navigationType":0,"readyForUser":1390.8000001907349,"redirectCount":0,"resourceLoadedEnd":927.9000000953674,"resourceLoadedStart":305.7000002861023,"resourceTiming":[{"duration":129.7999997138977,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":305.7000002861023,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":305.7000002861023,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":435.5,"responseStart":0,"secureConnectionStart":0},{"duration":130,"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":306.09999990463257,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":306.09999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":436.09999990463257,"responseStart":0,"secureConnectionStart":0},{"duration":427.09999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/s/0917945aaa57108d00c5076fea35e069-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":306.2000002861023,"connectEnd":306.2000002861023,"connectStart":306.2000002861023,"domainLookupEnd":306.2000002861023,"domainLookupStart":306.2000002861023,"fetchStart":306.2000002861023,"redirectEnd":0,"redirectStart":0,"requestStart":439.5,"responseEnd":733.3000001907349,"responseStart":452.7000002861023,"secureConnectionStart":306.2000002861023},{"duration":621.5,"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":306.40000009536743,"connectEnd":306.40000009536743,"connectStart":306.40000009536743,"domainLookupEnd":306.40000009536743,"domainLookupStart":306.40000009536743,"fetchStart":306.40000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":440,"responseEnd":927.9000000953674,"responseStart":456.30000019073486,"secureConnectionStart":306.40000009536743},{"duration":264.5,"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":306.5,"connectEnd":306.5,"connectStart":306.5,"domainLookupEnd":306.5,"domainLookupStart":306.5,"fetchStart":306.5,"redirectEnd":0,"redirectStart":0,"requestStart":446,"responseEnd":571,"responseStart":570.4000000953674,"secureConnectionStart":306.5},{"duration":265.59999990463257,"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":306.80000019073486,"connectEnd":306.80000019073486,"connectStart":306.80000019073486,"domainLookupEnd":306.80000019073486,"domainLookupStart":306.80000019073486,"fetchStart":306.80000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":446.30000019073486,"responseEnd":572.4000000953674,"responseStart":571.2000002861023,"secureConnectionStart":306.80000019073486},{"duration":263.40000009536743,"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":306.90000009536743,"connectEnd":306.90000009536743,"connectStart":306.90000009536743,"domainLookupEnd":306.90000009536743,"domainLookupStart":306.90000009536743,"fetchStart":306.90000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":446.7000002861023,"responseEnd":570.3000001907349,"responseStart":569.5999999046326,"secureConnectionStart":306.90000009536743},{"duration":139.2000002861023,"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":307.09999990463257,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":307.09999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":446.30000019073486,"responseStart":0,"secureConnectionStart":0},{"duration":160.5,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":307.30000019073486,"connectEnd":450,"connectStart":450,"domainLookupEnd":450,"domainLookupStart":450,"fetchStart":307.30000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":450.09999990463257,"responseEnd":467.80000019073486,"responseStart":465.80000019073486,"secureConnectionStart":450},{"duration":140.80000019073486,"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":307.40000009536743,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":307.40000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":448.2000002861023,"responseStart":0,"secureConnectionStart":0},{"duration":164.30000019073486,"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":307.59999990463257,"connectEnd":307.59999990463257,"connectStart":307.59999990463257,"domainLookupEnd":307.59999990463257,"domainLookupStart":307.59999990463257,"fetchStart":307.59999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":452.09999990463257,"responseEnd":471.90000009536743,"responseStart":468.30000019073486,"secureConnectionStart":307.59999990463257},{"duration":460,"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":328.30000019073486,"connectEnd":328.30000019073486,"connectStart":328.30000019073486,"domainLookupEnd":328.30000019073486,"domainLookupStart":328.30000019073486,"fetchStart":328.30000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":618.7000002861023,"responseEnd":788.3000001907349,"responseStart":786,"secureConnectionStart":328.30000019073486},{"duration":446.80000019073486,"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":341.90000009536743,"connectEnd":341.90000009536743,"connectStart":341.90000009536743,"domainLookupEnd":341.90000009536743,"domainLookupStart":341.90000009536743,"fetchStart":341.90000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":619.2000002861023,"responseEnd":788.7000002861023,"responseStart":786.5999999046326,"secureConnectionStart":341.90000009536743},{"duration":163.30000019073486,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":867.5999999046326,"connectEnd":867.5999999046326,"connectStart":867.5999999046326,"domainLookupEnd":867.5999999046326,"domainLookupStart":867.5999999046326,"fetchStart":867.5999999046326,"redirectEnd":0,"redirectStart":0,"requestStart":996.2000002861023,"responseEnd":1030.9000000953674,"responseStart":1030.2000002861023,"secureConnectionStart":867.5999999046326},{"duration":373.40000009536743,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":1154.8000001907349,"connectEnd":1154.8000001907349,"connectStart":1154.8000001907349,"domainLookupEnd":1154.8000001907349,"domainLookupStart":1154.8000001907349,"fetchStart":1154.8000001907349,"redirectEnd":0,"redirectStart":0,"requestStart":1493,"responseEnd":1528.2000002861023,"responseStart":1527.7000002861023,"secureConnectionStart":1154.8000001907349}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":116,"responseStart":300,"responseEnd":341,"domLoading":304,"domInteractive":1481,"domContentLoadedEventStart":1481,"domContentLoadedEventEnd":1545,"domComplete":2194,"loadEventStart":2194,"loadEventEnd":2195,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":1446.2000002861023},{"name":"bigPipe.sidebar-id.end","time":1447.0999999046326},{"name":"bigPipe.activity-panel-pipe-id.start","time":1447.2000002861023},{"name":"bigPipe.activity-panel-pipe-id.end","time":1450.0999999046326},{"name":"activityTabFullyLoaded","time":1563.5}],"measures":[],"correlationId":"5dee2c434fa4f3","effectiveType":"4g","downlink":9.1,"rtt":0,"serverDuration":101,"dbReadsTimeInMs":10,"dbConnsTimeInMs":19,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}
Re: Optimizer pruning chooses very inefficient join plan when all joined tables are similar
Test case:
EXPLAIN
EXPLAIN
EXPLAIN
EXPLAIN