Consider a testcase:
create table t1 (a int);
|
insert into t1 select seq from seq_1_to_100;
|
|
create table t2 (
|
kp1 int,
|
kp2 int,
|
filler char(100),
|
key(kp1, kp2)
|
);
|
insert into t2
|
select
|
seq, seq,
|
'filler-data'
|
from seq_1_to_10000;
|
Suppose table t2 has
- a ref(non-const) access
- a range access that uses more keyparts than ref:
Query Q1:
explain
|
select *
|
from t1, t2
|
where
|
t2.kp1=t1.a and t2.kp1<=100 and t2.kp2<=20
|
The query plan is:
+------+-------------+-------+-------+---------------+------+---------+------+------+------------------------------------------------------------------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|
+------+-------------+-------+-------+---------------+------+---------+------+------+------------------------------------------------------------------------+
|
| 1 | SIMPLE | t1 | ALL | NULL | NULL | NULL | NULL | 100 | Using where |
|
| 1 | SIMPLE | t2 | range | kp1 | kp1 | 10 | NULL | 999 | Using index condition; Using where; Using join buffer (flat, BNL join) |
|
+------+-------------+-------+-------+---------------+------+---------+------+------+------------------------------------------------------------------------+
|
One can get a much better if one prevents the optimizer from constructing range access with two keyparts:
Query Q2:
explain
|
select *
|
from t1, t2
|
where
|
t2.kp1=t1.a and t2.kp1<=1000 and t2.kp2+1<=20; -- make kp2 unusable for range.
|
+------+-------------+-------+------+---------------+------+---------+-----------+------+-----------------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|
+------+-------------+-------+------+---------------+------+---------+-----------+------+-----------------------+
|
| 1 | SIMPLE | t1 | ALL | NULL | NULL | NULL | NULL | 100 | Using where |
|
| 1 | SIMPLE | t2 | ref | kp1 | kp1 | 5 | test.t1.a | 1 | Using index condition |
|
+------+-------------+-------+------+---------------+------+---------+-----------+------+-----------------------+
|
Examining the optimizer trace for Q1, one can see that the optimizer picks ref access for table t2, but then the rewrite makes it range access:
"attached_conditions_computation": [
|
{
|
"ref_to_range": true,
|
"cause": "range uses longer key"
|
}
|
],
|
{"report":{"fcp":1040,"ttfb":274.1000003814697,"pageVisibility":"visible","entityId":131242,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":0.5,"journeyId":"1a90d4db-9ce0-4c7c-a79d-8a83eb6da48f","navigationType":0,"readyForUser":1125.6000003814697,"redirectCount":0,"resourceLoadedEnd":727.1000003814697,"resourceLoadedStart":282.9000005722046,"resourceTiming":[{"duration":53.69999980926514,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":282.9000005722046,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":282.9000005722046,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":336.6000003814697,"responseStart":0,"secureConnectionStart":0},{"duration":53.69999980926514,"initiatorType":"link","name":"https://jira.mariadb.org/s/7ebd35e77e471bc30ff0eba799ebc151-CDN/lu2cib/820016/12ta74/2bf333562ca6724060a9d5f1535471f6/_/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":283.30000019073486,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":283.30000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":337,"responseStart":0,"secureConnectionStart":0},{"duration":292.80000019073486,"initiatorType":"script","name":"https://jira.mariadb.org/s/0917945aaa57108d00c5076fea35e069-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":283.5,"connectEnd":283.5,"connectStart":283.5,"domainLookupEnd":283.5,"domainLookupStart":283.5,"fetchStart":283.5,"redirectEnd":0,"redirectStart":0,"requestStart":338.30000019073486,"responseEnd":576.3000001907349,"responseStart":350.80000019073486,"secureConnectionStart":283.5},{"duration":443,"initiatorType":"script","name":"https://jira.mariadb.org/s/2d8175ec2fa4c816e8023260bd8c1786-CDN/lu2cib/820016/12ta74/2bf333562ca6724060a9d5f1535471f6/_/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":284.1000003814697,"connectEnd":284.1000003814697,"connectStart":284.1000003814697,"domainLookupEnd":284.1000003814697,"domainLookupStart":284.1000003814697,"fetchStart":284.1000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":338.6000003814697,"responseEnd":727.1000003814697,"responseStart":356.6000003814697,"secureConnectionStart":284.1000003814697},{"duration":78.39999961853027,"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":284.30000019073486,"connectEnd":284.30000019073486,"connectStart":284.30000019073486,"domainLookupEnd":284.30000019073486,"domainLookupStart":284.30000019073486,"fetchStart":284.30000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":339.6000003814697,"responseEnd":362.69999980926514,"responseStart":359.6000003814697,"secureConnectionStart":284.30000019073486},{"duration":80.10000038146973,"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":284.5,"connectEnd":284.5,"connectStart":284.5,"domainLookupEnd":284.5,"domainLookupStart":284.5,"fetchStart":284.5,"redirectEnd":0,"redirectStart":0,"requestStart":342.4000005722046,"responseEnd":364.6000003814697,"responseStart":361,"secureConnectionStart":284.5},{"duration":81.39999961853027,"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":284.6000003814697,"connectEnd":284.6000003814697,"connectStart":284.6000003814697,"domainLookupEnd":284.6000003814697,"domainLookupStart":284.6000003814697,"fetchStart":284.6000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":342.9000005722046,"responseEnd":366,"responseStart":362.9000005722046,"secureConnectionStart":284.6000003814697},{"duration":56.69999980926514,"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":284.9000005722046,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":284.9000005722046,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":341.6000003814697,"responseStart":0,"secureConnectionStart":0},{"duration":88.40000057220459,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":285,"connectEnd":285,"connectStart":285,"domainLookupEnd":285,"domainLookupStart":285,"fetchStart":285,"redirectEnd":0,"redirectStart":0,"requestStart":347.4000005722046,"responseEnd":373.4000005722046,"responseStart":368.1000003814697,"secureConnectionStart":285},{"duration":60.39999961853027,"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":285.1000003814697,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":285.1000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":345.5,"responseStart":0,"secureConnectionStart":0},{"duration":90.39999961853027,"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":285.30000019073486,"connectEnd":285.30000019073486,"connectStart":285.30000019073486,"domainLookupEnd":285.30000019073486,"domainLookupStart":285.30000019073486,"fetchStart":285.30000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":348.6000003814697,"responseEnd":375.69999980926514,"responseStart":370.30000019073486,"secureConnectionStart":285.30000019073486},{"duration":430.3999996185303,"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":289.1000003814697,"connectEnd":289.1000003814697,"connectStart":289.1000003814697,"domainLookupEnd":289.1000003814697,"domainLookupStart":289.1000003814697,"fetchStart":289.1000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":431.30000019073486,"responseEnd":719.5,"responseStart":716,"secureConnectionStart":289.1000003814697},{"duration":434.19999980926514,"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":289.1000003814697,"connectEnd":289.1000003814697,"connectStart":289.1000003814697,"domainLookupEnd":289.1000003814697,"domainLookupStart":289.1000003814697,"fetchStart":289.1000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":486.4000005722046,"responseEnd":723.3000001907349,"responseStart":719.8000001907349,"secureConnectionStart":289.1000003814697},{"duration":98,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":708.1999998092651,"connectEnd":708.1999998092651,"connectStart":708.1999998092651,"domainLookupEnd":708.1999998092651,"domainLookupStart":708.1999998092651,"fetchStart":708.1999998092651,"redirectEnd":0,"redirectStart":0,"requestStart":773.9000005722046,"responseEnd":806.1999998092651,"responseStart":805.1999998092651,"secureConnectionStart":708.1999998092651},{"duration":196.4000005722046,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":952,"connectEnd":952,"connectStart":952,"domainLookupEnd":952,"domainLookupStart":952,"fetchStart":952,"redirectEnd":0,"redirectStart":0,"requestStart":1117.6999998092651,"responseEnd":1148.4000005722046,"responseStart":1147.4000005722046,"secureConnectionStart":952}],"fetchStart":0,"domainLookupStart":79,"domainLookupEnd":87,"connectStart":87,"connectEnd":105,"secureConnectionStart":95,"requestStart":106,"responseStart":274,"responseEnd":289,"domLoading":278,"domInteractive":1190,"domContentLoadedEventStart":1190,"domContentLoadedEventEnd":1247,"domComplete":1428,"loadEventStart":1428,"loadEventEnd":1428,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":1168.4000005722046},{"name":"bigPipe.sidebar-id.end","time":1169.1999998092651},{"name":"bigPipe.activity-panel-pipe-id.start","time":1169.4000005722046},{"name":"bigPipe.activity-panel-pipe-id.end","time":1171},{"name":"activityTabFullyLoaded","time":1262.5}],"measures":[],"correlationId":"6963ab999486b4","effectiveType":"4g","downlink":9.9,"rtt":0,"serverDuration":96,"dbReadsTimeInMs":10,"dbConnsTimeInMs":18,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}
commit 3a38d99cbd03893f9be0b6d14fcfe1d98b89e395 (HEAD -> bb-10.6-MDEV-35180, origin/bb-10.6-MDEV-35180)
Author: Sergei Petrunia <sergey@mariadb.com>
Date: Wed Oct 16 18:35:37 2024 +0300
MDEV-35180: ref_to_range rewrite causes poor query plan
(Variant 2: only allow rewrite for ref(const))