When the optimizer is evaluating join order and estimating rows produced by a join order, it loops through column constraints adding rather than merging selectivity estimates.
Attachments
Issue Links
relates to
MDEV-31067selectivity_from_histogram >1.0 for col=const on DOUBLE_PREC_HB histogram
Closed
MDEV-22360Sufficient conditions for accurate calculation of join cardinality
select json_detailed(json_extract(trace, '$**.selectivity_for_columns')) from information_schema.optimizer_trace;
produces
| [
[
{
"column_name": "a",
"ranges":
[
"86930 <= a <= 86930",
"86931 <= a <= 86931",
"86932 <= a <= 86932",
"86933 <= a <= 86933",
"86934 <= a <= 86934",
"86935 <= a <= 86935",
<SNIP>
"87055 <= a <= 87055",
"87056 <= a <= 87056",
"87057 <= a <= 87057",
"87058 <= a <= 87058",
"87059 <= a <= 87059"
],
"selectivity_from_histogram": 1.0078
}
]
] |
All these values fit into one bucket. The correct selectivity is 0.0078125.
Rex Johnston
added a comment -
drop table if exists bucket, one_third_of_bucket, t10;
create table bucket(a int ); # This holds how many rows we hold in a bucket.
insert into bucket select 1 from seq_1_to_780;
create table one_third_of_bucket(a int ); # one-third of a bucket
insert into one_third_of_bucket select 1 from seq_1_to_260;
create table t10 (a int );
insert into t10 select 0 from bucket, seq_1_to_4;
insert into t10 select 86930 from one_third_of_bucket;
insert into t10 select 86940 from one_third_of_bucket;
insert into t10 select 86950 from one_third_of_bucket;
insert into t10 select 347830 from one_third_of_bucket;
insert into t10 select 347840 from one_third_of_bucket;
insert into t10 select 347850 from one_third_of_bucket;
insert into t10 select 347850 from bucket, seq_1_to_8;
insert into t10 select 652140 from one_third_of_bucket;
insert into t10 select 652150 from one_third_of_bucket;
insert into t10 select 652160 from one_third_of_bucket;
insert into t10 select 652160 from bucket, seq_1_to_52;
insert into t10 select 652170 from one_third_of_bucket;
insert into t10 select 652180 from one_third_of_bucket;
insert into t10 select 652190 from one_third_of_bucket;
insert into t10 select 652190 from bucket;
insert into t10 select 739130 from one_third_of_bucket;
insert into t10 select 739140 from one_third_of_bucket;
insert into t10 select 739150 from one_third_of_bucket;
insert into t10 select 739150 from bucket, seq_1_to_40;
insert into t10 select 782570 from one_third_of_bucket;
insert into t10 select 782580 from one_third_of_bucket;
insert into t10 select 782590 from one_third_of_bucket;
insert into t10 select 913000 from one_third_of_bucket;
insert into t10 select 913010 from one_third_of_bucket;
insert into t10 select 913020 from one_third_of_bucket;
insert into t10 select 913020 from bucket, seq_1_to_6;
insert into t10 select 913030 from one_third_of_bucket;
insert into t10 select 913040 from one_third_of_bucket;
insert into t10 select 913050 from one_third_of_bucket;
insert into t10 select 913050 from bucket, seq_1_to_8;
insert into t10 select 999980 from one_third_of_bucket;
insert into t10 select 999990 from one_third_of_bucket;
insert into t10 select 1000000 from one_third_of_bucket;
analyze table t10 persistent for all ;
and
set optimizer_trace=1;
explain select * from t10 where a in (86930, 86931, 86932, 86933, 86934, 86935, 86936, 86937, 86938, 86939, 86940, 86941, 86942, 86943, 86944, 86945, 86946, 86947, 86948, 86949, 86950, 86951, 86952, 86953, 86954, 86955, 86956, 86957, 86958, 86959, 86960, 86961, 86962, 86963, 86964, 86965, 86966, 86967, 86968, 86969, 86960, 86971, 86972, 86973, 86974, 86975, 86976, 86977, 86978, 86979, 86980, 86981, 86982, 86983, 86984, 86985, 86986, 86987, 86988, 86989, 86990, 86991, 86992, 86993, 86994, 86995, 86996, 86997, 86998, 86999, 87000, 87001, 87002, 87003, 87004, 87005, 87006, 87007, 87008, 87009, 87010, 87011, 87012, 87013, 87014, 87015, 87016, 87017, 87018, 87019, 87020, 87021, 87022, 87023, 87024, 87025, 87026, 87027, 87028, 87029, 87030, 87031, 87032, 87033, 87034, 87035, 87036, 87037, 87038, 87039, 87040, 87041, 87042, 87043, 87044, 87045, 87046, 87047, 87048, 87049, 87050, 87051, 87052, 87053, 87054, 87055, 87056, 87057, 87058, 87059);
select json_detailed(json_extract(trace, '$**.selectivity_for_columns' )) from information_schema.optimizer_trace;
produces
| [
[
{
"column_name": "a",
"ranges":
[
"86930 <= a <= 86930",
"86931 <= a <= 86931",
"86932 <= a <= 86932",
"86933 <= a <= 86933",
"86934 <= a <= 86934",
"86935 <= a <= 86935",
<SNIP>
"87055 <= a <= 87055",
"87056 <= a <= 87056",
"87057 <= a <= 87057",
"87058 <= a <= 87058",
"87059 <= a <= 87059"
],
"selectivity_from_histogram": 1.0078
}
]
] |
All these values fit into one bucket. The correct selectivity is 0.0078125.
For the record: the following happens in the code with the pushed variant for MDEV-31067:
The column has 30 distinct values, the histogram has 128 buckets.
All lookup constants fit into one bucket so we get 1/128th (as it is less than 1/30th) as the estimate for each a=const.
The IN-list has 129 elements so the estimate sums up to a value slightly greater than 1.
With the old code (before any patches for MDEV-31067), we didn't have this effect for this example, because the bucket the values hit is "narrow" and the logic that MDEV-31067 patch refers to as "brave heuristic" makes the estimate smaller.
Sergei Petrunia
added a comment - Agree.
For the record: the following happens in the code with the pushed variant for MDEV-31067 :
The column has 30 distinct values, the histogram has 128 buckets.
All lookup constants fit into one bucket so we get 1/128th (as it is less than 1/30th) as the estimate for each a=const .
The IN-list has 129 elements so the estimate sums up to a value slightly greater than 1.
With the old code (before any patches for MDEV-31067 ), we didn't have this effect for this example, because the bucket the values hit is "narrow" and the logic that MDEV-31067 patch refers to as "brave heuristic" makes the estimate smaller.
... that is, in order to observe the issue the column had to have 30 values and the query had to query for more values than that (129).
Sergei Petrunia
added a comment - ... that is, in order to observe the issue the column had to have 30 values and the query had to query for more values than that (129).
Automated message:
----------------------------
Since this issue has not been updated since 6 weeks, it's time to move it back to Stalled.
Julien Fritsch
added a comment - Automated message:
----------------------------
Since this issue has not been updated since 6 weeks, it's time to move it back to Stalled.
Automated message:
----------------------------
Since this issue has not been updated since 6 weeks, it's time to move it back to Stalled.
JiraAutomate
added a comment - Automated message:
----------------------------
Since this issue has not been updated since 6 weeks, it's time to move it back to Stalled.
People
Rex Johnston
Rex Johnston
Votes:
0Vote for this issue
Watchers:
3Start watching this issue
Dates
Created:
Updated:
Git Integration
Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.
{"report":{"fcp":837.7999999523163,"ttfb":153.59999990463257,"pageVisibility":"visible","entityId":122141,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":1,"journeyId":"632fe3ba-1bbc-4488-88fc-6a27dde0cc46","navigationType":0,"readyForUser":902.7000000476837,"redirectCount":0,"resourceLoadedEnd":952.7999999523163,"resourceLoadedStart":158.59999990463257,"resourceTiming":[{"duration":117.40000009536743,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2bu7/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":158.59999990463257,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":158.59999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":276,"responseStart":0,"secureConnectionStart":0},{"duration":117.09999990463257,"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":159,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":159,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":276.09999990463257,"responseStart":0,"secureConnectionStart":0},{"duration":126,"initiatorType":"script","name":"https://jira.mariadb.org/s/fbf975c0cce4b1abf04784eeae9ba1f4-CDN/lu2bu7/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":159,"connectEnd":159,"connectStart":159,"domainLookupEnd":159,"domainLookupStart":159,"fetchStart":159,"redirectEnd":0,"redirectStart":0,"requestStart":159,"responseEnd":285,"responseStart":285,"secureConnectionStart":159},{"duration":325.7999999523163,"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":159.09999990463257,"connectEnd":159.09999990463257,"connectStart":159.09999990463257,"domainLookupEnd":159.09999990463257,"domainLookupStart":159.09999990463257,"fetchStart":159.09999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":159.09999990463257,"responseEnd":484.89999985694885,"responseStart":484.89999985694885,"secureConnectionStart":159.09999990463257},{"duration":329.2000000476837,"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":159.29999995231628,"connectEnd":159.29999995231628,"connectStart":159.29999995231628,"domainLookupEnd":159.29999995231628,"domainLookupStart":159.29999995231628,"fetchStart":159.29999995231628,"redirectEnd":0,"redirectStart":0,"requestStart":159.29999995231628,"responseEnd":488.5,"responseStart":488.5,"secureConnectionStart":159.29999995231628},{"duration":329.5,"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":159.29999995231628,"connectEnd":159.29999995231628,"connectStart":159.29999995231628,"domainLookupEnd":159.29999995231628,"domainLookupStart":159.29999995231628,"fetchStart":159.29999995231628,"redirectEnd":0,"redirectStart":0,"requestStart":159.29999995231628,"responseEnd":488.7999999523163,"responseStart":488.7999999523163,"secureConnectionStart":159.29999995231628},{"duration":330.2000000476837,"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":159.5,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":159.5,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":489.7000000476837,"responseStart":0,"secureConnectionStart":0},{"duration":329.7000000476837,"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":159.5,"connectEnd":159.5,"connectStart":159.5,"domainLookupEnd":159.5,"domainLookupStart":159.5,"fetchStart":159.5,"redirectEnd":0,"redirectStart":0,"requestStart":159.5,"responseEnd":489.2000000476837,"responseStart":489.2000000476837,"secureConnectionStart":159.5},{"duration":330.10000014305115,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":159.59999990463257,"connectEnd":159.59999990463257,"connectStart":159.59999990463257,"domainLookupEnd":159.59999990463257,"domainLookupStart":159.59999990463257,"fetchStart":159.59999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":159.59999990463257,"responseEnd":489.7000000476837,"responseStart":489.59999990463257,"secureConnectionStart":159.59999990463257},{"duration":330.2000000476837,"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":159.79999995231628,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":159.79999995231628,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":490,"responseStart":0,"secureConnectionStart":0},{"duration":330.2000000476837,"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":160,"connectEnd":160,"connectStart":160,"domainLookupEnd":160,"domainLookupStart":160,"fetchStart":160,"redirectEnd":0,"redirectStart":0,"requestStart":160,"responseEnd":490.2000000476837,"responseStart":490.09999990463257,"secureConnectionStart":160},{"duration":461,"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":160.70000004768372,"connectEnd":160.70000004768372,"connectStart":160.70000004768372,"domainLookupEnd":160.70000004768372,"domainLookupStart":160.70000004768372,"fetchStart":160.70000004768372,"redirectEnd":0,"redirectStart":0,"requestStart":160.70000004768372,"responseEnd":621.7000000476837,"responseStart":621.7000000476837,"secureConnectionStart":160.70000004768372},{"duration":674.3999998569489,"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":181.20000004768372,"connectEnd":181.20000004768372,"connectStart":181.20000004768372,"domainLookupEnd":181.20000004768372,"domainLookupStart":181.20000004768372,"fetchStart":181.20000004768372,"redirectEnd":0,"redirectStart":0,"requestStart":181.20000004768372,"responseEnd":855.5999999046326,"responseStart":855.5999999046326,"secureConnectionStart":181.20000004768372},{"duration":178.19999980926514,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":432.2000000476837,"connectEnd":432.2000000476837,"connectStart":432.2000000476837,"domainLookupEnd":432.2000000476837,"domainLookupStart":432.2000000476837,"fetchStart":432.2000000476837,"redirectEnd":0,"redirectStart":0,"requestStart":432.2000000476837,"responseEnd":610.3999998569489,"responseStart":610.3999998569489,"secureConnectionStart":432.2000000476837},{"duration":206.20000004768372,"initiatorType":"link","name":"https://jira.mariadb.org/s/d5715adaadd168a9002b108b2b039b50-CDN/lu2bu7/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":746.5999999046326,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":746.5999999046326,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":952.7999999523163,"responseStart":0,"secureConnectionStart":0},{"duration":292.90000009536743,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bu7/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":747.2999999523163,"connectEnd":747.2999999523163,"connectStart":747.2999999523163,"domainLookupEnd":747.2999999523163,"domainLookupStart":747.2999999523163,"fetchStart":747.2999999523163,"redirectEnd":0,"redirectStart":0,"requestStart":747.2999999523163,"responseEnd":1040.2000000476837,"responseStart":1040.2000000476837,"secureConnectionStart":747.2999999523163},{"duration":296.2999999523163,"initiatorType":"script","name":"https://jira.mariadb.org/s/f51ef5507eea4c158f257c66c93b2a3f-CDN/lu2bu7/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":747.7000000476837,"connectEnd":747.7000000476837,"connectStart":747.7000000476837,"domainLookupEnd":747.7000000476837,"domainLookupStart":747.7000000476837,"fetchStart":747.7000000476837,"redirectEnd":0,"redirectStart":0,"requestStart":747.7000000476837,"responseEnd":1044,"responseStart":1044,"secureConnectionStart":747.7000000476837},{"duration":213.29999995231628,"initiatorType":"script","name":"https://www.google-analytics.com/analytics.js","startTime":831.5,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":831.5,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1044.7999999523163,"responseStart":0,"secureConnectionStart":0}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":9,"responseStart":154,"responseEnd":181,"domLoading":157,"domInteractive":974,"domContentLoadedEventStart":974,"domContentLoadedEventEnd":1033,"domComplete":1334,"loadEventStart":1334,"loadEventEnd":1334,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":933.2999999523163},{"name":"bigPipe.sidebar-id.end","time":934.2000000476837},{"name":"bigPipe.activity-panel-pipe-id.start","time":934.2999999523163},{"name":"bigPipe.activity-panel-pipe-id.end","time":939.7000000476837},{"name":"activityTabFullyLoaded","time":1048.5}],"measures":[],"correlationId":"f81dbe76abfca3","effectiveType":"4g","downlink":10,"rtt":0,"serverDuration":94,"dbReadsTimeInMs":13,"dbConnsTimeInMs":21,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}
and
produces
| [
[
{
"column_name": "a",
"ranges":
[
"86930 <= a <= 86930",
"86931 <= a <= 86931",
"86932 <= a <= 86932",
"86933 <= a <= 86933",
"86934 <= a <= 86934",
"86935 <= a <= 86935",
<SNIP>
"87055 <= a <= 87055",
"87056 <= a <= 87056",
"87057 <= a <= 87057",
"87058 <= a <= 87058",
"87059 <= a <= 87059"
],
"selectivity_from_histogram": 1.0078
}
]
] |
All these values fit into one bucket. The correct selectivity is 0.0078125.