Create a 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;
|
Then, check the plan:
explain select
|
(SELECT
|
concat(id, '-', key1, '-', col1)
|
FROM tsubq
|
WHERE tsubq.key1 = t1.a
|
ORDER BY tsubq.id ASC LIMIT 1)
|
from
|
t1;
|
+------+--------------------+-------+-------+---------------+---------+---------+------+------+-------------+
|
| 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 | PRIMARY | 4 | NULL | 1 | Using where |
|
+------+--------------------+-------+-------+---------------+---------+---------+------+------+-------------+
|
The subquery uses "index" access, which is very inefficient. The estimate for #rows seems to come from the LIMIT clause and is very wrong in this case.
The table is InnoDB (with extended keys). The index KEY(key1) is actually KEY(key1, id). The query has a restriction on key1 which makes it constant (tsubq.key1 = t1.a). After that, ORDER BY tsubq.id is achieved automatically.
The problem seems to be specifically with references to outside of subquery. If I use a constant instead, the query plan is able to use key1:
explain select (SELECT concat(id, '-', key1, '-', col1) FROM tsubq WHERE tsubq.key1 = 333 ORDER BY tsubq.id ASC LIMIT 1) from t1;
|
+------+-------------+-------+------+---------------+------+---------+-------+------+-------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|
+------+-------------+-------+------+---------------+------+---------+-------+------+-------------+
|
| 1 | PRIMARY | t1 | ALL | NULL | NULL | NULL | NULL | 1000 | |
|
| 2 | SUBQUERY | tsubq | ref | key1 | key1 | 5 | const | 999 | Using where |
|
+------+-------------+-------+------+---------------+------+---------+-------+------+-------------+
|
- relates to
-
MDEV-6081
ORDER BY+ref(const): selectivity is very incorrect (MySQL Bug#14338686)
-
-
Closed
{"report":{"fcp":1189.5999999046326,"ttfb":361.2999997138977,"pageVisibility":"visible","entityId":34610,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":0.5,"journeyId":"29035e8d-f349-40c1-9d70-910d7d05dcbb","navigationType":0,"readyForUser":1267.7999997138977,"redirectCount":0,"resourceLoadedEnd":1523.7999997138977,"resourceLoadedStart":366.7999997138977,"resourceTiming":[{"duration":330.80000019073486,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":366.7999997138977,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":366.7999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":697.5999999046326,"responseStart":0,"secureConnectionStart":0},{"duration":330.7999997138977,"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":367.09999990463257,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":367.09999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":697.8999996185303,"responseStart":0,"secureConnectionStart":0},{"duration":340,"initiatorType":"script","name":"https://jira.mariadb.org/s/0917945aaa57108d00c5076fea35e069-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":367.2999997138977,"connectEnd":367.2999997138977,"connectStart":367.2999997138977,"domainLookupEnd":367.2999997138977,"domainLookupStart":367.2999997138977,"fetchStart":367.2999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":367.2999997138977,"responseEnd":707.2999997138977,"responseStart":707.1999998092651,"secureConnectionStart":367.2999997138977},{"duration":401.90000009536743,"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":367.3999996185303,"connectEnd":367.3999996185303,"connectStart":367.3999996185303,"domainLookupEnd":367.3999996185303,"domainLookupStart":367.3999996185303,"fetchStart":367.3999996185303,"redirectEnd":0,"redirectStart":0,"requestStart":367.3999996185303,"responseEnd":769.2999997138977,"responseStart":769.2999997138977,"secureConnectionStart":367.3999996185303},{"duration":407.09999990463257,"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":367.69999980926514,"connectEnd":367.69999980926514,"connectStart":367.69999980926514,"domainLookupEnd":367.69999980926514,"domainLookupStart":367.69999980926514,"fetchStart":367.69999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":367.69999980926514,"responseEnd":774.7999997138977,"responseStart":774.7999997138977,"secureConnectionStart":367.69999980926514},{"duration":407.40000009536743,"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":367.7999997138977,"connectEnd":367.7999997138977,"connectStart":367.7999997138977,"domainLookupEnd":367.7999997138977,"domainLookupStart":367.7999997138977,"fetchStart":367.7999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":367.7999997138977,"responseEnd":775.1999998092651,"responseStart":775.1999998092651,"secureConnectionStart":367.7999997138977},{"duration":407.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":368.09999990463257,"connectEnd":368.09999990463257,"connectStart":368.09999990463257,"domainLookupEnd":368.09999990463257,"domainLookupStart":368.09999990463257,"fetchStart":368.09999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":368.09999990463257,"responseEnd":775.5,"responseStart":775.5,"secureConnectionStart":368.09999990463257},{"duration":504.30000019073486,"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":368.19999980926514,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":368.19999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":872.5,"responseStart":0,"secureConnectionStart":0},{"duration":407.6000003814697,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":368.3999996185303,"connectEnd":368.3999996185303,"connectStart":368.3999996185303,"domainLookupEnd":368.3999996185303,"domainLookupStart":368.3999996185303,"fetchStart":368.3999996185303,"redirectEnd":0,"redirectStart":0,"requestStart":368.3999996185303,"responseEnd":776,"responseStart":776,"secureConnectionStart":368.3999996185303},{"duration":504,"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":368.69999980926514,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":368.69999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":872.6999998092651,"responseStart":0,"secureConnectionStart":0},{"duration":407.90000009536743,"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":368.7999997138977,"connectEnd":368.7999997138977,"connectStart":368.7999997138977,"domainLookupEnd":368.7999997138977,"domainLookupStart":368.7999997138977,"fetchStart":368.7999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":368.7999997138977,"responseEnd":776.6999998092651,"responseStart":776.6999998092651,"secureConnectionStart":368.7999997138977},{"duration":997.5999999046326,"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":374.19999980926514,"connectEnd":374.19999980926514,"connectStart":374.19999980926514,"domainLookupEnd":374.19999980926514,"domainLookupStart":374.19999980926514,"fetchStart":374.19999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":374.19999980926514,"responseEnd":1371.7999997138977,"responseStart":1371.6999998092651,"secureConnectionStart":374.19999980926514},{"duration":1149.5999999046326,"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":374.19999980926514,"connectEnd":374.19999980926514,"connectStart":374.19999980926514,"domainLookupEnd":374.19999980926514,"domainLookupStart":374.19999980926514,"fetchStart":374.19999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":374.19999980926514,"responseEnd":1523.7999997138977,"responseStart":1523.7999997138977,"secureConnectionStart":374.19999980926514},{"duration":487.40000009536743,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":884.8999996185303,"connectEnd":884.8999996185303,"connectStart":884.8999996185303,"domainLookupEnd":884.8999996185303,"domainLookupStart":884.8999996185303,"fetchStart":884.8999996185303,"redirectEnd":0,"redirectStart":0,"requestStart":884.8999996185303,"responseEnd":1372.2999997138977,"responseStart":1372.2999997138977,"secureConnectionStart":884.8999996185303},{"duration":343.5,"initiatorType":"script","name":"https://www.google-analytics.com/analytics.js","startTime":1183.5,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":1183.5,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1527,"responseStart":0,"secureConnectionStart":0}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":197,"responseStart":361,"responseEnd":370,"domLoading":365,"domInteractive":1585,"domContentLoadedEventStart":1585,"domContentLoadedEventEnd":1631,"domComplete":2862,"loadEventStart":2862,"loadEventEnd":2864,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":1528},{"name":"bigPipe.sidebar-id.end","time":1528.7999997138977},{"name":"bigPipe.activity-panel-pipe-id.start","time":1528.8999996185303},{"name":"bigPipe.activity-panel-pipe-id.end","time":1531.5999999046326},{"name":"activityTabFullyLoaded","time":1637.8999996185303}],"measures":[],"correlationId":"f0edf0ba90d25d","effectiveType":"4g","downlink":10,"rtt":0,"serverDuration":103,"dbReadsTimeInMs":10,"dbConnsTimeInMs":19,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}