InnoDB's records_in_range estimates seem to be capped at ~50% of the table. (We used to observe this on various occasions before but I haven't been able to find an MDEV for this).
If I pass a range that contains a bigger fraction of the table, the estimated number of rows is still around 50% of the total rows in the table.
Test dataset:
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 t1 (
|
a int,
|
b varchar(32),
|
c int,
|
key(a),
|
key(b)
|
) engine=innodb;
|
# 100K NULLs
|
insert into t1 select
|
null,
|
null,
|
A.a + 1000*B.a
|
from
|
one_k A,
|
one_k B
|
where
|
B.a<100;
|
|
# 900 K non-NULLs
|
insert into t1 select
|
A.a + 1000*B.a,
|
A.a + 1000*B.a,
|
A.a + 1000*B.a
|
from
|
one_k A,
|
one_k B
|
where
|
B.a>=100;
|
Now, both a IS NOT NULL or b IS NOT NULL match 900K rows (90% of the table).
But EXPLAIN will show the estimates of about 500K rows, which is 50% of the table:
explain select * from t1 force index (a) where a is not null ;
|
+------+-------------+-------+-------+---------------+------+---------+------+--------+-----------------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|
+------+-------------+-------+-------+---------------+------+---------+------+--------+-----------------------+
|
| 1 | SIMPLE | t1 | range | a | a | 5 | NULL | 494308 | Using index condition |
|
+------+-------------+-------+-------+---------------+------+---------+------+--------+-----------------------+
|
explain select * from t1 force index (b) where b is not null ;
|
+------+-------------+-------+-------+---------------+------+---------+------+--------+-----------------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|
+------+-------------+-------+-------+---------------+------+---------+------+--------+-----------------------+
|
| 1 | SIMPLE | t1 | range | b | b | 35 | NULL | 494308 | Using index condition |
|
+------+-------------+-------+-------+---------------+------+---------+------+--------+-----------------------+
|
When the optimizer was simple, this property was not a problem.
A simple optimizer would only use range estimates to construct range access. Range access is cheaper than full table if it covers about 30% of the table. Returning 50% of the table instead of 90% was not an issue.
A more advanced optimizer also attempts to use range estimates for condition selectivity, etc. Here, returning 50% selectivity instead of 90% is a problem. (One must take into account that selectivity is computed for multiple indexes. For example, for 5 indexes 0.5^2= 1/32 . 32x under-estimation of selectivity)
{"report":{"fcp":2405.5,"ttfb":1589.5999999046326,"pageVisibility":"visible","entityId":75930,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":32,"apdex":0.5,"journeyId":"0c03e14f-0b47-458b-9326-f69bfbb98793","navigationType":0,"readyForUser":2538.8999996185303,"redirectCount":0,"resourceLoadedEnd":2586.7999997138977,"resourceLoadedStart":1595.5,"resourceTiming":[{"duration":202.39999961853027,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":1595.5,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":1595.5,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1797.8999996185303,"responseStart":0,"secureConnectionStart":0},{"duration":203.7000002861023,"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":1595.7999997138977,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":1595.7999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1799.5,"responseStart":0,"secureConnectionStart":0},{"duration":290.09999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/s/0917945aaa57108d00c5076fea35e069-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":1596,"connectEnd":1596,"connectStart":1596,"domainLookupEnd":1596,"domainLookupStart":1596,"fetchStart":1596,"redirectEnd":0,"redirectStart":0,"requestStart":1596,"responseEnd":1886.0999999046326,"responseStart":1886.0999999046326,"secureConnectionStart":1596},{"duration":435.59999990463257,"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":1596.1999998092651,"connectEnd":1596.1999998092651,"connectStart":1596.1999998092651,"domainLookupEnd":1596.1999998092651,"domainLookupStart":1596.1999998092651,"fetchStart":1596.1999998092651,"redirectEnd":0,"redirectStart":0,"requestStart":1596.1999998092651,"responseEnd":2031.7999997138977,"responseStart":2031.7999997138977,"secureConnectionStart":1596.1999998092651},{"duration":438.90000009536743,"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":1596.2999997138977,"connectEnd":1596.2999997138977,"connectStart":1596.2999997138977,"domainLookupEnd":1596.2999997138977,"domainLookupStart":1596.2999997138977,"fetchStart":1596.2999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":1596.2999997138977,"responseEnd":2035.1999998092651,"responseStart":2035.1999998092651,"secureConnectionStart":1596.2999997138977},{"duration":439.5,"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":1596.5,"connectEnd":1596.5,"connectStart":1596.5,"domainLookupEnd":1596.5,"domainLookupStart":1596.5,"fetchStart":1596.5,"redirectEnd":0,"redirectStart":0,"requestStart":1596.5,"responseEnd":2036,"responseStart":2036,"secureConnectionStart":1596.5},{"duration":439.80000019073486,"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":1596.6999998092651,"connectEnd":1596.6999998092651,"connectStart":1596.6999998092651,"domainLookupEnd":1596.6999998092651,"domainLookupStart":1596.6999998092651,"fetchStart":1596.6999998092651,"redirectEnd":0,"redirectStart":0,"requestStart":1596.6999998092651,"responseEnd":2036.5,"responseStart":2036.5,"secureConnectionStart":1596.6999998092651},{"duration":450.7999997138977,"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":1597,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":1597,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":2047.7999997138977,"responseStart":0,"secureConnectionStart":0},{"duration":440.59999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":1597.0999999046326,"connectEnd":1597.0999999046326,"connectStart":1597.0999999046326,"domainLookupEnd":1597.0999999046326,"domainLookupStart":1597.0999999046326,"fetchStart":1597.0999999046326,"redirectEnd":0,"redirectStart":0,"requestStart":1597.0999999046326,"responseEnd":2037.6999998092651,"responseStart":2037.6999998092651,"secureConnectionStart":1597.0999999046326},{"duration":450.90000009536743,"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":1597.2999997138977,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":1597.2999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":2048.199999809265,"responseStart":0,"secureConnectionStart":0},{"duration":441.5,"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":1597.3999996185303,"connectEnd":1597.3999996185303,"connectStart":1597.3999996185303,"domainLookupEnd":1597.3999996185303,"domainLookupStart":1597.3999996185303,"fetchStart":1597.3999996185303,"redirectEnd":0,"redirectStart":0,"requestStart":1597.3999996185303,"responseEnd":2038.8999996185303,"responseStart":2038.8999996185303,"secureConnectionStart":1597.3999996185303},{"duration":674.8000001907349,"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":1603.1999998092651,"connectEnd":1603.1999998092651,"connectStart":1603.1999998092651,"domainLookupEnd":1603.1999998092651,"domainLookupStart":1603.1999998092651,"fetchStart":1603.1999998092651,"redirectEnd":0,"redirectStart":0,"requestStart":1603.1999998092651,"responseEnd":2278,"responseStart":2278,"secureConnectionStart":1603.1999998092651},{"duration":897.1999998092651,"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":1603.1999998092651,"connectEnd":1603.1999998092651,"connectStart":1603.1999998092651,"domainLookupEnd":1603.1999998092651,"domainLookupStart":1603.1999998092651,"fetchStart":1603.1999998092651,"redirectEnd":0,"redirectStart":0,"requestStart":1603.1999998092651,"responseEnd":2500.3999996185303,"responseStart":2500.3999996185303,"secureConnectionStart":1603.1999998092651},{"duration":206.40000009536743,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":2072.0999999046326,"connectEnd":2072.0999999046326,"connectStart":2072.0999999046326,"domainLookupEnd":2072.0999999046326,"domainLookupStart":2072.0999999046326,"fetchStart":2072.0999999046326,"redirectEnd":0,"redirectStart":0,"requestStart":2072.0999999046326,"responseEnd":2278.5,"responseStart":2278.5,"secureConnectionStart":2072.0999999046326},{"duration":251.40000009536743,"initiatorType":"link","name":"https://jira.mariadb.org/s/d5715adaadd168a9002b108b2b039b50-CDN/lu2cib/820016/12ta74/be4b45e9cec53099498fa61c8b7acba4/_/download/contextbatch/css/jira.project.sidebar,-_super,-project.issue.navigator,-jira.general,-jira.browse.project,-jira.view.issue,-jira.global,-atl.general,-com.atlassian.jira.projects.sidebar.init/batch.css?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&slack-enabled=true&whisper-enabled=true","startTime":2335.3999996185303,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":2335.3999996185303,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":2586.7999997138977,"responseStart":0,"secureConnectionStart":0},{"duration":239.59999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/e65b778d185daf5aee24936755b43da6/_/download/contextbatch/js/browser-metrics-plugin.contrib,-_super,-project.issue.navigator,-jira.view.issue,-atl.general/batch.js?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&slack-enabled=true&whisper-enabled=true","startTime":2336.2999997138977,"connectEnd":2336.2999997138977,"connectStart":2336.2999997138977,"domainLookupEnd":2336.2999997138977,"domainLookupStart":2336.2999997138977,"fetchStart":2336.2999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":2336.2999997138977,"responseEnd":2575.8999996185303,"responseStart":2575.8999996185303,"secureConnectionStart":2336.2999997138977},{"duration":244.09999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/s/097ae97cb8fbec7d6ea4bbb1f26955b9-CDN/lu2cib/820016/12ta74/be4b45e9cec53099498fa61c8b7acba4/_/download/contextbatch/js/jira.project.sidebar,-_super,-project.issue.navigator,-jira.general,-jira.browse.project,-jira.view.issue,-jira.global,-atl.general,-com.atlassian.jira.projects.sidebar.init/batch.js?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&locale=en&slack-enabled=true&whisper-enabled=true","startTime":2336.5999999046326,"connectEnd":2336.5999999046326,"connectStart":2336.5999999046326,"domainLookupEnd":2336.5999999046326,"domainLookupStart":2336.5999999046326,"fetchStart":2336.5999999046326,"redirectEnd":0,"redirectStart":0,"requestStart":2336.5999999046326,"responseEnd":2580.699999809265,"responseStart":2580.699999809265,"secureConnectionStart":2336.5999999046326},{"duration":310.7999997138977,"initiatorType":"script","name":"https://www.google-analytics.com/analytics.js","startTime":2398,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":2398,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":2708.7999997138977,"responseStart":0,"secureConnectionStart":0}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":1034,"responseStart":1590,"responseEnd":1593,"domLoading":1593,"domInteractive":2625,"domContentLoadedEventStart":2625,"domContentLoadedEventEnd":2687,"domComplete":2953,"loadEventStart":2953,"loadEventEnd":2954,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":2608.8999996185303},{"name":"bigPipe.sidebar-id.end","time":2609.699999809265},{"name":"bigPipe.activity-panel-pipe-id.start","time":2609.7999997138977},{"name":"bigPipe.activity-panel-pipe-id.end","time":2611.699999809265},{"name":"activityTabFullyLoaded","time":2735.2999997138977}],"measures":[],"correlationId":"8ecc17628698ed","effectiveType":"4g","downlink":9,"rtt":0,"serverDuration":115,"dbReadsTimeInMs":17,"dbConnsTimeInMs":27,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}
According to marko, this part of InnoDB code has not been touched by anyone for a very long time.