Create the 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 t1p (
|
pk int primary key,
|
removed enum('true') DEFAULT NULL,
|
key(removed)
|
) engine=innodb pack_keys=1;
|
|
# This inserts 10M records:
|
insert into t1p
|
select
|
A.a + 1000*B.a+1000*1000*C.a,
|
IF(A.a+1000*B.a > 100, NULL, 'true')
|
from one_k A, one_k B,ten C;
|
Let's explore the dataset
MariaDB [test2]> explain select count(*) from t1p force index(removed) where removed is null;
|
+------+-------------+-------+------+---------------+---------+---------+-------+---------+--------------------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|
+------+-------------+-------+------+---------------+---------+---------+-------+---------+--------------------------+
|
| 1 | SIMPLE | t1p | ref | removed | removed | 2 | const | 5003505 | Using where; Using index |
|
+------+-------------+-------+------+---------------+---------+---------+-------+---------+--------------------------+
|
Here, it underestimates the amount of NULLs (by about two times). This is not a problem, yet.
MariaDB [test2]> explain select count(*) from t1p force index(removed) where removed is not null;
|
+------+-------------+-------+-------+---------------+---------+---------+------+------+--------------------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|
+------+-------------+-------+-------+---------------+---------+---------+------+------+--------------------------+
|
| 1 | SIMPLE | t1p | range | removed | removed | 2 | NULL | 1009 | Using where; Using index |
|
+------+-------------+-------+-------+---------------+---------+---------+------+------+--------------------------+
|
This is close to reality.
MariaDB [test2]> explain
|
-> select * from ten left join t1p on ten.a=3 and t1p.removed is null;
|
+------+-------------+-------+------+---------------+---------+---------+-------+------+--------------------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|
+------+-------------+-------+------+---------------+---------+---------+-------+------+--------------------------+
|
| 1 | SIMPLE | ten | ALL | NULL | NULL | NULL | NULL | 10 | |
|
| 1 | SIMPLE | t1p | ref | removed | removed | 2 | const | 10 | Using where; Using index |
|
+------+-------------+-------+------+---------------+---------+---------+-------+------+--------------------------+
|
Now, here the scan on "removed IS NULL" produces 10 rows, even if we saw above that the optimizer knows it will produce about 1K rows.
This might be not a problem for this particular query, but it may cause the optimizer not to pick a good query plan.
- relates to
-
MDEV-6672
Performance degradation on a query with joins and ORDER BY .. LIMIT
-
-
Closed
{"report":{"fcp":991.1999998092651,"ttfb":286.4000005722046,"pageVisibility":"visible","entityId":43600,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":32,"apdex":0.5,"journeyId":"2b9572ec-3a4a-4f48-a694-534e7b2b56eb","navigationType":0,"readyForUser":1114.1999998092651,"redirectCount":0,"resourceLoadedEnd":1353.1999998092651,"resourceLoadedStart":301.30000019073486,"resourceTiming":[{"duration":172.80000019073486,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":301.30000019073486,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":301.30000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":474.1000003814697,"responseStart":0,"secureConnectionStart":0},{"duration":172.80000019073486,"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":301.6000003814697,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":301.6000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":474.4000005722046,"responseStart":0,"secureConnectionStart":0},{"duration":181.5,"initiatorType":"script","name":"https://jira.mariadb.org/s/0917945aaa57108d00c5076fea35e069-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":301.80000019073486,"connectEnd":301.80000019073486,"connectStart":301.80000019073486,"domainLookupEnd":301.80000019073486,"domainLookupStart":301.80000019073486,"fetchStart":301.80000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":301.80000019073486,"responseEnd":483.30000019073486,"responseStart":483.30000019073486,"secureConnectionStart":301.80000019073486},{"duration":253.69999980926514,"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":301.9000005722046,"connectEnd":301.9000005722046,"connectStart":301.9000005722046,"domainLookupEnd":301.9000005722046,"domainLookupStart":301.9000005722046,"fetchStart":301.9000005722046,"redirectEnd":0,"redirectStart":0,"requestStart":301.9000005722046,"responseEnd":555.6000003814697,"responseStart":555.6000003814697,"secureConnectionStart":301.9000005722046},{"duration":257.1000003814697,"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":302.19999980926514,"connectEnd":302.19999980926514,"connectStart":302.19999980926514,"domainLookupEnd":302.19999980926514,"domainLookupStart":302.19999980926514,"fetchStart":302.19999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":302.19999980926514,"responseEnd":559.3000001907349,"responseStart":559.3000001907349,"secureConnectionStart":302.19999980926514},{"duration":257.5999994277954,"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":302.4000005722046,"connectEnd":302.4000005722046,"connectStart":302.4000005722046,"domainLookupEnd":302.4000005722046,"domainLookupStart":302.4000005722046,"fetchStart":302.4000005722046,"redirectEnd":0,"redirectStart":0,"requestStart":302.4000005722046,"responseEnd":560,"responseStart":560,"secureConnectionStart":302.4000005722046},{"duration":257.9000005722046,"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":302.5,"connectEnd":302.5,"connectStart":302.5,"domainLookupEnd":302.5,"domainLookupStart":302.5,"fetchStart":302.5,"redirectEnd":0,"redirectStart":0,"requestStart":302.5,"responseEnd":560.4000005722046,"responseStart":560.4000005722046,"secureConnectionStart":302.5},{"duration":331.30000019073486,"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":302.69999980926514,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":302.69999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":634,"responseStart":0,"secureConnectionStart":0},{"duration":258.1000003814697,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":302.80000019073486,"connectEnd":302.80000019073486,"connectStart":302.80000019073486,"domainLookupEnd":302.80000019073486,"domainLookupStart":302.80000019073486,"fetchStart":302.80000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":302.80000019073486,"responseEnd":560.9000005722046,"responseStart":560.9000005722046,"secureConnectionStart":302.80000019073486},{"duration":331.30000019073486,"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":303,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":303,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":634.3000001907349,"responseStart":0,"secureConnectionStart":0},{"duration":258.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":303.19999980926514,"connectEnd":303.19999980926514,"connectStart":303.19999980926514,"domainLookupEnd":303.19999980926514,"domainLookupStart":303.19999980926514,"fetchStart":303.19999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":303.19999980926514,"responseEnd":561.6999998092651,"responseStart":561.6999998092651,"secureConnectionStart":303.19999980926514},{"duration":415,"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":304,"connectEnd":304,"connectStart":304,"domainLookupEnd":304,"domainLookupStart":304,"fetchStart":304,"redirectEnd":0,"redirectStart":0,"requestStart":304,"responseEnd":719,"responseStart":719,"secureConnectionStart":304},{"duration":1029.5,"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":304.1000003814697,"connectEnd":304.1000003814697,"connectStart":304.1000003814697,"domainLookupEnd":304.1000003814697,"domainLookupStart":304.1000003814697,"fetchStart":304.1000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":304.1000003814697,"responseEnd":1333.6000003814697,"responseStart":1333.6000003814697,"secureConnectionStart":304.1000003814697},{"duration":72.5,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":664.4000005722046,"connectEnd":664.4000005722046,"connectStart":664.4000005722046,"domainLookupEnd":664.4000005722046,"domainLookupStart":664.4000005722046,"fetchStart":664.4000005722046,"redirectEnd":0,"redirectStart":0,"requestStart":664.4000005722046,"responseEnd":736.9000005722046,"responseStart":736.9000005722046,"secureConnectionStart":664.4000005722046},{"duration":398.5,"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":936.5,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":936.5,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1335,"responseStart":0,"secureConnectionStart":0},{"duration":415.69999980926514,"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":937.5,"connectEnd":937.5,"connectStart":937.5,"domainLookupEnd":937.5,"domainLookupStart":937.5,"fetchStart":937.5,"redirectEnd":0,"redirectStart":0,"requestStart":937.5,"responseEnd":1353.1999998092651,"responseStart":1353.1999998092651,"secureConnectionStart":937.5},{"duration":428.6000003814697,"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":937.8000001907349,"connectEnd":937.8000001907349,"connectStart":937.8000001907349,"domainLookupEnd":937.8000001907349,"domainLookupStart":937.8000001907349,"fetchStart":937.8000001907349,"redirectEnd":0,"redirectStart":0,"requestStart":937.8000001907349,"responseEnd":1366.4000005722046,"responseStart":1366.4000005722046,"secureConnectionStart":937.8000001907349}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":136,"responseStart":287,"responseEnd":293,"domLoading":299,"domInteractive":1366,"domContentLoadedEventStart":1366,"domContentLoadedEventEnd":1408,"domComplete":2538,"loadEventStart":2538,"loadEventEnd":2540,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":1339.6000003814697},{"name":"bigPipe.sidebar-id.end","time":1340.6000003814697},{"name":"bigPipe.activity-panel-pipe-id.start","time":1340.6999998092651},{"name":"bigPipe.activity-panel-pipe-id.end","time":1345.5},{"name":"activityTabFullyLoaded","time":1416.6999998092651}],"measures":[],"correlationId":"b83463313716cd","effectiveType":"4g","downlink":9.8,"rtt":0,"serverDuration":85,"dbReadsTimeInMs":8,"dbConnsTimeInMs":16,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}