Type:
Bug
Priority:
Minor
Resolution:
Unresolved
Affects Version/s:
10.1(EOL) , 10.2(EOL) , 10.3(EOL) , 10.4(EOL) , 10.5 , 10.6 , 10.7(EOL) , 10.8(EOL) , 10.9(EOL) , 10.10(EOL)
The ORDER BY...LIMIT optimizer displays strange effects when one adds
histogram and enables use_condition_selectivity.
create table ten(a int );
insert into ten values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
create table one_k(a int );
insert into one_k select A.a + B.a* 10 + C.a * 100 from ten A, ten B, ten C;
create table ten_k(a int );
insert into ten_k select A.a + 1000 *B.a from one_k A, ten B;
create table t12 (
a int ,
b int ,
c int ,
filler1 char (255),
filler2 char (255),
key (a)
);
insert into t12 select a,a,a, a,a from ten_k;
With current default settings and @@optimizer_use_condition_selectivity=1:
mysql> explain extended select * from t12 where b < 5000;
+------+-------------+-------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+------+-------------+-------+------+---------------+------+---------+------+------+----------+-------------+
| 1 | SIMPLE | t12 | ALL | NULL | NULL | NULL | NULL | 9646 | 100.00 | Using where |
+------+-------------+-------+------+---------------+------+---------+------+------+----------+-------------+
Ok, filtered=100%, the optimizer has no clue about selectivity.
Now, the ORDER BY ... LIMIT query:
mysql> explain extended select * from t12 where b < 5000 order by a limit 600;
+------+-------------+-------+------+---------------+------+---------+------+------+----------+-----------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+------+-------------+-------+------+---------------+------+---------+------+------+----------+-----------------------------+
| 1 | SIMPLE | t12 | ALL | NULL | NULL | NULL | NULL | 9646 | 100.00 | Using where; Using filesort |
+------+-------------+-------+------+---------------+------+---------+------+------+----------+-----------------------------+
In order to read 600 rows it will use filesort. (if one uses a lower LIMIT value, e.g. "LIMIT 400", the optimizer will use the
index).
Now, let's give optimizer a clue about the condition selectivity:
set histogram_size=100;
set use_stat_tables=preferably;
set optimizer_use_condition_selectivity=4;
analyze table t12 persistent for columns(b) indexes ();
Now, the optimizer knows about condition selectivity:
mysql> explain extended select * from t12 where b < 5000 ;
+------+-------------+-------+------+---------------+------+---------+------+-------+----------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+------+-------------+-------+------+---------------+------+---------+------+-------+----------+-------------+
| 1 | SIMPLE | t12 | ALL | NULL | NULL | NULL | NULL | 10000 | 50.50 | Using where |
+------+-------------+-------+------+---------------+------+---------+------+-------+----------+-------------+
The query plan for the ORDER BY...LIMIT query becomes:
mysql> explain extended select * from t12 where b < 5000 order by a limit 600;
+------+-------------+-------+-------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+------+-------------+-------+-------+---------------+------+---------+------+------+----------+-------------+
| 1 | SIMPLE | t12 | index | NULL | a | 5 | NULL | 600 | 100.00 | Using where |
+------+-------------+-------+-------+---------------+------+---------+------+------+----------+-------------+
The odd parts about this are:
(Minor) filtered=100%, although the optimizer has information about the condition selectivity.
(Major) Why did the query plan change from using filesort to using an index?
selectivity=50% which means we'll need to scan 2x more rows before we find
#LIMIT matching rows.
{"report":{"fcp":754.5,"ttfb":220.40000000596046,"pageVisibility":"visible","entityId":70933,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":1,"journeyId":"ab4a3537-5f28-4ddc-af32-ff7c2f61bd75","navigationType":0,"readyForUser":841.1000000238419,"redirectCount":0,"resourceLoadedEnd":784.7000000178814,"resourceLoadedStart":226.10000002384186,"resourceTiming":[{"duration":7.699999988079071,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2bu7/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":226.10000002384186,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":226.10000002384186,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":233.80000001192093,"responseStart":0,"secureConnectionStart":0},{"duration":7.700000017881393,"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":226.40000000596046,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":226.40000000596046,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":234.10000002384186,"responseStart":0,"secureConnectionStart":0},{"duration":74.7000000178814,"initiatorType":"script","name":"https://jira.mariadb.org/s/fbf975c0cce4b1abf04784eeae9ba1f4-CDN/lu2bu7/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":226.5,"connectEnd":226.5,"connectStart":226.5,"domainLookupEnd":226.5,"domainLookupStart":226.5,"fetchStart":226.5,"redirectEnd":0,"redirectStart":0,"requestStart":226.5,"responseEnd":301.2000000178814,"responseStart":301.2000000178814,"secureConnectionStart":226.5},{"duration":147.69999998807907,"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":226.80000001192093,"connectEnd":226.80000001192093,"connectStart":226.80000001192093,"domainLookupEnd":226.80000001192093,"domainLookupStart":226.80000001192093,"fetchStart":226.80000001192093,"redirectEnd":0,"redirectStart":0,"requestStart":226.80000001192093,"responseEnd":374.5,"responseStart":374.5,"secureConnectionStart":226.80000001192093},{"duration":151.5,"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":227,"connectEnd":227,"connectStart":227,"domainLookupEnd":227,"domainLookupStart":227,"fetchStart":227,"redirectEnd":0,"redirectStart":0,"requestStart":227,"responseEnd":378.5,"responseStart":378.40000000596046,"secureConnectionStart":227},{"duration":151.69999998807907,"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":227.2000000178814,"connectEnd":227.2000000178814,"connectStart":227.2000000178814,"domainLookupEnd":227.2000000178814,"domainLookupStart":227.2000000178814,"fetchStart":227.2000000178814,"redirectEnd":0,"redirectStart":0,"requestStart":227.2000000178814,"responseEnd":378.90000000596046,"responseStart":378.90000000596046,"secureConnectionStart":227.2000000178814},{"duration":151.80000001192093,"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":227.40000000596046,"connectEnd":227.40000000596046,"connectStart":227.40000000596046,"domainLookupEnd":227.40000000596046,"domainLookupStart":227.40000000596046,"fetchStart":227.40000000596046,"redirectEnd":0,"redirectStart":0,"requestStart":227.40000000596046,"responseEnd":379.2000000178814,"responseStart":379.2000000178814,"secureConnectionStart":227.40000000596046},{"duration":213.90000000596046,"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":227.5,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":227.5,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":441.40000000596046,"responseStart":0,"secureConnectionStart":0},{"duration":152,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":227.7000000178814,"connectEnd":227.7000000178814,"connectStart":227.7000000178814,"domainLookupEnd":227.7000000178814,"domainLookupStart":227.7000000178814,"fetchStart":227.7000000178814,"redirectEnd":0,"redirectStart":0,"requestStart":227.7000000178814,"responseEnd":379.7000000178814,"responseStart":379.7000000178814,"secureConnectionStart":227.7000000178814},{"duration":213.69999998807907,"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":227.80000001192093,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":227.80000001192093,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":441.5,"responseStart":0,"secureConnectionStart":0},{"duration":152.30000001192093,"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":228,"connectEnd":228,"connectStart":228,"domainLookupEnd":228,"domainLookupStart":228,"fetchStart":228,"redirectEnd":0,"redirectStart":0,"requestStart":228,"responseEnd":380.30000001192093,"responseStart":380.30000001192093,"secureConnectionStart":228},{"duration":543.6999999880791,"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":229.10000002384186,"connectEnd":229.10000002384186,"connectStart":229.10000002384186,"domainLookupEnd":229.10000002384186,"domainLookupStart":229.10000002384186,"fetchStart":229.10000002384186,"redirectEnd":0,"redirectStart":0,"requestStart":229.10000002384186,"responseEnd":772.8000000119209,"responseStart":772.8000000119209,"secureConnectionStart":229.10000002384186},{"duration":555.5,"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":229.2000000178814,"connectEnd":229.2000000178814,"connectStart":229.2000000178814,"domainLookupEnd":229.2000000178814,"domainLookupStart":229.2000000178814,"fetchStart":229.2000000178814,"redirectEnd":0,"redirectStart":0,"requestStart":229.2000000178814,"responseEnd":784.7000000178814,"responseStart":784.7000000178814,"secureConnectionStart":229.2000000178814},{"duration":321.09999999403954,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":452.10000002384186,"connectEnd":452.10000002384186,"connectStart":452.10000002384186,"domainLookupEnd":452.10000002384186,"domainLookupStart":452.10000002384186,"fetchStart":452.10000002384186,"redirectEnd":0,"redirectStart":0,"requestStart":452.10000002384186,"responseEnd":773.2000000178814,"responseStart":773.2000000178814,"secureConnectionStart":452.10000002384186},{"duration":195.40000000596046,"initiatorType":"script","name":"https://www.google-analytics.com/analytics.js","startTime":748.4000000059605,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":748.4000000059605,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":943.8000000119209,"responseStart":0,"secureConnectionStart":0}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":18,"responseStart":221,"responseEnd":223,"domLoading":224,"domInteractive":977,"domContentLoadedEventStart":977,"domContentLoadedEventEnd":1020,"domComplete":1636,"loadEventStart":1636,"loadEventEnd":1638,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":951.9000000059605},{"name":"bigPipe.sidebar-id.end","time":953.6000000238419},{"name":"bigPipe.activity-panel-pipe-id.start","time":953.7000000178814},{"name":"bigPipe.activity-panel-pipe-id.end","time":955.1000000238419},{"name":"activityTabFullyLoaded","time":1034.2000000178814}],"measures":[],"correlationId":"755a62520bce2b","effectiveType":"4g","downlink":9.6,"rtt":0,"serverDuration":127,"dbReadsTimeInMs":31,"dbConnsTimeInMs":41,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}