Create a 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 int);
|
insert into t1 select NULL, a from one_k;
|
set optimizer_use_condition_selectivity=4;
|
SET use_stat_tables = PREFERABLY;
|
set histogram_size=100;
|
analyze table t1 persistent for all;
|
Ok, so we've got EITS statistics for t1.A. t1.a has only NULL values. Let's see how selectivity estimates work
MariaDB [test]> explain extended select * from t1 A straight_join t1 B where A.a < 5;
|
+------+-------------+-------+------+---------------+------+---------+------+------+----------+------------------------------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
|
+------+-------------+-------+------+---------------+------+---------+------+------+----------+------------------------------------+
|
| 1 | SIMPLE | A | ALL | NULL | NULL | NULL | NULL | 1000 | 0.00 | Using where |
|
| 1 | SIMPLE | B | ALL | NULL | NULL | NULL | NULL | 1000 | 100.00 | Using join buffer (flat, BNL join) |
|
+------+-------------+-------+------+---------------+------+---------+------+------+----------+------------------------------------+
|
So far, good. Now, let's try an "IS NULL":
MariaDB [test]> explain extended select * from t1 A straight_join t1 B where A.a is null;
|
+------+-------------+-------+------+---------------+------+---------+------+------+----------+------------------------------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
|
+------+-------------+-------+------+---------------+------+---------+------+------+----------+------------------------------------+
|
| 1 | SIMPLE | A | ALL | NULL | NULL | NULL | NULL | 1000 | 0.00 | Using where |
|
| 1 | SIMPLE | B | ALL | NULL | NULL | NULL | NULL | 1000 | 100.00 | Using join buffer (flat, BNL join) |
|
+------+-------------+-------+------+---------------+------+---------+------+------+----------+------------------------------------+
|
We get filtered=0 even if it should have been 100.
{"report":{"fcp":2298.1000003814697,"ttfb":1174.7000002861023,"pageVisibility":"visible","entityId":45902,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":0.5,"journeyId":"c83ab565-d9fd-471f-81c7-3db6e7ca7a1b","navigationType":0,"readyForUser":2372.2000002861023,"redirectCount":0,"resourceLoadedEnd":2973.6000003814697,"resourceLoadedStart":1180.2000002861023,"resourceTiming":[{"duration":641.6999998092651,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2bv2/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":1180.2000002861023,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":1180.2000002861023,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1821.9000000953674,"responseStart":0,"secureConnectionStart":0},{"duration":641.8000001907349,"initiatorType":"link","name":"https://jira.mariadb.org/s/7ebd35e77e471bc30ff0eba799ebc151-CDN/lu2bv2/820016/12ta74/2380add21a9a1006587582385952de73/_/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":1180.5,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":1180.5,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1822.3000001907349,"responseStart":0,"secureConnectionStart":0},{"duration":650.2999997138977,"initiatorType":"script","name":"https://jira.mariadb.org/s/e9b27a47da5fb0f74a35acd57e9847fb-CDN/lu2bv2/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":1180.6000003814697,"connectEnd":1180.6000003814697,"connectStart":1180.6000003814697,"domainLookupEnd":1180.6000003814697,"domainLookupStart":1180.6000003814697,"fetchStart":1180.6000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":1180.6000003814697,"responseEnd":1830.9000000953674,"responseStart":1830.9000000953674,"secureConnectionStart":1180.6000003814697},{"duration":755.8000001907349,"initiatorType":"script","name":"https://jira.mariadb.org/s/c32eb0da7ad9831253f8397e6cc26afd-CDN/lu2bv2/820016/12ta74/2380add21a9a1006587582385952de73/_/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":1180.8000001907349,"connectEnd":1180.8000001907349,"connectStart":1180.8000001907349,"domainLookupEnd":1180.8000001907349,"domainLookupStart":1180.8000001907349,"fetchStart":1180.8000001907349,"redirectEnd":0,"redirectStart":0,"requestStart":1180.8000001907349,"responseEnd":1936.6000003814697,"responseStart":1936.6000003814697,"secureConnectionStart":1180.8000001907349},{"duration":759.1000003814697,"initiatorType":"script","name":"https://jira.mariadb.org/s/bc0bcb146314416123c992714ee00ff7-CDN/lu2bv2/820016/12ta74/c92c0caa9a024ae85b0ebdbed7fb4bd7/_/download/contextbatch/js/atl.global,-_super/batch.js?locale=en","startTime":1181,"connectEnd":1181,"connectStart":1181,"domainLookupEnd":1181,"domainLookupStart":1181,"fetchStart":1181,"redirectEnd":0,"redirectStart":0,"requestStart":1181,"responseEnd":1940.1000003814697,"responseStart":1940.1000003814697,"secureConnectionStart":1181},{"duration":759.2999997138977,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bv2/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-en/jira.webresources:calendar-en.js","startTime":1181.2000002861023,"connectEnd":1181.2000002861023,"connectStart":1181.2000002861023,"domainLookupEnd":1181.2000002861023,"domainLookupStart":1181.2000002861023,"fetchStart":1181.2000002861023,"redirectEnd":0,"redirectStart":0,"requestStart":1181.2000002861023,"responseEnd":1940.5,"responseStart":1940.5,"secureConnectionStart":1181.2000002861023},{"duration":759.4000000953674,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bv2/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-localisation-moment/jira.webresources:calendar-localisation-moment.js","startTime":1181.5,"connectEnd":1181.5,"connectStart":1181.5,"domainLookupEnd":1181.5,"domainLookupStart":1181.5,"fetchStart":1181.5,"redirectEnd":0,"redirectStart":0,"requestStart":1181.5,"responseEnd":1940.9000000953674,"responseStart":1940.9000000953674,"secureConnectionStart":1181.5},{"duration":804.3999996185303,"initiatorType":"link","name":"https://jira.mariadb.org/s/b04b06a02d1959df322d9cded3aeecc1-CDN/lu2bv2/820016/12ta74/a2ff6aa845ffc9a1d22fe23d9ee791fc/_/download/contextbatch/css/jira.global.look-and-feel,-_super/batch.css","startTime":1181.6000003814697,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":1181.6000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1986,"responseStart":0,"secureConnectionStart":0},{"duration":759.5999999046326,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":1181.8000001907349,"connectEnd":1181.8000001907349,"connectStart":1181.8000001907349,"domainLookupEnd":1181.8000001907349,"domainLookupStart":1181.8000001907349,"fetchStart":1181.8000001907349,"redirectEnd":0,"redirectStart":0,"requestStart":1181.8000001907349,"responseEnd":1941.4000000953674,"responseStart":1941.4000000953674,"secureConnectionStart":1181.8000001907349},{"duration":804.3000001907349,"initiatorType":"link","name":"https://jira.mariadb.org/s/3ac36323ba5e4eb0af2aa7ac7211b4bb-CDN/lu2bv2/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":1181.9000000953674,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":1181.9000000953674,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1986.2000002861023,"responseStart":0,"secureConnectionStart":0},{"duration":759.6999998092651,"initiatorType":"script","name":"https://jira.mariadb.org/s/719848dd97ebe0663199f49a3936487a-CDN/lu2bv2/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":1182.2000002861023,"connectEnd":1182.2000002861023,"connectStart":1182.2000002861023,"domainLookupEnd":1182.2000002861023,"domainLookupStart":1182.2000002861023,"fetchStart":1182.2000002861023,"redirectEnd":0,"redirectStart":0,"requestStart":1182.2000002861023,"responseEnd":1941.9000000953674,"responseStart":1941.9000000953674,"secureConnectionStart":1182.2000002861023},{"duration":1364.7000002861023,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bv2/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-js/jira.webresources:bigpipe-js.js","startTime":1182.9000000953674,"connectEnd":1182.9000000953674,"connectStart":1182.9000000953674,"domainLookupEnd":1182.9000000953674,"domainLookupStart":1182.9000000953674,"fetchStart":1182.9000000953674,"redirectEnd":0,"redirectStart":0,"requestStart":1182.9000000953674,"responseEnd":2547.6000003814697,"responseStart":2547.5,"secureConnectionStart":1182.9000000953674},{"duration":1790.6000003814697,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bv2/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-init/jira.webresources:bigpipe-init.js","startTime":1183,"connectEnd":1183,"connectStart":1183,"domainLookupEnd":1183,"domainLookupStart":1183,"fetchStart":1183,"redirectEnd":0,"redirectStart":0,"requestStart":1183,"responseEnd":2973.6000003814697,"responseStart":2973.6000003814697,"secureConnectionStart":1183},{"duration":562.1999998092651,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":1997.6000003814697,"connectEnd":1997.6000003814697,"connectStart":1997.6000003814697,"domainLookupEnd":1997.6000003814697,"domainLookupStart":1997.6000003814697,"fetchStart":1997.6000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":1997.6000003814697,"responseEnd":2559.800000190735,"responseStart":2559.800000190735,"secureConnectionStart":1997.6000003814697},{"duration":682.9000000953674,"initiatorType":"script","name":"https://www.google-analytics.com/analytics.js","startTime":2291.800000190735,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":2291.800000190735,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":2974.7000002861023,"responseStart":0,"secureConnectionStart":0}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":1006,"responseStart":1174,"responseEnd":1177,"domLoading":1177,"domInteractive":2992,"domContentLoadedEventStart":2992,"domContentLoadedEventEnd":3033,"domComplete":3560,"loadEventStart":3560,"loadEventEnd":3561,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":2975.6000003814697},{"name":"bigPipe.sidebar-id.end","time":2976.5},{"name":"bigPipe.activity-panel-pipe-id.start","time":2976.6000003814697},{"name":"bigPipe.activity-panel-pipe-id.end","time":2977.7000002861023},{"name":"activityTabFullyLoaded","time":3046.5}],"measures":[],"correlationId":"a702998929c6ba","effectiveType":"4g","downlink":10,"rtt":0,"serverDuration":99,"dbReadsTimeInMs":11,"dbConnsTimeInMs":19,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}
The reason is this code in get_column_range_cardinality:
if (col_non_nulls < 1)
res= 0;
it basically means: 'if there are sufficiently many NULLs in the table, assume #matching_rows=0'. It ignores the possibility that the predicate is satisfied by NULL values.