Type:
Bug
Priority:
Major
Resolution:
Fixed
Affects Version/s:
5.5(EOL) , 10.0(EOL) , 10.1(EOL) , 10.2(EOL) , 10.3(EOL)
When the optimizer considers a ref access for a full PRIMARY key, it will use an estimate of rows=1. The same will happen for a UNIQUE key, but only if the columns are not NULL-able. If any of the columns are NULLable, the estimate from index statistics is used.
The reason is possible NULL values. A unique index with NULL-able columns may have multiple rows with NULL values.
However, the most common type of join uses null-rejecting predicates, "keypart = ...". For those, NULLable UNIQUE index is the same as non-NULLable, and the optimizer should be able to figure out that it should use an estimate of rows=1.
Testcase:
create table ten(a int );
insert into ten values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
create table t1 (
pk int not null primary key auto_increment,
a int ,
b int ,
unique key (a)
);
# 10K of null values
insert into t1 (a,b) select null , 12345 from ten A, ten B, ten C;
# 10 non- null values
insert into t1 (a,b) select a,a from ten;
analyze table t1;
mysql> explain select * from ten,t1 where ten.a=t1.a;
+------+-------------+-------+------+---------------+------+---------+----------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+------+---------------+------+---------+----------+------+-------------+
| 1 | SIMPLE | ten | ALL | NULL | NULL | NULL | NULL | 10 | Using where |
| 1 | SIMPLE | t1 | ref | a | a | 5 | j5.ten.a | 45 | |
+------+-------------+-------+------+---------------+------+---------+----------+------+-------------+
note, t1.rows=45 here.
Even if we use a null-rejecting ref access on a UNIQUE key, and will get at most 1 row.
Index statistics:
mysql> show keys from t1;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| t1 | 0 | PRIMARY | 1 | pk | A | 1010 | NULL | NULL | | BTREE | | |
| t1 | 0 | a | 1 | a | A | 22 | NULL | NULL | YES | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
(If you want to suggest to fiddle with innodb_stats_method , check out MDEV-19574 - persistent innodb stats will always use nulls_equal)
(If you want to suggest to use EITS, note that collecting index stats with EITS currently requires a full index scan, so we can't assume EITS is always there ATM).
relates to
MDEV-21480
Unique key using ref access though eq_ref access can be used
Closed
{"report":{"fcp":1116.800000011921,"ttfb":397.10000002384186,"pageVisibility":"visible","entityId":76269,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":0.5,"journeyId":"418b91f2-138d-46e6-85a2-fca9af4e2bcc","navigationType":0,"readyForUser":1203.5,"redirectCount":0,"resourceLoadedEnd":1073,"resourceLoadedStart":402.80000001192093,"resourceTiming":[{"duration":193.19999998807907,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2bv2/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":402.80000001192093,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":402.80000001192093,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":596,"responseStart":0,"secureConnectionStart":0},{"duration":193.4000000357628,"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":403,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":403,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":596.4000000357628,"responseStart":0,"secureConnectionStart":0},{"duration":202.39999997615814,"initiatorType":"script","name":"https://jira.mariadb.org/s/e9b27a47da5fb0f74a35acd57e9847fb-CDN/lu2bv2/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":403.2000000476837,"connectEnd":403.2000000476837,"connectStart":403.2000000476837,"domainLookupEnd":403.2000000476837,"domainLookupStart":403.2000000476837,"fetchStart":403.2000000476837,"redirectEnd":0,"redirectStart":0,"requestStart":403.2000000476837,"responseEnd":605.6000000238419,"responseStart":605.6000000238419,"secureConnectionStart":403.2000000476837},{"duration":253.80000001192093,"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":403.4000000357628,"connectEnd":403.4000000357628,"connectStart":403.4000000357628,"domainLookupEnd":403.4000000357628,"domainLookupStart":403.4000000357628,"fetchStart":403.4000000357628,"redirectEnd":0,"redirectStart":0,"requestStart":403.4000000357628,"responseEnd":657.2000000476837,"responseStart":657.1000000238419,"secureConnectionStart":403.4000000357628},{"duration":257.5999999642372,"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":403.7000000476837,"connectEnd":403.7000000476837,"connectStart":403.7000000476837,"domainLookupEnd":403.7000000476837,"domainLookupStart":403.7000000476837,"fetchStart":403.7000000476837,"redirectEnd":0,"redirectStart":0,"requestStart":403.7000000476837,"responseEnd":661.3000000119209,"responseStart":661.3000000119209,"secureConnectionStart":403.7000000476837},{"duration":257.89999997615814,"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":403.9000000357628,"connectEnd":403.9000000357628,"connectStart":403.9000000357628,"domainLookupEnd":403.9000000357628,"domainLookupStart":403.9000000357628,"fetchStart":403.9000000357628,"redirectEnd":0,"redirectStart":0,"requestStart":403.9000000357628,"responseEnd":661.8000000119209,"responseStart":661.8000000119209,"secureConnectionStart":403.9000000357628},{"duration":258.2000000476837,"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":404,"connectEnd":404,"connectStart":404,"domainLookupEnd":404,"domainLookupStart":404,"fetchStart":404,"redirectEnd":0,"redirectStart":0,"requestStart":404,"responseEnd":662.2000000476837,"responseStart":662.2000000476837,"secureConnectionStart":404},{"duration":343.7999999523163,"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":404.2000000476837,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":404.2000000476837,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":748,"responseStart":0,"secureConnectionStart":0},{"duration":258.30000001192093,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":404.4000000357628,"connectEnd":404.4000000357628,"connectStart":404.4000000357628,"domainLookupEnd":404.4000000357628,"domainLookupStart":404.4000000357628,"fetchStart":404.4000000357628,"redirectEnd":0,"redirectStart":0,"requestStart":404.4000000357628,"responseEnd":662.7000000476837,"responseStart":662.7000000476837,"secureConnectionStart":404.4000000357628},{"duration":343.7000000476837,"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":404.5,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":404.5,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":748.2000000476837,"responseStart":0,"secureConnectionStart":0},{"duration":258.5,"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":404.7000000476837,"connectEnd":404.7000000476837,"connectStart":404.7000000476837,"domainLookupEnd":404.7000000476837,"domainLookupStart":404.7000000476837,"fetchStart":404.7000000476837,"redirectEnd":0,"redirectStart":0,"requestStart":404.7000000476837,"responseEnd":663.2000000476837,"responseStart":663.2000000476837,"secureConnectionStart":404.7000000476837},{"duration":512.8999999761581,"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":405.60000002384186,"connectEnd":405.60000002384186,"connectStart":405.60000002384186,"domainLookupEnd":405.60000002384186,"domainLookupStart":405.60000002384186,"fetchStart":405.60000002384186,"redirectEnd":0,"redirectStart":0,"requestStart":405.60000002384186,"responseEnd":918.5,"responseStart":918.5,"secureConnectionStart":405.60000002384186},{"duration":656.8999999761581,"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":410.10000002384186,"connectEnd":410.10000002384186,"connectStart":410.10000002384186,"domainLookupEnd":410.10000002384186,"domainLookupStart":410.10000002384186,"fetchStart":410.10000002384186,"redirectEnd":0,"redirectStart":0,"requestStart":410.10000002384186,"responseEnd":1067,"responseStart":1067,"secureConnectionStart":410.10000002384186},{"duration":159.80000001192093,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":759,"connectEnd":759,"connectStart":759,"domainLookupEnd":759,"domainLookupStart":759,"fetchStart":759,"redirectEnd":0,"redirectStart":0,"requestStart":759,"responseEnd":918.8000000119209,"responseStart":918.8000000119209,"secureConnectionStart":759},{"duration":55.799999952316284,"initiatorType":"link","name":"https://jira.mariadb.org/s/d5715adaadd168a9002b108b2b039b50-CDN/lu2bv2/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","startTime":1017.2000000476837,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":1017.2000000476837,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1073,"responseStart":0,"secureConnectionStart":0},{"duration":271.60000002384186,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bv2/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","startTime":1018.3000000119209,"connectEnd":1018.3000000119209,"connectStart":1018.3000000119209,"domainLookupEnd":1018.3000000119209,"domainLookupStart":1018.3000000119209,"fetchStart":1018.3000000119209,"redirectEnd":0,"redirectStart":0,"requestStart":1018.3000000119209,"responseEnd":1289.9000000357628,"responseStart":1289.9000000357628,"secureConnectionStart":1018.3000000119209},{"duration":275.5999999642372,"initiatorType":"script","name":"https://jira.mariadb.org/s/53a43b6764f587426c7bb9a150184c00-CDN/lu2bv2/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","startTime":1018.7000000476837,"connectEnd":1018.7000000476837,"connectStart":1018.7000000476837,"domainLookupEnd":1018.7000000476837,"domainLookupStart":1018.7000000476837,"fetchStart":1018.7000000476837,"redirectEnd":0,"redirectStart":0,"requestStart":1018.7000000476837,"responseEnd":1294.300000011921,"responseStart":1294.300000011921,"secureConnectionStart":1018.7000000476837}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":158,"responseStart":397,"responseEnd":402,"domLoading":401,"domInteractive":1261,"domContentLoadedEventStart":1261,"domContentLoadedEventEnd":1310,"domComplete":2528,"loadEventStart":2528,"loadEventEnd":2529,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":1242.9000000357628},{"name":"bigPipe.sidebar-id.end","time":1243.6000000238419},{"name":"bigPipe.activity-panel-pipe-id.start","time":1243.9000000357628},{"name":"bigPipe.activity-panel-pipe-id.end","time":1246.4000000357628},{"name":"activityTabFullyLoaded","time":1320.9000000357628}],"measures":[],"correlationId":"37b17aa33a9900","effectiveType":"4g","downlink":9.7,"rtt":0,"serverDuration":98,"dbReadsTimeInMs":11,"dbConnsTimeInMs":19,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}