create table ten(a int);
|
insert into ten values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
|
create table t1 (key1 int, col1 int, filler1 varchar(100), filler varchar(100), key(key1));
|
insert into t1
|
select A.a + B.a* 10 + C.a * 100,
|
A.a + B.a* 10 + C.a * 100,
|
'filler1-data', 'filler2-data'
|
from
|
ten A, ten B, ten C;
|
set histogram_size=100;
|
set use_stat_tables='preferably';
|
set optimizer_use_condition_selectivity=4;
|
analyze table t1 persistent for all;
|
Let's check the data distribution. First, we have a non-indexed condition with
50% selectivity.
explain extended select * from t1 where col1 < 500;
|
+------+-------------+-------+------+---------------+------+---------+------+------+----------+-------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
|
+------+-------------+-------+------+---------------+------+---------+------+------+----------+-------------+
|
| 1 | SIMPLE | t1 | ALL | NULL | NULL | NULL | NULL | 1000 | 50.50 | Using where |
|
+------+-------------+-------+------+---------------+------+---------+------+------+----------+-------------+
|
Second, "key1<10" produces a range access which selects 10/1000= 1/100th of the
table:
explain extended select * from t1 where key1<10 ;
|
+------+-------------+-------+-------+---------------+------+---------+------+------+----------+-----------------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
|
+------+-------------+-------+-------+---------------+------+---------+------+------+----------+-----------------------+
|
| 1 | SIMPLE | t1 | range | key1 | key1 | 5 | NULL | 9 | 100.00 | Using index condition |
|
+------+-------------+-------+-------+---------------+------+---------+------+------+----------+-----------------------+
|
Now, let's use both conditions:
explain extended select * from t1 where key1<10 and col1< 500;
|
+------+-------------+-------+-------+---------------+------+---------+------+------+----------+------------------------------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
|
+------+-------------+-------+-------+---------------+------+---------+------+------+----------+------------------------------------+
|
| 1 | SIMPLE | t1 | range | key1 | key1 | 5 | NULL | 9 | 50.50 | Using index condition; Using where |
|
+------+-------------+-------+-------+---------------+------+---------+------+------+----------+------------------------------------+
|
EXPLAIN looks ok. However, in debugger, one can see:
Breakpoint 31, matching_candidates_in_table (...
|
(gdb) fini
|
Run till exit from #0 matching_candidates_in_table (...
|
Value returned is $279 = 4.5445544554455441
|
4.5 records is 9 records * 0.5 selectivity. Ok so far.
Breakpoint 32, table_cond_selectivity (...
|
(gdb) fini
|
0x000000000069e43e in best_extension_by_limited_search (...
|
Value returned is $281 = 0.50495049504950495
|
(gdb) list
|
7597 double partial_join_cardinality= current_record_count *
|
7598 pushdown_cond_selectivity;
|
(gdb) print partial_join_cardinality
|
$283 = 2.294775022056661
|
Ooops. Selectivity=0.5 has been applied for the second time, and now we've got
9 * 0.5 * 0.5 = 2.25 for estimate.
Let's check if it matters..
create table t2 as select * from t1;
|
explain extended select * from t1,t2 where t1.key1<10 and t1.col1< 500;
|
For the last query, put a breakpoint in best_access_path().
Breakpoint 33, best_access_path (join=0x7fffca5a4088, s=0x7fffca5a5038, remaining_tables=3, idx=0, disable_jbuf=false, record_count=1, pos=0x7fffca5a58b0, loose_scan_pos=0x7ffff7e9fac0) at /home/psergey/dev2/10.0/sql/sql_select.cc:5642
|
(gdb) p s->table->alias.Ptr
|
$288 = 0x7fffca4215f0 "t1"
|
(gdb) c
|
Continuing.
|
|
Breakpoint 33, best_access_path (join=0x7fffca5a4088, s=0x7fffca5a5360, remaining_tables=2, idx=1, disable_jbuf=false, record_count=2.294775022056661, pos=0x7fffca5a59b8, loose_scan_pos=0x7ffff7e9f8b0) at /home/psergey/dev2/10.0/sql/sql_select.cc:5642
|
(gdb) p s->table->alias.Ptr
|
$289 = 0x7fffca5a7270 "t2"
|
See: table t2 has got record_count=2.29. Half of what should be.
{"report":{"fcp":1376.2999992370605,"ttfb":646.8999996185303,"pageVisibility":"visible","entityId":34109,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":32,"apdex":0.5,"journeyId":"3c02e577-301d-4068-aad6-552266565855","navigationType":0,"readyForUser":1457.3999996185303,"redirectCount":0,"resourceLoadedEnd":1823.5,"resourceLoadedStart":655.1999998092651,"resourceTiming":[{"duration":162.0999994277954,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":655.1999998092651,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":655.1999998092651,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":817.2999992370605,"responseStart":0,"secureConnectionStart":0},{"duration":162,"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":655.5999994277954,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":655.5999994277954,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":817.5999994277954,"responseStart":0,"secureConnectionStart":0},{"duration":262.1000003814697,"initiatorType":"script","name":"https://jira.mariadb.org/s/0917945aaa57108d00c5076fea35e069-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":655.7999992370605,"connectEnd":655.7999992370605,"connectStart":655.7999992370605,"domainLookupEnd":655.7999992370605,"domainLookupStart":655.7999992370605,"fetchStart":655.7999992370605,"redirectEnd":0,"redirectStart":0,"requestStart":655.7999992370605,"responseEnd":917.8999996185303,"responseStart":917.8999996185303,"secureConnectionStart":655.7999992370605},{"duration":366.80000019073486,"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":655.8999996185303,"connectEnd":655.8999996185303,"connectStart":655.8999996185303,"domainLookupEnd":655.8999996185303,"domainLookupStart":655.8999996185303,"fetchStart":655.8999996185303,"redirectEnd":0,"redirectStart":0,"requestStart":655.8999996185303,"responseEnd":1022.6999998092651,"responseStart":1022.6999998092651,"secureConnectionStart":655.8999996185303},{"duration":370.30000019073486,"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":656.1999998092651,"connectEnd":656.1999998092651,"connectStart":656.1999998092651,"domainLookupEnd":656.1999998092651,"domainLookupStart":656.1999998092651,"fetchStart":656.1999998092651,"redirectEnd":0,"redirectStart":0,"requestStart":656.1999998092651,"responseEnd":1026.5,"responseStart":1026.5,"secureConnectionStart":656.1999998092651},{"duration":370.69999980926514,"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":656.3999996185303,"connectEnd":656.3999996185303,"connectStart":656.3999996185303,"domainLookupEnd":656.3999996185303,"domainLookupStart":656.3999996185303,"fetchStart":656.3999996185303,"redirectEnd":0,"redirectStart":0,"requestStart":656.3999996185303,"responseEnd":1027.0999994277954,"responseStart":1027.0999994277954,"secureConnectionStart":656.3999996185303},{"duration":371.0999994277954,"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":656.5,"connectEnd":656.5,"connectStart":656.5,"domainLookupEnd":656.5,"domainLookupStart":656.5,"fetchStart":656.5,"redirectEnd":0,"redirectStart":0,"requestStart":656.5,"responseEnd":1027.5999994277954,"responseStart":1027.5999994277954,"secureConnectionStart":656.5},{"duration":410.5,"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":656.6999998092651,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":656.6999998092651,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1067.1999998092651,"responseStart":0,"secureConnectionStart":0},{"duration":371.19999980926514,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":656.8999996185303,"connectEnd":656.8999996185303,"connectStart":656.8999996185303,"domainLookupEnd":656.8999996185303,"domainLookupStart":656.8999996185303,"fetchStart":656.8999996185303,"redirectEnd":0,"redirectStart":0,"requestStart":656.8999996185303,"responseEnd":1028.0999994277954,"responseStart":1028.0999994277954,"secureConnectionStart":656.8999996185303},{"duration":410.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":657.0999994277954,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":657.0999994277954,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1067.3999996185303,"responseStart":0,"secureConnectionStart":0},{"duration":371.5999994277954,"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":657.1999998092651,"connectEnd":657.1999998092651,"connectStart":657.1999998092651,"domainLookupEnd":657.1999998092651,"domainLookupStart":657.1999998092651,"fetchStart":657.1999998092651,"redirectEnd":0,"redirectStart":0,"requestStart":657.1999998092651,"responseEnd":1028.7999992370605,"responseStart":1028.7999992370605,"secureConnectionStart":657.1999998092651},{"duration":723.8999996185303,"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":658,"connectEnd":658,"connectStart":658,"domainLookupEnd":658,"domainLookupStart":658,"fetchStart":658,"redirectEnd":0,"redirectStart":0,"requestStart":658,"responseEnd":1381.8999996185303,"responseStart":1381.8999996185303,"secureConnectionStart":658},{"duration":1165.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":658,"connectEnd":658,"connectStart":658,"domainLookupEnd":658,"domainLookupStart":658,"fetchStart":658,"redirectEnd":0,"redirectStart":0,"requestStart":658,"responseEnd":1823.5,"responseStart":1823.5,"secureConnectionStart":658},{"duration":305.30000019073486,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":1078.6999998092651,"connectEnd":1078.6999998092651,"connectStart":1078.6999998092651,"domainLookupEnd":1078.6999998092651,"domainLookupStart":1078.6999998092651,"fetchStart":1078.6999998092651,"redirectEnd":0,"redirectStart":0,"requestStart":1078.6999998092651,"responseEnd":1384,"responseStart":1384,"secureConnectionStart":1078.6999998092651}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":472,"responseStart":647,"responseEnd":651,"domLoading":652,"domInteractive":1856,"domContentLoadedEventStart":1856,"domContentLoadedEventEnd":1905,"domComplete":2189,"loadEventStart":2189,"loadEventEnd":2189,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":1838.7999992370605},{"name":"bigPipe.sidebar-id.end","time":1839.5999994277954},{"name":"bigPipe.activity-panel-pipe-id.start","time":1839.7999992370605},{"name":"bigPipe.activity-panel-pipe-id.end","time":1840.6999998092651},{"name":"activityTabFullyLoaded","time":1917.5999994277954}],"measures":[],"correlationId":"13b8e94936d833","effectiveType":"4g","downlink":10,"rtt":0,"serverDuration":104,"dbReadsTimeInMs":13,"dbConnsTimeInMs":23,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}
So, matching_candidates_in_table() takes into account that
selectivity(t1.col1<500)=0.5. This is correct.
The wrong action is that table_cond_selectivity() returns 1.
The problem is in table_cond_selectivity(). It should return 1.