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":855.3000000715256,"ttfb":230.80000007152557,"pageVisibility":"visible","entityId":34610,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":1,"journeyId":"d102b851-f00a-4673-89fc-fd7701bacc3d","navigationType":0,"readyForUser":917.4000000953674,"redirectCount":0,"resourceLoadedEnd":1178.2000000476837,"resourceLoadedStart":236.30000007152557,"resourceTiming":[{"duration":190,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2bu7/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":236.30000007152557,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":236.30000007152557,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":426.3000000715256,"responseStart":0,"secureConnectionStart":0},{"duration":189.90000009536743,"initiatorType":"link","name":"https://jira.mariadb.org/s/7ebd35e77e471bc30ff0eba799ebc151-CDN/lu2bu7/820016/12ta74/8679b4946efa1a0bb029a3a22206fb5d/_/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":236.5,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":236.5,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":426.40000009536743,"responseStart":0,"secureConnectionStart":0},{"duration":199.89999997615814,"initiatorType":"script","name":"https://jira.mariadb.org/s/fbf975c0cce4b1abf04784eeae9ba1f4-CDN/lu2bu7/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":236.60000002384186,"connectEnd":236.60000002384186,"connectStart":236.60000002384186,"domainLookupEnd":236.60000002384186,"domainLookupStart":236.60000002384186,"fetchStart":236.60000002384186,"redirectEnd":0,"redirectStart":0,"requestStart":236.60000002384186,"responseEnd":436.5,"responseStart":436.5,"secureConnectionStart":236.60000002384186},{"duration":279.2000000476837,"initiatorType":"script","name":"https://jira.mariadb.org/s/099b33461394b8015fc36c0a4b96e19f-CDN/lu2bu7/820016/12ta74/8679b4946efa1a0bb029a3a22206fb5d/_/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":236.70000004768372,"connectEnd":236.70000004768372,"connectStart":236.70000004768372,"domainLookupEnd":236.70000004768372,"domainLookupStart":236.70000004768372,"fetchStart":236.70000004768372,"redirectEnd":0,"redirectStart":0,"requestStart":236.70000004768372,"responseEnd":515.9000000953674,"responseStart":515.9000000953674,"secureConnectionStart":236.70000004768372},{"duration":282.60000002384186,"initiatorType":"script","name":"https://jira.mariadb.org/s/94c15bff32baef80f4096a08aceae8bc-CDN/lu2bu7/820016/12ta74/c92c0caa9a024ae85b0ebdbed7fb4bd7/_/download/contextbatch/js/atl.global,-_super/batch.js?locale=en","startTime":236.80000007152557,"connectEnd":236.80000007152557,"connectStart":236.80000007152557,"domainLookupEnd":236.80000007152557,"domainLookupStart":236.80000007152557,"fetchStart":236.80000007152557,"redirectEnd":0,"redirectStart":0,"requestStart":236.80000007152557,"responseEnd":519.4000000953674,"responseStart":519.4000000953674,"secureConnectionStart":236.80000007152557},{"duration":283,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bu7/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-en/jira.webresources:calendar-en.js","startTime":236.90000009536743,"connectEnd":236.90000009536743,"connectStart":236.90000009536743,"domainLookupEnd":236.90000009536743,"domainLookupStart":236.90000009536743,"fetchStart":236.90000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":236.90000009536743,"responseEnd":519.9000000953674,"responseStart":519.9000000953674,"secureConnectionStart":236.90000009536743},{"duration":283.3000000715256,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bu7/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-localisation-moment/jira.webresources:calendar-localisation-moment.js","startTime":237,"connectEnd":237,"connectStart":237,"domainLookupEnd":237,"domainLookupStart":237,"fetchStart":237,"redirectEnd":0,"redirectStart":0,"requestStart":237,"responseEnd":520.3000000715256,"responseStart":520.2000000476837,"secureConnectionStart":237},{"duration":283.5,"initiatorType":"link","name":"https://jira.mariadb.org/s/b04b06a02d1959df322d9cded3aeecc1-CDN/lu2bu7/820016/12ta74/a2ff6aa845ffc9a1d22fe23d9ee791fc/_/download/contextbatch/css/jira.global.look-and-feel,-_super/batch.css","startTime":237.10000002384186,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":237.10000002384186,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":520.6000000238419,"responseStart":0,"secureConnectionStart":0},{"duration":283.2999999523163,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":237.30000007152557,"connectEnd":237.30000007152557,"connectStart":237.30000007152557,"domainLookupEnd":237.30000007152557,"domainLookupStart":237.30000007152557,"fetchStart":237.30000007152557,"redirectEnd":0,"redirectStart":0,"requestStart":237.30000007152557,"responseEnd":520.6000000238419,"responseStart":520.5,"secureConnectionStart":237.30000007152557},{"duration":283.59999990463257,"initiatorType":"link","name":"https://jira.mariadb.org/s/3ac36323ba5e4eb0af2aa7ac7211b4bb-CDN/lu2bu7/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":237.40000009536743,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":237.40000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":521,"responseStart":0,"secureConnectionStart":0},{"duration":283.60000002384186,"initiatorType":"script","name":"https://jira.mariadb.org/s/3339d87fa2538a859872f2df449bf8d0-CDN/lu2bu7/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":237.5,"connectEnd":237.5,"connectStart":237.5,"domainLookupEnd":237.5,"domainLookupStart":237.5,"fetchStart":237.5,"redirectEnd":0,"redirectStart":0,"requestStart":237.5,"responseEnd":521.1000000238419,"responseStart":521.1000000238419,"secureConnectionStart":237.5},{"duration":546.5,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bu7/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-js/jira.webresources:bigpipe-js.js","startTime":238.20000004768372,"connectEnd":238.20000004768372,"connectStart":238.20000004768372,"domainLookupEnd":238.20000004768372,"domainLookupStart":238.20000004768372,"fetchStart":238.20000004768372,"redirectEnd":0,"redirectStart":0,"requestStart":238.20000004768372,"responseEnd":784.7000000476837,"responseStart":784.7000000476837,"secureConnectionStart":238.20000004768372},{"duration":935.2999999523163,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bu7/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-init/jira.webresources:bigpipe-init.js","startTime":242.90000009536743,"connectEnd":242.90000009536743,"connectStart":242.90000009536743,"domainLookupEnd":242.90000009536743,"domainLookupStart":242.90000009536743,"fetchStart":242.90000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":242.90000009536743,"responseEnd":1178.2000000476837,"responseStart":1178.2000000476837,"secureConnectionStart":242.90000009536743},{"duration":479.10000002384186,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":656.6000000238419,"connectEnd":656.6000000238419,"connectStart":656.6000000238419,"domainLookupEnd":656.6000000238419,"domainLookupStart":656.6000000238419,"fetchStart":656.6000000238419,"redirectEnd":0,"redirectStart":0,"requestStart":656.6000000238419,"responseEnd":1135.7000000476837,"responseStart":1135.6000000238419,"secureConnectionStart":656.6000000238419},{"duration":359.3000000715256,"initiatorType":"script","name":"https://www.google-analytics.com/analytics.js","startTime":833.1000000238419,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":833.1000000238419,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1192.4000000953674,"responseStart":0,"secureConnectionStart":0}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":27,"responseStart":231,"responseEnd":240,"domLoading":234,"domInteractive":1206,"domContentLoadedEventStart":1206,"domContentLoadedEventEnd":1246,"domComplete":1600,"loadEventStart":1600,"loadEventEnd":1601,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":1181.9000000953674},{"name":"bigPipe.sidebar-id.end","time":1182.7000000476837},{"name":"bigPipe.activity-panel-pipe-id.start","time":1182.8000000715256},{"name":"bigPipe.activity-panel-pipe-id.end","time":1185.1000000238419},{"name":"activityTabFullyLoaded","time":1263}],"measures":[],"correlationId":"cd5a0c80a83ce5","effectiveType":"4g","downlink":10,"rtt":0,"serverDuration":131,"dbReadsTimeInMs":13,"dbConnsTimeInMs":23,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}
Initial investigation:
The optimizer fails to detect that sorting is not needed, because TABLE::const_key_parts[ key1] = 0.
I'm wondering if the fix is to make const_key_parts=1 for cases like this.
TABLE::const_key_parts is modified by
Checked by
Suggestion: change the meaning of "const_key_parts" to be "including
OUTER_REF_TABLE_BIT".