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":882.7000002861023,"ttfb":273.80000019073486,"pageVisibility":"visible","entityId":20154,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":1,"journeyId":"1d6bfbe7-facb-441a-99d7-cc4417687963","navigationType":0,"readyForUser":992.7000002861023,"redirectCount":0,"resourceLoadedEnd":581.1000003814697,"resourceLoadedStart":279.2000002861023,"resourceTiming":[{"duration":12,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":279.2000002861023,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":279.2000002861023,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":291.2000002861023,"responseStart":0,"secureConnectionStart":0},{"duration":12.099999904632568,"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":279.40000009536743,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":279.40000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":291.5,"responseStart":0,"secureConnectionStart":0},{"duration":192.2999997138977,"initiatorType":"script","name":"https://jira.mariadb.org/s/0917945aaa57108d00c5076fea35e069-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":279.6000003814697,"connectEnd":279.6000003814697,"connectStart":279.6000003814697,"domainLookupEnd":279.6000003814697,"domainLookupStart":279.6000003814697,"fetchStart":279.6000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":294.1000003814697,"responseEnd":471.90000009536743,"responseStart":332,"secureConnectionStart":279.6000003814697},{"duration":301.30000019073486,"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":279.80000019073486,"connectEnd":279.80000019073486,"connectStart":279.80000019073486,"domainLookupEnd":279.80000019073486,"domainLookupStart":279.80000019073486,"fetchStart":279.80000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":294.90000009536743,"responseEnd":581.1000003814697,"responseStart":333,"secureConnectionStart":279.80000019073486},{"duration":13.599999904632568,"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":279.90000009536743,"connectEnd":279.90000009536743,"connectStart":279.90000009536743,"domainLookupEnd":279.90000009536743,"domainLookupStart":279.90000009536743,"fetchStart":279.90000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":279.90000009536743,"responseEnd":293.5,"responseStart":293.5,"secureConnectionStart":279.90000009536743},{"duration":15.199999809265137,"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":280.1000003814697,"connectEnd":280.1000003814697,"connectStart":280.1000003814697,"domainLookupEnd":280.1000003814697,"domainLookupStart":280.1000003814697,"fetchStart":280.1000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":280.1000003814697,"responseEnd":295.30000019073486,"responseStart":295.30000019073486,"secureConnectionStart":280.1000003814697},{"duration":15.5,"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":280.30000019073486,"connectEnd":280.30000019073486,"connectStart":280.30000019073486,"domainLookupEnd":280.30000019073486,"domainLookupStart":280.30000019073486,"fetchStart":280.30000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":280.30000019073486,"responseEnd":295.80000019073486,"responseStart":295.80000019073486,"secureConnectionStart":280.30000019073486},{"duration":16,"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":280.5,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":280.5,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":296.5,"responseStart":0,"secureConnectionStart":0},{"duration":56.799999713897705,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":280.6000003814697,"connectEnd":280.6000003814697,"connectStart":280.6000003814697,"domainLookupEnd":280.6000003814697,"domainLookupStart":280.6000003814697,"fetchStart":280.6000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":305,"responseEnd":337.40000009536743,"responseStart":335.90000009536743,"secureConnectionStart":280.6000003814697},{"duration":17.59999990463257,"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":280.90000009536743,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":280.90000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":298.5,"responseStart":0,"secureConnectionStart":0},{"duration":17.799999713897705,"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":281.1000003814697,"connectEnd":281.1000003814697,"connectStart":281.1000003814697,"domainLookupEnd":281.1000003814697,"domainLookupStart":281.1000003814697,"fetchStart":281.1000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":281.1000003814697,"responseEnd":298.90000009536743,"responseStart":298.90000009536743,"secureConnectionStart":281.1000003814697},{"duration":249.89999961853027,"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":295.1000003814697,"connectEnd":295.1000003814697,"connectStart":295.1000003814697,"domainLookupEnd":295.1000003814697,"domainLookupStart":295.1000003814697,"fetchStart":295.1000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":323.6000003814697,"responseEnd":545,"responseStart":541.3000001907349,"secureConnectionStart":295.1000003814697},{"duration":247.7000002861023,"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":297.5,"connectEnd":297.5,"connectStart":297.5,"domainLookupEnd":297.5,"domainLookupStart":297.5,"fetchStart":297.5,"redirectEnd":0,"redirectStart":0,"requestStart":323.80000019073486,"responseEnd":545.2000002861023,"responseStart":541.8000001907349,"secureConnectionStart":297.5},{"duration":166,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":631.3000001907349,"connectEnd":631.3000001907349,"connectStart":631.3000001907349,"domainLookupEnd":631.3000001907349,"domainLookupStart":631.3000001907349,"fetchStart":631.3000001907349,"redirectEnd":0,"redirectStart":0,"requestStart":763.8000001907349,"responseEnd":797.3000001907349,"responseStart":794.8000001907349,"secureConnectionStart":631.3000001907349},{"duration":209,"initiatorType":"script","name":"https://www.google-analytics.com/analytics.js","startTime":875.9000000953674,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":875.9000000953674,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1084.9000000953674,"responseStart":0,"secureConnectionStart":0},{"duration":115,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":882.1000003814697,"connectEnd":882.1000003814697,"connectStart":882.1000003814697,"domainLookupEnd":882.1000003814697,"domainLookupStart":882.1000003814697,"fetchStart":882.1000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":966.2000002861023,"responseEnd":997.1000003814697,"responseStart":995.4000000953674,"secureConnectionStart":882.1000003814697}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":24,"responseStart":273,"responseEnd":297,"domLoading":277,"domInteractive":1118,"domContentLoadedEventStart":1118,"domContentLoadedEventEnd":1181,"domComplete":1562,"loadEventStart":1562,"loadEventEnd":1562,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":1087.5},{"name":"bigPipe.sidebar-id.end","time":1088.3000001907349},{"name":"bigPipe.activity-panel-pipe-id.start","time":1088.4000000953674},{"name":"bigPipe.activity-panel-pipe-id.end","time":1091.2000002861023},{"name":"activityTabFullyLoaded","time":1199}],"measures":[],"correlationId":"107d6bbe080a13","effectiveType":"4g","downlink":10,"rtt":0,"serverDuration":117,"dbReadsTimeInMs":17,"dbConnsTimeInMs":27,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}
Re: Optimizer pruning chooses very inefficient join plan when all joined tables are similar
Test case:
EXPLAIN
EXPLAIN
EXPLAIN
EXPLAIN