CREATE TABLE revenue(id int, month int, year int, value int);
|
INSERT INTO revenue values (1, 1, 2000, 100), (2, 2, 2000, 200), (3, 1, 2000, 300), (4, 2, 2000, 400);
|
SELECT
|
anon.month_and_year,
|
(SUM(anon.value) / COUNT(DISTINCT anon.id)) AS average_revenue
|
FROM (
|
SELECT
|
id, value,
|
concat(CAST(month AS CHAR(2)), '-', CAST(year AS CHAR(4))) AS month_and_year
|
FROM revenue
|
) as anon
|
GROUP BY anon.month_and_year
|
ORDER BY average_revenue;
|
Produces
+----------------+-----------------+
|
| month_and_year | average_revenue |
|
+----------------+-----------------+
|
| 1-2000 | 100.0000 |
|
| 2-2000 | 200.0000 |
|
| 1-2000 | 300.0000 |
|
| 2-2000 | 400.0000 |
|
+----------------+-----------------+
|
Removing the order by clause gives:
+----------------+-----------------+
|
| month_and_year | average_revenue |
|
+----------------+-----------------+
|
| 1-2000 | 200.0000 |
|
| 2-2000 | 300.0000 |
|
+----------------+-----------------+
|
Turning off the derived_merge optimization gives:
set session optimizer_switch="derived_merge=off";
|
+----------------+-----------------+
|
| month_and_year | average_revenue |
|
+----------------+-----------------+
|
| 1-2000 | 200.0000 |
|
| 2-2000 | 300.0000 |
|
+----------------+-----------------+
|
The issue seems to be somewhat similar to MDEV-17775, but in my case there is no join.
Also, here are a few more interesting observations:
- Removing the DISTINCT removes the duplicate rows
- Removing the ORDER BY removes the duplicate rows
Interestingly, when the derived_merged optimization is on (the default), the rewritten query seems correct:
EXPLAIN EXTENDED
|
SELECT
|
anon.month_and_year,
|
(SUM(anon.value) / COUNT(DISTINCT anon.id)) AS average_revenue
|
FROM (
|
SELECT
|
id, value,
|
concat(CAST(month AS CHAR(2)), '-', CAST(year AS CHAR(4))) AS month_and_year
|
FROM revenue
|
) as anon
|
GROUP BY anon.month_and_year
|
ORDER BY average_revenue;
|
produces:
+------+-------------+---------+------+---------------+------+---------+------+------+----------+---------------------------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
|
+------+-------------+---------+------+---------------+------+---------+------+------+----------+---------------------------------+
|
| 1 | SIMPLE | revenue | ALL | NULL | NULL | NULL | NULL | 8 | 100.00 | Using temporary; Using filesort |
|
+------+-------------+---------+------+---------------+------+---------+------+------+----------+---------------------------------+
|
+-------+------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
| Level | Code | Message |
|
+-------+------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
| Note | 1003 | select concat(cast(`test`.`revenue`.`month` as char(2) charset utf8mb4),'-',cast(`test`.`revenue`.`year` as char(4) charset utf8mb4)) AS `month_and_year`,sum(`test`.`revenue`.`value`) / count(distinct `test`.`revenue`.`id`) AS `average_revenue` from `test`.`revenue` group by concat(cast(`test`.`revenue`.`month` as char(2) charset utf8mb4),'-',cast(`test`.`revenue`.`year` as char(4) charset utf8mb4)) order by sum(`test`.`revenue`.`value`) / count(distinct `test`.`revenue`.`id`) |
|
+-------+------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
If I run the rewritten query (the one from above), it produces the correct result:
select concat(cast(`test`.`revenue`.`month` as char(2) charset utf8mb4),'-',cast(`test`.`revenue`.`year` as char(4) charset utf8mb4)) AS `month_and_year`,sum(`test`.`revenue`.`value`) / count(distinct `test`.`revenue`.`id`) AS `average_revenue` from `test`.`revenue` group by concat(cast(`test`.`revenue`.`month` as char(2) charset utf8mb4),'-',cast(`test`.`revenue`.`year` as char(4) charset utf8mb4)) order by sum(`test`.`revenue`.`value`) / count(distinct `test`.`revenue`.`id`)
|
|
+----------------+-----------------+
|
| month_and_year | average_revenue |
|
+----------------+-----------------+
|
| 1-2000 | 200.0000 |
|
| 2-2000 | 300.0000 |
|
+----------------+-----------------+
|
But the original query produces an incorrect result. This is particularly puzzling.
{"report":{"fcp":2703.6000003814697,"ttfb":1207.5,"pageVisibility":"visible","entityId":79989,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":32,"apdex":0.5,"journeyId":"91bb5642-118d-4375-9ea3-53f4c80d5fce","navigationType":0,"readyForUser":2806.300000190735,"redirectCount":0,"resourceLoadedEnd":3319.7000002861023,"resourceLoadedStart":1213.6000003814697,"resourceTiming":[{"duration":958.7999997138977,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":1213.6000003814697,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":1213.6000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":2172.4000000953674,"responseStart":0,"secureConnectionStart":0},{"duration":971.2000002861023,"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":1214,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":1214,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":2185.2000002861023,"responseStart":0,"secureConnectionStart":0},{"duration":1043,"initiatorType":"script","name":"https://jira.mariadb.org/s/0917945aaa57108d00c5076fea35e069-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":1214.1000003814697,"connectEnd":1214.1000003814697,"connectStart":1214.1000003814697,"domainLookupEnd":1214.1000003814697,"domainLookupStart":1214.1000003814697,"fetchStart":1214.1000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":1214.1000003814697,"responseEnd":2257.1000003814697,"responseStart":2257.1000003814697,"secureConnectionStart":1214.1000003814697},{"duration":1109.8000001907349,"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":1214.4000000953674,"connectEnd":1214.4000000953674,"connectStart":1214.4000000953674,"domainLookupEnd":1214.4000000953674,"domainLookupStart":1214.4000000953674,"fetchStart":1214.4000000953674,"redirectEnd":0,"redirectStart":0,"requestStart":1214.4000000953674,"responseEnd":2324.2000002861023,"responseStart":2324.2000002861023,"secureConnectionStart":1214.4000000953674},{"duration":1113.2999997138977,"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":1214.6000003814697,"connectEnd":1214.6000003814697,"connectStart":1214.6000003814697,"domainLookupEnd":1214.6000003814697,"domainLookupStart":1214.6000003814697,"fetchStart":1214.6000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":1214.6000003814697,"responseEnd":2327.9000000953674,"responseStart":2327.9000000953674,"secureConnectionStart":1214.6000003814697},{"duration":1113.6999998092651,"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":1214.7000002861023,"connectEnd":1214.7000002861023,"connectStart":1214.7000002861023,"domainLookupEnd":1214.7000002861023,"domainLookupStart":1214.7000002861023,"fetchStart":1214.7000002861023,"redirectEnd":0,"redirectStart":0,"requestStart":1214.7000002861023,"responseEnd":2328.4000000953674,"responseStart":2328.4000000953674,"secureConnectionStart":1214.7000002861023},{"duration":1113.8000001907349,"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":1214.9000000953674,"connectEnd":1214.9000000953674,"connectStart":1214.9000000953674,"domainLookupEnd":1214.9000000953674,"domainLookupStart":1214.9000000953674,"fetchStart":1214.9000000953674,"redirectEnd":0,"redirectStart":0,"requestStart":1214.9000000953674,"responseEnd":2328.7000002861023,"responseStart":2328.7000002861023,"secureConnectionStart":1214.9000000953674},{"duration":1182.8999996185303,"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":1215.1000003814697,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":1215.1000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":2398,"responseStart":0,"secureConnectionStart":0},{"duration":1114,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":1215.1000003814697,"connectEnd":1215.1000003814697,"connectStart":1215.1000003814697,"domainLookupEnd":1215.1000003814697,"domainLookupStart":1215.1000003814697,"fetchStart":1215.1000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":1215.1000003814697,"responseEnd":2329.1000003814697,"responseStart":2329.1000003814697,"secureConnectionStart":1215.1000003814697},{"duration":1182.7000002861023,"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":1215.4000000953674,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":1215.4000000953674,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":2398.1000003814697,"responseStart":0,"secureConnectionStart":0},{"duration":1114.2000002861023,"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":1215.5,"connectEnd":1215.5,"connectStart":1215.5,"domainLookupEnd":1215.5,"domainLookupStart":1215.5,"fetchStart":1215.5,"redirectEnd":0,"redirectStart":0,"requestStart":1215.5,"responseEnd":2329.7000002861023,"responseStart":2329.6000003814697,"secureConnectionStart":1215.5},{"duration":2101.7999997138977,"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":1216.6000003814697,"connectEnd":1216.6000003814697,"connectStart":1216.6000003814697,"domainLookupEnd":1216.6000003814697,"domainLookupStart":1216.6000003814697,"fetchStart":1216.6000003814697,"redirectEnd":0,"redirectStart":0,"requestStart":1216.6000003814697,"responseEnd":3318.4000000953674,"responseStart":3318.4000000953674,"secureConnectionStart":1216.6000003814697},{"duration":2103,"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":1216.7000002861023,"connectEnd":1216.7000002861023,"connectStart":1216.7000002861023,"domainLookupEnd":1216.7000002861023,"domainLookupStart":1216.7000002861023,"fetchStart":1216.7000002861023,"redirectEnd":0,"redirectStart":0,"requestStart":1216.7000002861023,"responseEnd":3319.7000002861023,"responseStart":3319.7000002861023,"secureConnectionStart":1216.7000002861023},{"duration":577.4000000953674,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":2409.2000002861023,"connectEnd":2409.2000002861023,"connectStart":2409.2000002861023,"domainLookupEnd":2409.2000002861023,"domainLookupStart":2409.2000002861023,"fetchStart":2409.2000002861023,"redirectEnd":0,"redirectStart":0,"requestStart":2409.2000002861023,"responseEnd":2986.6000003814697,"responseStart":2986.6000003814697,"secureConnectionStart":2409.2000002861023}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":1025,"responseStart":1208,"responseEnd":1210,"domLoading":1211,"domInteractive":3345,"domContentLoadedEventStart":3345,"domContentLoadedEventEnd":3391,"domComplete":4679,"loadEventStart":4680,"loadEventEnd":4680,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":3325.7000002861023},{"name":"bigPipe.sidebar-id.end","time":3326.5},{"name":"bigPipe.activity-panel-pipe-id.start","time":3326.6000003814697},{"name":"bigPipe.activity-panel-pipe-id.end","time":3328},{"name":"activityTabFullyLoaded","time":3406.9000000953674}],"measures":[],"correlationId":"ec2717661476e9","effectiveType":"4g","downlink":10,"rtt":0,"serverDuration":111,"dbReadsTimeInMs":13,"dbConnsTimeInMs":22,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}
Patch
http://lists.askmonty.org/pipermail/commits/2019-December/014081.html