The dataset used
create table t0 (a int);
|
INSERT INTO t0 VALUES (0),(0),(0),(0),(2),(0),(0),(1),(1),(0);
|
|
CREATE TABLE t1 (
|
a int(11) DEFAULT NULL,
|
b int(11) DEFAULT NULL,
|
d int(11) DEFAULT NULL,
|
KEY b_3 (a,d),
|
KEY b_4 (a,b)
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
insert into t1 select A.a , B.a, C.a from t0 A, t0 B, t0 C, t0 D;
|
MariaDB [test]> select count(*) from t1;
|
+----------+
|
| count(*) |
|
+----------+
|
| 10000 |
|
+----------+
|
Now running the query without any limit
MariaDB [test]> analyze select a,b,d from t1 where a=1 and d=2 order by b;
|
+------+-------------+-------+------+---------------+------+---------+-------------+------+--------+----------+------------+-----------------------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | r_rows | filtered | r_filtered | Extra |
|
+------+-------------+-------+------+---------------+------+---------+-------------+------+--------+----------+------------+-----------------------------+
|
| 1 | SIMPLE | t1 | ref | b_3,b_4 | b_3 | 10 | const,const | 200 | 200.00 | 100.00 | 100.00 | Using where; Using filesort |
|
+------+-------------+-------+------+---------------+------+---------+-------------+------+--------+----------+------------+-----------------------------+
|
Now running the query with limit= NUMBER OF TABLE RECORDS
MariaDB [test]> analyze select a,b,d from t1 where a=1 and d=2 order by b limit 10000;
|
+------+-------------+-------+-------+---------------+------+---------+------+------+---------+----------+------------+-------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | r_rows | filtered | r_filtered | Extra |
|
+------+-------------+-------+-------+---------------+------+---------+------+------+---------+----------+------------+-------------+
|
| 1 | SIMPLE | t1 | range | b_3,b_4 | b_4 | 5 | NULL | 2000 | 2000.00 | 10.00 | 10.00 | Using where |
|
+------+-------------+-------+-------+---------------+------+---------+------+------+---------+----------+------------+-------------+
|
This looks incorrect, we definitely have a problem with the cost model that changes ref access -> index(or range) by which we can do the ORDERING.
With limit = 2x table_records
MariaDB [test]> analyze select a,b,d from t1 where a=1 and d=2 order by b limit 20000;
|
+------+-------------+-------+-------+---------------+------+---------+-------------+-------+----------+----------+------------+-------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | r_rows | filtered | r_filtered | Extra |
|
+------+-------------+-------+-------+---------------+------+---------+-------------+-------+----------+----------+------------+-------------+
|
| 1 | SIMPLE | t1 | index | b_3,b_4 | b_4 | 10 | const,const | 10266 | 10000.00 | 1.95 | 2.00 | Using where |
|
+------+-------------+-------+-------+---------------+------+---------+-------------+-------+----------+----------+------------+-------------+
|
{"report":{"fcp":1006.8999999761581,"ttfb":222.59999999403954,"pageVisibility":"visible","entityId":71597,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":0.5,"journeyId":"db9a9cc6-b1aa-4de2-92fc-473e9b27a2ba","navigationType":0,"readyForUser":1084.0999999940395,"redirectCount":0,"resourceLoadedEnd":1035.3999999761581,"resourceLoadedStart":228,"resourceTiming":[{"duration":334.59999999403954,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2bv2/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":228,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":228,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":562.5999999940395,"responseStart":0,"secureConnectionStart":0},{"duration":334.40000000596046,"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":228.39999997615814,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":228.39999997615814,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":562.7999999821186,"responseStart":0,"secureConnectionStart":0},{"duration":343,"initiatorType":"script","name":"https://jira.mariadb.org/s/e9b27a47da5fb0f74a35acd57e9847fb-CDN/lu2bv2/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":228.5,"connectEnd":228.5,"connectStart":228.5,"domainLookupEnd":228.5,"domainLookupStart":228.5,"fetchStart":228.5,"redirectEnd":0,"redirectStart":0,"requestStart":228.5,"responseEnd":571.5,"responseStart":571.5,"secureConnectionStart":228.5},{"duration":380.80000001192093,"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":228.69999998807907,"connectEnd":228.69999998807907,"connectStart":228.69999998807907,"domainLookupEnd":228.69999998807907,"domainLookupStart":228.69999998807907,"fetchStart":228.69999998807907,"redirectEnd":0,"redirectStart":0,"requestStart":228.69999998807907,"responseEnd":609.5,"responseStart":609.5,"secureConnectionStart":228.69999998807907},{"duration":383.80000001192093,"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":228.89999997615814,"connectEnd":228.89999997615814,"connectStart":228.89999997615814,"domainLookupEnd":228.89999997615814,"domainLookupStart":228.89999997615814,"fetchStart":228.89999997615814,"redirectEnd":0,"redirectStart":0,"requestStart":228.89999997615814,"responseEnd":612.6999999880791,"responseStart":612.6999999880791,"secureConnectionStart":228.89999997615814},{"duration":384.09999999403954,"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":229.09999999403954,"connectEnd":229.09999999403954,"connectStart":229.09999999403954,"domainLookupEnd":229.09999999403954,"domainLookupStart":229.09999999403954,"fetchStart":229.09999999403954,"redirectEnd":0,"redirectStart":0,"requestStart":229.09999999403954,"responseEnd":613.1999999880791,"responseStart":613.1999999880791,"secureConnectionStart":229.09999999403954},{"duration":384.2000000178814,"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":229.2999999821186,"connectEnd":229.2999999821186,"connectStart":229.2999999821186,"domainLookupEnd":229.2999999821186,"domainLookupStart":229.2999999821186,"fetchStart":229.2999999821186,"redirectEnd":0,"redirectStart":0,"requestStart":229.2999999821186,"responseEnd":613.5,"responseStart":613.5,"secureConnectionStart":229.2999999821186},{"duration":477.2000000178814,"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":229.39999997615814,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":229.39999997615814,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":706.5999999940395,"responseStart":0,"secureConnectionStart":0},{"duration":384.2999999821186,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":229.59999999403954,"connectEnd":229.59999999403954,"connectStart":229.59999999403954,"domainLookupEnd":229.59999999403954,"domainLookupStart":229.59999999403954,"fetchStart":229.59999999403954,"redirectEnd":0,"redirectStart":0,"requestStart":229.59999999403954,"responseEnd":613.8999999761581,"responseStart":613.8999999761581,"secureConnectionStart":229.59999999403954},{"duration":477,"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":229.69999998807907,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":229.69999998807907,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":706.6999999880791,"responseStart":0,"secureConnectionStart":0},{"duration":384.5,"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":229.89999997615814,"connectEnd":229.89999997615814,"connectStart":229.89999997615814,"domainLookupEnd":229.89999997615814,"domainLookupStart":229.89999997615814,"fetchStart":229.89999997615814,"redirectEnd":0,"redirectStart":0,"requestStart":229.89999997615814,"responseEnd":614.3999999761581,"responseStart":614.3999999761581,"secureConnectionStart":229.89999997615814},{"duration":541.2000000178814,"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":230.89999997615814,"connectEnd":230.89999997615814,"connectStart":230.89999997615814,"domainLookupEnd":230.89999997615814,"domainLookupStart":230.89999997615814,"fetchStart":230.89999997615814,"redirectEnd":0,"redirectStart":0,"requestStart":230.89999997615814,"responseEnd":772.0999999940395,"responseStart":772.0999999940395,"secureConnectionStart":230.89999997615814},{"duration":804.3999999761581,"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":231,"connectEnd":231,"connectStart":231,"domainLookupEnd":231,"domainLookupStart":231,"fetchStart":231,"redirectEnd":0,"redirectStart":0,"requestStart":231,"responseEnd":1035.3999999761581,"responseStart":1035.3999999761581,"secureConnectionStart":231},{"duration":91.19999998807907,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":717.1999999880791,"connectEnd":717.1999999880791,"connectStart":717.1999999880791,"domainLookupEnd":717.1999999880791,"domainLookupStart":717.1999999880791,"fetchStart":717.1999999880791,"redirectEnd":0,"redirectStart":0,"requestStart":717.1999999880791,"responseEnd":808.3999999761581,"responseStart":808.3999999761581,"secureConnectionStart":717.1999999880791}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":34,"responseStart":223,"responseEnd":225,"domLoading":226,"domInteractive":1133,"domContentLoadedEventStart":1133,"domContentLoadedEventEnd":1169,"domComplete":2260,"loadEventStart":2260,"loadEventEnd":2262,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":1122.699999988079},{"name":"bigPipe.sidebar-id.end","time":1123.3999999761581},{"name":"bigPipe.activity-panel-pipe-id.start","time":1123.5},{"name":"bigPipe.activity-panel-pipe-id.end","time":1125.7999999821186},{"name":"activityTabFullyLoaded","time":1179.2999999821186}],"measures":[],"correlationId":"139078b02eaf27","effectiveType":"4g","downlink":9.3,"rtt":0,"serverDuration":108,"dbReadsTimeInMs":16,"dbConnsTimeInMs":25,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}