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
Transition |
Time In Source Status |
Execution Times |
Open |
|
In Progress |
|
19h 48m
|
1
|
In Progress |
|
Closed |
|
6s
|
1
|
{"report":{"fcp":1183.3000001907349,"ttfb":362.20000076293945,"pageVisibility":"visible","entityId":43600,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":0.5,"journeyId":"1529907c-0e90-4759-9dff-5c593a1633ec","navigationType":0,"readyForUser":1262.2000007629395,"redirectCount":0,"resourceLoadedEnd":1882.7000007629395,"resourceLoadedStart":367.6000003814697,"resourceTiming":[{"duration":301,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":367.6000003814697,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":367.6000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":668.6000003814697,"responseStart":0,"secureConnectionStart":0},{"duration":301.0999994277954,"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":367.9000005722046,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":367.9000005722046,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":669,"responseStart":0,"secureConnectionStart":0},{"duration":357.8999996185303,"initiatorType":"script","name":"https://jira.mariadb.org/s/0917945aaa57108d00c5076fea35e069-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":368.1000003814697,"connectEnd":368.1000003814697,"connectStart":368.1000003814697,"domainLookupEnd":368.1000003814697,"domainLookupStart":368.1000003814697,"fetchStart":368.1000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":368.1000003814697,"responseEnd":726,"responseStart":726,"secureConnectionStart":368.1000003814697},{"duration":436.29999923706055,"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":368.20000076293945,"connectEnd":368.20000076293945,"connectStart":368.20000076293945,"domainLookupEnd":368.20000076293945,"domainLookupStart":368.20000076293945,"fetchStart":368.20000076293945,"redirectEnd":0,"redirectStart":0,"requestStart":368.20000076293945,"responseEnd":804.5,"responseStart":804.5,"secureConnectionStart":368.20000076293945},{"duration":439.9000005722046,"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":368.5,"connectEnd":368.5,"connectStart":368.5,"domainLookupEnd":368.5,"domainLookupStart":368.5,"fetchStart":368.5,"redirectEnd":0,"redirectStart":0,"requestStart":368.5,"responseEnd":808.4000005722046,"responseStart":808.4000005722046,"secureConnectionStart":368.5},{"duration":440.3999996185303,"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":368.6000003814697,"connectEnd":368.6000003814697,"connectStart":368.6000003814697,"domainLookupEnd":368.6000003814697,"domainLookupStart":368.6000003814697,"fetchStart":368.6000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":368.6000003814697,"responseEnd":809,"responseStart":809,"secureConnectionStart":368.6000003814697},{"duration":440.5,"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":368.80000019073486,"connectEnd":368.80000019073486,"connectStart":368.80000019073486,"domainLookupEnd":368.80000019073486,"domainLookupStart":368.80000019073486,"fetchStart":368.80000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":368.80000019073486,"responseEnd":809.3000001907349,"responseStart":809.3000001907349,"secureConnectionStart":368.80000019073486},{"duration":500.6000003814697,"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":369.1000003814697,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":369.1000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":869.7000007629395,"responseStart":0,"secureConnectionStart":0},{"duration":440.5,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":369.30000019073486,"connectEnd":369.30000019073486,"connectStart":369.30000019073486,"domainLookupEnd":369.30000019073486,"domainLookupStart":369.30000019073486,"fetchStart":369.30000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":369.30000019073486,"responseEnd":809.8000001907349,"responseStart":809.8000001907349,"secureConnectionStart":369.30000019073486},{"duration":500.5,"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":369.4000005722046,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":369.4000005722046,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":869.9000005722046,"responseStart":0,"secureConnectionStart":0},{"duration":440.80000019073486,"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":369.6000003814697,"connectEnd":369.6000003814697,"connectStart":369.6000003814697,"domainLookupEnd":369.6000003814697,"domainLookupStart":369.6000003814697,"fetchStart":369.6000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":369.6000003814697,"responseEnd":810.4000005722046,"responseStart":810.4000005722046,"secureConnectionStart":369.6000003814697},{"duration":1345.6000003814697,"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":374.80000019073486,"connectEnd":374.80000019073486,"connectStart":374.80000019073486,"domainLookupEnd":374.80000019073486,"domainLookupStart":374.80000019073486,"fetchStart":374.80000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":374.80000019073486,"responseEnd":1720.4000005722046,"responseStart":1720.4000005722046,"secureConnectionStart":374.80000019073486},{"duration":1507.8000001907349,"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":374.9000005722046,"connectEnd":374.9000005722046,"connectStart":374.9000005722046,"domainLookupEnd":374.9000005722046,"domainLookupStart":374.9000005722046,"fetchStart":374.9000005722046,"redirectEnd":0,"redirectStart":0,"requestStart":374.9000005722046,"responseEnd":1882.7000007629395,"responseStart":1882.7000007629395,"secureConnectionStart":374.9000005722046},{"duration":839.0999994277954,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":881.7000007629395,"connectEnd":881.7000007629395,"connectStart":881.7000007629395,"domainLookupEnd":881.7000007629395,"domainLookupStart":881.7000007629395,"fetchStart":881.7000007629395,"redirectEnd":0,"redirectStart":0,"requestStart":881.7000007629395,"responseEnd":1720.8000001907349,"responseStart":1720.8000001907349,"secureConnectionStart":881.7000007629395},{"duration":721.1999998092651,"initiatorType":"script","name":"https://www.google-analytics.com/analytics.js","startTime":1177.1000003814697,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":1177.1000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1898.3000001907349,"responseStart":0,"secureConnectionStart":0}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":210,"responseStart":363,"responseEnd":368,"domLoading":366,"domInteractive":1900,"domContentLoadedEventStart":1900,"domContentLoadedEventEnd":1942,"domComplete":2541,"loadEventStart":2541,"loadEventEnd":2541,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":1886.9000005722046},{"name":"bigPipe.sidebar-id.end","time":1887.7000007629395},{"name":"bigPipe.activity-panel-pipe-id.start","time":1887.9000005722046},{"name":"bigPipe.activity-panel-pipe-id.end","time":1888.9000005722046},{"name":"activityTabFullyLoaded","time":1957}],"measures":[],"correlationId":"d0a8c1cca2bb2a","effectiveType":"4g","downlink":9.3,"rtt":0,"serverDuration":92,"dbReadsTimeInMs":14,"dbConnsTimeInMs":21,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}