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":1037.1000003814697,"ttfb":493.1000003814697,"pageVisibility":"visible","entityId":43600,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":0.5,"journeyId":"554230c4-e7f5-41d8-a29e-528a7046fd7e","navigationType":0,"readyForUser":1095.5,"redirectCount":0,"resourceLoadedEnd":1400.3000001907349,"resourceLoadedStart":498.69999980926514,"resourceTiming":[{"duration":124.90000057220459,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2bu7/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":498.69999980926514,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":498.69999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":623.6000003814697,"responseStart":0,"secureConnectionStart":0},{"duration":124.69999980926514,"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":499,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":499,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":623.6999998092651,"responseStart":0,"secureConnectionStart":0},{"duration":133.5,"initiatorType":"script","name":"https://jira.mariadb.org/s/fbf975c0cce4b1abf04784eeae9ba1f4-CDN/lu2bu7/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":499.1000003814697,"connectEnd":499.1000003814697,"connectStart":499.1000003814697,"domainLookupEnd":499.1000003814697,"domainLookupStart":499.1000003814697,"fetchStart":499.1000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":499.1000003814697,"responseEnd":632.6000003814697,"responseStart":632.6000003814697,"secureConnectionStart":499.1000003814697},{"duration":179.80000019073486,"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":499.19999980926514,"connectEnd":499.19999980926514,"connectStart":499.19999980926514,"domainLookupEnd":499.19999980926514,"domainLookupStart":499.19999980926514,"fetchStart":499.19999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":499.19999980926514,"responseEnd":679,"responseStart":679,"secureConnectionStart":499.19999980926514},{"duration":183.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":499.4000005722046,"connectEnd":499.4000005722046,"connectStart":499.4000005722046,"domainLookupEnd":499.4000005722046,"domainLookupStart":499.4000005722046,"fetchStart":499.4000005722046,"redirectEnd":0,"redirectStart":0,"requestStart":499.4000005722046,"responseEnd":682.9000005722046,"responseStart":682.9000005722046,"secureConnectionStart":499.4000005722046},{"duration":184.29999923706055,"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":499.4000005722046,"connectEnd":499.4000005722046,"connectStart":499.4000005722046,"domainLookupEnd":499.4000005722046,"domainLookupStart":499.4000005722046,"fetchStart":499.4000005722046,"redirectEnd":0,"redirectStart":0,"requestStart":499.4000005722046,"responseEnd":683.6999998092651,"responseStart":683.6999998092651,"secureConnectionStart":499.4000005722046},{"duration":184.60000038146973,"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":499.5,"connectEnd":499.5,"connectStart":499.5,"domainLookupEnd":499.5,"domainLookupStart":499.5,"fetchStart":499.5,"redirectEnd":0,"redirectStart":0,"requestStart":499.5,"responseEnd":684.1000003814697,"responseStart":684.1000003814697,"secureConnectionStart":499.5},{"duration":184.80000019073486,"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":499.69999980926514,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":499.69999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":684.5,"responseStart":0,"secureConnectionStart":0},{"duration":185.19999980926514,"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":499.80000019073486,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":499.80000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":685,"responseStart":0,"secureConnectionStart":0},{"duration":184.69999980926514,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":499.80000019073486,"connectEnd":499.80000019073486,"connectStart":499.80000019073486,"domainLookupEnd":499.80000019073486,"domainLookupStart":499.80000019073486,"fetchStart":499.80000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":499.80000019073486,"responseEnd":684.5,"responseStart":684.5,"secureConnectionStart":499.80000019073486},{"duration":185.19999980926514,"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":500,"connectEnd":500,"connectStart":500,"domainLookupEnd":500,"domainLookupStart":500,"fetchStart":500,"redirectEnd":0,"redirectStart":0,"requestStart":500,"responseEnd":685.1999998092651,"responseStart":685.1000003814697,"secureConnectionStart":500},{"duration":660.0999994277954,"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":500.6000003814697,"connectEnd":500.6000003814697,"connectStart":500.6000003814697,"domainLookupEnd":500.6000003814697,"domainLookupStart":500.6000003814697,"fetchStart":500.6000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":500.6000003814697,"responseEnd":1160.6999998092651,"responseStart":1160.6999998092651,"secureConnectionStart":500.6000003814697},{"duration":899.6000003814697,"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":500.69999980926514,"connectEnd":500.69999980926514,"connectStart":500.69999980926514,"domainLookupEnd":500.69999980926514,"domainLookupStart":500.69999980926514,"fetchStart":500.69999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":500.69999980926514,"responseEnd":1400.3000001907349,"responseStart":1400.3000001907349,"secureConnectionStart":500.69999980926514},{"duration":441.3999996185303,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":846.6000003814697,"connectEnd":846.6000003814697,"connectStart":846.6000003814697,"domainLookupEnd":846.6000003814697,"domainLookupStart":846.6000003814697,"fetchStart":846.6000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":846.6000003814697,"responseEnd":1288,"responseStart":1288,"secureConnectionStart":846.6000003814697},{"duration":428.79999923706055,"initiatorType":"script","name":"https://www.google-analytics.com/analytics.js","startTime":1017.4000005722046,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":1017.4000005722046,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1446.1999998092651,"responseStart":0,"secureConnectionStart":0}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":371,"responseStart":493,"responseEnd":495,"domLoading":496,"domInteractive":1425,"domContentLoadedEventStart":1425,"domContentLoadedEventEnd":1459,"domComplete":2016,"loadEventStart":2016,"loadEventEnd":2017,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":1413.6999998092651},{"name":"bigPipe.sidebar-id.end","time":1414.5},{"name":"bigPipe.activity-panel-pipe-id.start","time":1414.6000003814697},{"name":"bigPipe.activity-panel-pipe-id.end","time":1416.9000005722046},{"name":"activityTabFullyLoaded","time":1467.6000003814697}],"measures":[],"correlationId":"7012a707a345c7","effectiveType":"4g","downlink":10,"rtt":0,"serverDuration":70,"dbReadsTimeInMs":11,"dbConnsTimeInMs":18,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}