The optimizer may make a very wrong choice in select ... where key1=const ORDER BY key2.
This bug originates from me analyzing the testcase from MDEV-6041 , but it is orthogonal to MDEV-6041 .
Create the test dataset:
create table t1 (a int);
insert into t1 select * from test.one_k;
create table tsubq(
id int primary key,
key1 int,
col1 int,
key(key1)
) engine=innodb;
insert into tsubq
select A.a + B.a*1000, A.a, 123456 from test.one_k A, test.one_k B;
alter table tsubq add key2 int;
update tsubq set key2=key1;
alter table tsubq add key(key2);
Then run the query:
explain select
(SELECT
concat(id, '-', key1, '-', col1)
FROM tsubq
WHERE tsubq.key1 = t1.a
ORDER BY tsubq.key2 ASC LIMIT 1)
from t1;
Query plan in MariaDB:
+------+--------------------+-------+-------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+--------------------+-------+-------+---------------+------+---------+------+------+-------------+
| 1 | PRIMARY | t1 | ALL | NULL | NULL | NULL | NULL | 1000 | |
| 2 | DEPENDENT SUBQUERY | tsubq | index | key1 | key2 | 5 | NULL | 1 | Using where |
+------+--------------------+-------+-------+---------------+------+---------+------+------+-------------+
This is a very inefficient plan. Assume key1 and key2 are not correlated. The plan shows that we will use full index scan on "key2". However, we know that we need to find a record that matches condition "tsubq.key1 = t1.a", which is very selective. We will have to scan a lot of rows before finding a match. It is much cheaper to use ref access on key1 and then use filesort.
MySQL has fixed this in 5.6. Starting from 5.6, one gets:
+----+--------------------+-------+------+---------------+------+---------+---------+------+-----------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+-------+------+---------------+------+---------+---------+------+-----------------------------+
| 1 | PRIMARY | t1 | ALL | NULL | NULL | NULL | NULL | 1 | NULL |
| 2 | DEPENDENT SUBQUERY | tsubq | ref | key1 | key1 | 5 | j5.t1.a | 532 | Using where; Using filesort |
+----+--------------------+-------+------+---------------+------+---------+---------+------+-----------------------------+
relates to
MDEV-6041
ORDER BY+subqueries: subquery_table.key=outer_table.col is not recongized as binding
Closed
Sergei Petrunia
made changes -
2014-04-11 23:08
Status
Open
[ 1
]
In Progress
[ 3
]
Sergei Petrunia
made changes -
2014-04-12 00:10
Resolution
Fixed
[ 1
]
Status
In Progress
[ 3
]
Closed
[ 6
]
Sergei Golubchik
made changes -
2014-06-13 15:06
Workflow
defaullt
[ 38826
]
MariaDB v2
[ 43786
]
Sergei Golubchik
made changes -
2021-12-06 21:39
Workflow
MariaDB v3
[ 62978
]
MariaDB v4
[ 147779
]
{"report":{"fcp":873.5,"ttfb":244.5,"pageVisibility":"visible","entityId":35026,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":1,"journeyId":"00b014bc-12ab-4e73-9b50-0ce3921864b7","navigationType":0,"readyForUser":951.8999996185303,"redirectCount":0,"resourceLoadedEnd":591.5,"resourceLoadedStart":252.5999994277954,"resourceTiming":[{"duration":21.699999809265137,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2bv2/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":252.5999994277954,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":252.5999994277954,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":274.29999923706055,"responseStart":0,"secureConnectionStart":0},{"duration":22.899999618530273,"initiatorType":"link","name":"https://jira.mariadb.org/s/7ebd35e77e471bc30ff0eba799ebc151-CDN/lu2bv2/820016/12ta74/2380add21a9a1006587582385952de73/_/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","startTime":252.89999961853027,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":252.89999961853027,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":275.79999923706055,"responseStart":0,"secureConnectionStart":0},{"duration":181.69999980926514,"initiatorType":"script","name":"https://jira.mariadb.org/s/e9b27a47da5fb0f74a35acd57e9847fb-CDN/lu2bv2/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":253.19999980926514,"connectEnd":280.5,"connectStart":280.5,"domainLookupEnd":280.5,"domainLookupStart":280.5,"fetchStart":253.19999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":281.8999996185303,"responseEnd":434.8999996185303,"responseStart":297.5999994277954,"secureConnectionStart":280.5},{"duration":307.69999980926514,"initiatorType":"script","name":"https://jira.mariadb.org/s/c32eb0da7ad9831253f8397e6cc26afd-CDN/lu2bv2/820016/12ta74/2380add21a9a1006587582385952de73/_/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","startTime":253.69999980926514,"connectEnd":253.69999980926514,"connectStart":253.69999980926514,"domainLookupEnd":253.69999980926514,"domainLookupStart":253.69999980926514,"fetchStart":253.69999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":282.3999996185303,"responseEnd":561.3999996185303,"responseStart":303.5,"secureConnectionStart":253.69999980926514},{"duration":55.80000019073486,"initiatorType":"script","name":"https://jira.mariadb.org/s/bc0bcb146314416123c992714ee00ff7-CDN/lu2bv2/820016/12ta74/c92c0caa9a024ae85b0ebdbed7fb4bd7/_/download/contextbatch/js/atl.global,-_super/batch.js?locale=en","startTime":253.89999961853027,"connectEnd":253.89999961853027,"connectStart":253.89999961853027,"domainLookupEnd":253.89999961853027,"domainLookupStart":253.89999961853027,"fetchStart":253.89999961853027,"redirectEnd":0,"redirectStart":0,"requestStart":282.69999980926514,"responseEnd":309.69999980926514,"responseStart":307.8999996185303,"secureConnectionStart":253.89999961853027},{"duration":55.30000019073486,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bv2/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-en/jira.webresources:calendar-en.js","startTime":254.0999994277954,"connectEnd":254.0999994277954,"connectStart":254.0999994277954,"domainLookupEnd":254.0999994277954,"domainLookupStart":254.0999994277954,"fetchStart":254.0999994277954,"redirectEnd":0,"redirectStart":0,"requestStart":283.0999994277954,"responseEnd":309.3999996185303,"responseStart":306.79999923706055,"secureConnectionStart":254.0999994277954},{"duration":50.30000019073486,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bv2/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-localisation-moment/jira.webresources:calendar-localisation-moment.js","startTime":254.29999923706055,"connectEnd":254.29999923706055,"connectStart":254.29999923706055,"domainLookupEnd":254.29999923706055,"domainLookupStart":254.29999923706055,"fetchStart":254.29999923706055,"redirectEnd":0,"redirectStart":0,"requestStart":283.79999923706055,"responseEnd":304.5999994277954,"responseStart":302.8999996185303,"secureConnectionStart":254.29999923706055},{"duration":28,"initiatorType":"link","name":"https://jira.mariadb.org/s/b04b06a02d1959df322d9cded3aeecc1-CDN/lu2bv2/820016/12ta74/a2ff6aa845ffc9a1d22fe23d9ee791fc/_/download/contextbatch/css/jira.global.look-and-feel,-_super/batch.css","startTime":254.5,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":254.5,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":282.5,"responseStart":0,"secureConnectionStart":0},{"duration":57.10000038146973,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":254.5999994277954,"connectEnd":254.5999994277954,"connectStart":254.5999994277954,"domainLookupEnd":254.5999994277954,"domainLookupStart":254.5999994277954,"fetchStart":254.5999994277954,"redirectEnd":0,"redirectStart":0,"requestStart":287.69999980926514,"responseEnd":311.69999980926514,"responseStart":310.3999996185303,"secureConnectionStart":254.5999994277954},{"duration":32.30000019073486,"initiatorType":"link","name":"https://jira.mariadb.org/s/3ac36323ba5e4eb0af2aa7ac7211b4bb-CDN/lu2bv2/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":254.69999980926514,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":254.69999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":287,"responseStart":0,"secureConnectionStart":0},{"duration":61.80000019073486,"initiatorType":"script","name":"https://jira.mariadb.org/s/719848dd97ebe0663199f49a3936487a-CDN/lu2bv2/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":254.79999923706055,"connectEnd":254.79999923706055,"connectStart":254.79999923706055,"domainLookupEnd":254.79999923706055,"domainLookupStart":254.79999923706055,"fetchStart":254.79999923706055,"redirectEnd":0,"redirectStart":0,"requestStart":289.3999996185303,"responseEnd":316.5999994277954,"responseStart":312.69999980926514,"secureConnectionStart":254.79999923706055},{"duration":324,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bv2/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-js/jira.webresources:bigpipe-js.js","startTime":263.19999980926514,"connectEnd":263.19999980926514,"connectStart":263.19999980926514,"domainLookupEnd":263.19999980926514,"domainLookupStart":263.19999980926514,"fetchStart":263.19999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":328.69999980926514,"responseEnd":587.1999998092651,"responseStart":582.5999994277954,"secureConnectionStart":263.19999980926514},{"duration":328.20000076293945,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bv2/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-init/jira.webresources:bigpipe-init.js","startTime":263.29999923706055,"connectEnd":263.29999923706055,"connectStart":263.29999923706055,"domainLookupEnd":263.29999923706055,"domainLookupStart":263.29999923706055,"fetchStart":263.29999923706055,"redirectEnd":0,"redirectStart":0,"requestStart":434.5,"responseEnd":591.5,"responseStart":584.2999992370605,"secureConnectionStart":263.29999923706055},{"duration":111.59999942779541,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":583.1999998092651,"connectEnd":583.1999998092651,"connectStart":583.1999998092651,"domainLookupEnd":583.1999998092651,"domainLookupStart":583.1999998092651,"fetchStart":583.1999998092651,"redirectEnd":0,"redirectStart":0,"requestStart":658.8999996185303,"responseEnd":694.7999992370605,"responseStart":694.0999994277954,"secureConnectionStart":583.1999998092651},{"duration":229.79999923706055,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":788,"connectEnd":788,"connectStart":788,"domainLookupEnd":788,"domainLookupStart":788,"fetchStart":788,"redirectEnd":0,"redirectStart":0,"requestStart":978.6999998092651,"responseEnd":1017.7999992370605,"responseStart":1016.8999996185303,"secureConnectionStart":788}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":39,"responseStart":245,"responseEnd":262,"domLoading":248,"domInteractive":1012,"domContentLoadedEventStart":1012,"domContentLoadedEventEnd":1060,"domComplete":1465,"loadEventStart":1465,"loadEventEnd":1466,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":990.1999998092651},{"name":"bigPipe.sidebar-id.end","time":991.0999994277954},{"name":"bigPipe.activity-panel-pipe-id.start","time":991.1999998092651},{"name":"bigPipe.activity-panel-pipe-id.end","time":993.1999998092651},{"name":"activityTabFullyLoaded","time":1081.6999998092651}],"measures":[],"correlationId":"146613069ca3f4","effectiveType":"4g","downlink":10,"rtt":0,"serverDuration":124,"dbReadsTimeInMs":28,"dbConnsTimeInMs":41,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}