Type:
Task
Priority:
Major
Resolution:
Unresolved
Fix Version/s:
None
Computing COUNT(DISTINCT <col1>, <col2>, <col3>) traditionally is done via storing the values in a UNIQUE object. The UNIQUE object uses a Red-Black tree with logarithmic complexity for insert and lookup.
This means that we achieve a theoretical complexity of O(rows * log(rows))
There is a way to speed this up, if we know that <col1>, <col2>, <col3> are already sorted.
In that case we only need to check when the tuple (col1, col2, col3) changes and increment the count value. This would lead to a complexity of O(rows), similar to the complexity of COUNT(*)
Test case to show the performance penalty:
analyze format=JSON
select count ( DISTINCT c,d,e),b from tt where a = 1 group by b\G
*************************** 1. row ***************************
ANALYZE: {
"query_block" : {
"select_id" : 1,
"r_loops" : 1,
"r_total_time_ms" : 518.9,
"table" : {
"table_name" : "tt" ,
"access_type" : "ref" ,
"possible_keys" : [ "tt_a_IDX" ],
"key" : "tt_a_IDX" ,
"key_length" : "4" ,
"used_key_parts" : [ "a" ],
"ref" : [ "const" ],
"r_loops" : 1,
"rows" : 248763,
"r_rows" : 500000,
"r_total_time_ms" : 99.395,
"filtered" : 100,
"r_filtered" : 100,
"attached_condition" : "tt.a <=> 1" ,
"using_index" : true
}
}
}
1 row in set (0.523 sec)
And here we can see the performance of count(*) case:
analyze format=JSON
select count (*),b from tt where a = 1 group by b\G
*************************** 1. row ***************************
ANALYZE: {
"query_block" : {
"select_id" : 1,
"r_loops" : 1,
"r_total_time_ms" : 114.54,
"table" : {
"table_name" : "tt" ,
"access_type" : "ref" ,
"possible_keys" : [ "tt_a_IDX" ],
"key" : "tt_a_IDX" ,
"key_length" : "4" ,
"used_key_parts" : [ "a" ],
"ref" : [ "const" ],
"r_loops" : 1,
"rows" : 248763,
"r_rows" : 500000,
"r_total_time_ms" : 97.696,
"filtered" : 100,
"r_filtered" : 100,
"attached_condition" : "tt.a <=> 1" ,
"using_index" : true
}
}
}
1 row in set (0.115 sec)
Data:
CREATE TABLE tt (a int , b varchar (32), c varchar (64), d varchar (64), e varchar (64), f int )
CREATE INDEX tt_a_IDX USING BTREE ON tt (a,b,c,d,e);
insert into tt select 1, mod(seq, 5), mod(seq, 11), mod(seq, 15), mod(seq, 22), seq from seq_1_to_1000000;
relates to
MDEV-30660
Aggregation functions fail to leverage uniqueness property
Closed
links to
{"report":{"fcp":1115.8000001907349,"ttfb":165.40000009536743,"pageVisibility":"visible","entityId":126606,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":0.5,"journeyId":"1edd1bae-98ff-492a-9213-114a8a59aa1d","navigationType":0,"readyForUser":1190.8000001907349,"redirectCount":0,"resourceLoadedEnd":1260.6000001430511,"resourceLoadedStart":182.40000009536743,"resourceTiming":[{"duration":447.7000000476837,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2bu7/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":182.40000009536743,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":182.40000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":630.1000001430511,"responseStart":0,"secureConnectionStart":0},{"duration":447.2999999523163,"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":182.80000019073486,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":182.80000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":630.1000001430511,"responseStart":0,"secureConnectionStart":0},{"duration":460.10000014305115,"initiatorType":"script","name":"https://jira.mariadb.org/s/fbf975c0cce4b1abf04784eeae9ba1f4-CDN/lu2bu7/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":183,"connectEnd":183,"connectStart":183,"domainLookupEnd":183,"domainLookupStart":183,"fetchStart":183,"redirectEnd":0,"redirectStart":0,"requestStart":183,"responseEnd":643.1000001430511,"responseStart":643.1000001430511,"secureConnectionStart":183},{"duration":503.7999999523163,"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":183.40000009536743,"connectEnd":183.40000009536743,"connectStart":183.40000009536743,"domainLookupEnd":183.40000009536743,"domainLookupStart":183.40000009536743,"fetchStart":183.40000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":183.40000009536743,"responseEnd":687.2000000476837,"responseStart":687.2000000476837,"secureConnectionStart":183.40000009536743},{"duration":500.40000009536743,"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":183.40000009536743,"connectEnd":183.40000009536743,"connectStart":183.40000009536743,"domainLookupEnd":183.40000009536743,"domainLookupStart":183.40000009536743,"fetchStart":183.40000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":183.40000009536743,"responseEnd":683.8000001907349,"responseStart":683.8000001907349,"secureConnectionStart":183.40000009536743},{"duration":504.30000019073486,"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":183.5,"connectEnd":183.5,"connectStart":183.5,"domainLookupEnd":183.5,"domainLookupStart":183.5,"fetchStart":183.5,"redirectEnd":0,"redirectStart":0,"requestStart":183.5,"responseEnd":687.8000001907349,"responseStart":687.8000001907349,"secureConnectionStart":183.5},{"duration":504.2999999523163,"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":183.70000004768372,"connectEnd":183.70000004768372,"connectStart":183.70000004768372,"domainLookupEnd":183.70000004768372,"domainLookupStart":183.70000004768372,"fetchStart":183.70000004768372,"redirectEnd":0,"redirectStart":0,"requestStart":183.70000004768372,"responseEnd":688,"responseStart":688,"secureConnectionStart":183.70000004768372},{"duration":504.40000009536743,"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":184.20000004768372,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":184.20000004768372,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":688.6000001430511,"responseStart":0,"secureConnectionStart":0},{"duration":504.2999999523163,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":184.20000004768372,"connectEnd":184.20000004768372,"connectStart":184.20000004768372,"domainLookupEnd":184.20000004768372,"domainLookupStart":184.20000004768372,"fetchStart":184.20000004768372,"redirectEnd":0,"redirectStart":0,"requestStart":184.20000004768372,"responseEnd":688.5,"responseStart":688.5,"secureConnectionStart":184.20000004768372},{"duration":505.09999990463257,"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":184.30000019073486,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":184.30000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":689.4000000953674,"responseStart":0,"secureConnectionStart":0},{"duration":504.7999999523163,"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":184.40000009536743,"connectEnd":184.40000009536743,"connectStart":184.40000009536743,"domainLookupEnd":184.40000009536743,"domainLookupStart":184.40000009536743,"fetchStart":184.40000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":184.40000009536743,"responseEnd":689.2000000476837,"responseStart":689.2000000476837,"secureConnectionStart":184.40000009536743},{"duration":1052.0999999046326,"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":207.60000014305115,"connectEnd":207.60000014305115,"connectStart":207.60000014305115,"domainLookupEnd":207.60000014305115,"domainLookupStart":207.60000014305115,"fetchStart":207.60000014305115,"redirectEnd":0,"redirectStart":0,"requestStart":207.60000014305115,"responseEnd":1259.7000000476837,"responseStart":1259.7000000476837,"secureConnectionStart":207.60000014305115},{"duration":1050.6000001430511,"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":210,"connectEnd":210,"connectStart":210,"domainLookupEnd":210,"domainLookupStart":210,"fetchStart":210,"redirectEnd":0,"redirectStart":0,"requestStart":210,"responseEnd":1260.6000001430511,"responseStart":1260.6000001430511,"secureConnectionStart":210},{"duration":373.7999999523163,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":878.4000000953674,"connectEnd":878.4000000953674,"connectStart":878.4000000953674,"domainLookupEnd":878.4000000953674,"domainLookupStart":878.4000000953674,"fetchStart":878.4000000953674,"redirectEnd":0,"redirectStart":0,"requestStart":878.4000000953674,"responseEnd":1252.2000000476837,"responseStart":1252.2000000476837,"secureConnectionStart":878.4000000953674}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":24,"responseStart":166,"responseEnd":210,"domLoading":169,"domInteractive":1277,"domContentLoadedEventStart":1277,"domContentLoadedEventEnd":1326,"domComplete":2140,"loadEventStart":2140,"loadEventEnd":2142,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":1263.3000001907349},{"name":"bigPipe.sidebar-id.end","time":1264.2000000476837},{"name":"bigPipe.activity-panel-pipe-id.start","time":1264.4000000953674},{"name":"bigPipe.activity-panel-pipe-id.end","time":1267.2000000476837},{"name":"activityTabFullyLoaded","time":1337}],"measures":[],"correlationId":"d20d58d2f467c7","effectiveType":"4g","downlink":9.1,"rtt":0,"serverDuration":69,"dbReadsTimeInMs":6,"dbConnsTimeInMs":12,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}