Consider the following slow query:
MariaDB [(none)]> analyze
|
-> SELECT
|
-> esd.esd_c1,
|
-> esd.esd_c2,
|
-> esd.esd_c3,
|
-> es.es_c1,
|
-> es.es_c2,
|
-> es.es_c3,
|
-> es.es_c4,
|
-> es.es_c5,
|
-> es.es_c6,
|
-> es.es_c7,
|
-> es.es_c8
|
-> FROM
|
-> DB1.t1 esd
|
-> INNER JOIN
|
-> DB1.t2 es ON esd.t1Id = es.t1Id
|
-> INNER JOIN
|
-> (SELECT
|
-> esd.esd_c1, MAX(es.es_c1) AS last_set
|
-> FROM
|
-> DB1.t1 esd
|
-> INNER JOIN DB1.t2 es ON esd.t1Id = es.t1Id
|
-> GROUP BY esd.esd_c1) q ON esd.esd_c1 = q.esd_c1
|
-> WHERE
|
-> es.es_c1 = q.last_set;
|
+------+-----------------+------------+-------+---------------------------+-----------+---------+----------------------------------------------+-------+----------+----------+------------+--------------------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | r_rows | filtered | r_filtered | Extra |
|
+------+-----------------+------------+-------+---------------------------+-----------+---------+----------------------------------------------+-------+----------+----------+------------+--------------------------+
|
| 1 | PRIMARY | esd | index | PRIMARY,UNIQUEMSG,Index_3 | UNIQUEMSG | 137 | NULL | 15197 | 15197.00 | 100.00 | 100.00 | Using where; Using index |
|
| 1 | PRIMARY | <derived2> | ref | key0 | key0 | 33 | DB1.esd.esd_c1 | 2 | 0.98 | 100.00 | 100.00 | Using where |
|
| 1 | PRIMARY | es | ref | PRIMARY | PRIMARY | 9 | DB1.esd.t1Id,q.last_set | 157 | 21.26 | 100.00 | 100.00 | |
|
| 2 | LATERAL DERIVED | esd | ref | PRIMARY,UNIQUEMSG,Index_3 | UNIQUEMSG | 33 | DB1.esd.esd_c1 | 7 | 17.40 | 100.00 | 100.00 | Using index |
|
| 2 | LATERAL DERIVED | es | ref | PRIMARY | PRIMARY | 4 | DB1.esd.t1Id | 875 | 783.76 | 100.00 | 100.00 | Using index |
|
+------+-----------------+------------+-------+---------------------------+-----------+---------+----------------------------------------------+-------+----------+----------+------------+--------------------------+
|
5 rows in set (1 min 12.157 sec)
|
It uses LATERAL DERIVED optimization and in the optimizer trace (see attached) we can find the following cardinality estimations (with statistics up to date including histograms etc):
"rows_estimation": [
|
{
|
"table": "esd",
|
"table_scan": {
|
"rows": 15197,
|
"cost": 97
|
}
|
},
|
{
|
"table": "es",
|
"table_scan": {
|
"rows": 12840562,
|
"cost": 103969
|
}
|
},
|
{
|
"table": "<derived2>",
|
"table_scan": {
|
"rows": 13304563,
|
"cost": 1.33e7
|
}
|
}
|
]
|
while real row counts are as follows:
MariaDB [(none)]> select count(*) from DB1.t1 esd;
|
+----------+
|
| count(*) |
|
+----------+
|
| 15197 |
|
+----------+
|
1 row in set (0.032 sec)
|
|
MariaDB [(none)]> select count(*) from DB1.t2 es;
|
+----------+
|
| count(*) |
|
+----------+
|
| 12846717 |
|
+----------+
|
1 row in set (13.979 sec)
|
|
MariaDB [(none)]> select count(*) from (SELECT
|
-> esd.symbol, MAX(es.es_c1) AS last_set
|
-> FROM
|
-> DB1.t1 esd
|
-> INNER JOIN DB1.t2 es ON esd.t1Id = es.t1Id
|
-> GROUP BY esd.esd_c1) q;
|
+----------+
|
| count(*) |
|
+----------+
|
| 1877 |
|
+----------+
|
1 row in set (5.507 sec)
|
You can see above that estimated number of rows for each of the tables is precise or very close to reality, while for the derived table it's orders of magnitude wrong. What can be done to fix this?
{"report":{"fcp":753.6999998092651,"ttfb":196.69999980926514,"pageVisibility":"visible","entityId":120304,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":1,"journeyId":"536f7112-1fc4-4bfe-bdc3-bf5a862bcaa7","navigationType":0,"readyForUser":853,"redirectCount":0,"resourceLoadedEnd":484.39999985694885,"resourceLoadedStart":202,"resourceTiming":[{"duration":18.199999809265137,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2bu7/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":202,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":202,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":220.19999980926514,"responseStart":0,"secureConnectionStart":0},{"duration":18,"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":202.19999980926514,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":202.19999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":220.19999980926514,"responseStart":0,"secureConnectionStart":0},{"duration":160.60000014305115,"initiatorType":"script","name":"https://jira.mariadb.org/s/fbf975c0cce4b1abf04784eeae9ba1f4-CDN/lu2bu7/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":202.39999985694885,"connectEnd":202.39999985694885,"connectStart":202.39999985694885,"domainLookupEnd":202.39999985694885,"domainLookupStart":202.39999985694885,"fetchStart":202.39999985694885,"redirectEnd":0,"redirectStart":0,"requestStart":225.29999995231628,"responseEnd":363,"responseStart":246.79999995231628,"secureConnectionStart":202.39999985694885},{"duration":281.89999985694885,"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":202.5,"connectEnd":202.5,"connectStart":202.5,"domainLookupEnd":202.5,"domainLookupStart":202.5,"fetchStart":202.5,"redirectEnd":0,"redirectStart":0,"requestStart":225.59999990463257,"responseEnd":484.39999985694885,"responseStart":244.89999985694885,"secureConnectionStart":202.5},{"duration":48.90000009536743,"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":202.59999990463257,"connectEnd":202.59999990463257,"connectStart":202.59999990463257,"domainLookupEnd":202.59999990463257,"domainLookupStart":202.59999990463257,"fetchStart":202.59999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":225.89999985694885,"responseEnd":251.5,"responseStart":248.29999995231628,"secureConnectionStart":202.59999990463257},{"duration":49.10000014305115,"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":202.69999980926514,"connectEnd":202.69999980926514,"connectStart":202.69999980926514,"domainLookupEnd":202.69999980926514,"domainLookupStart":202.69999980926514,"fetchStart":202.69999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":227.09999990463257,"responseEnd":251.79999995231628,"responseStart":249.19999980926514,"secureConnectionStart":202.69999980926514},{"duration":50.200000047683716,"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":202.69999980926514,"connectEnd":202.69999980926514,"connectStart":202.69999980926514,"domainLookupEnd":202.69999980926514,"domainLookupStart":202.69999980926514,"fetchStart":202.69999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":226.19999980926514,"responseEnd":252.89999985694885,"responseStart":250.29999995231628,"secureConnectionStart":202.69999980926514},{"duration":23,"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":202.79999995231628,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":202.79999995231628,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":225.79999995231628,"responseStart":0,"secureConnectionStart":0},{"duration":52.59999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":203,"connectEnd":203,"connectStart":203,"domainLookupEnd":203,"domainLookupStart":203,"fetchStart":203,"redirectEnd":0,"redirectStart":0,"requestStart":230.19999980926514,"responseEnd":255.59999990463257,"responseStart":254,"secureConnectionStart":203},{"duration":26.299999952316284,"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":203.09999990463257,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":203.09999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":229.39999985694885,"responseStart":0,"secureConnectionStart":0},{"duration":56.299999952316284,"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":203.09999990463257,"connectEnd":203.09999990463257,"connectStart":203.09999990463257,"domainLookupEnd":203.09999990463257,"domainLookupStart":203.09999990463257,"fetchStart":203.09999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":237.59999990463257,"responseEnd":259.39999985694885,"responseStart":257.7999999523163,"secureConnectionStart":203.09999990463257},{"duration":251.90000009536743,"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":208.89999985694885,"connectEnd":208.89999985694885,"connectStart":208.89999985694885,"domainLookupEnd":208.89999985694885,"domainLookupStart":208.89999985694885,"fetchStart":208.89999985694885,"redirectEnd":0,"redirectStart":0,"requestStart":266.59999990463257,"responseEnd":460.7999999523163,"responseStart":452.7999999523163,"secureConnectionStart":208.89999985694885},{"duration":234,"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":228.09999990463257,"connectEnd":228.09999990463257,"connectStart":228.09999990463257,"domainLookupEnd":228.09999990463257,"domainLookupStart":228.09999990463257,"fetchStart":228.09999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":280.09999990463257,"responseEnd":462.09999990463257,"responseStart":456.39999985694885,"secureConnectionStart":228.09999990463257},{"duration":149.5,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":512.6999998092651,"connectEnd":512.6999998092651,"connectStart":512.6999998092651,"domainLookupEnd":512.6999998092651,"domainLookupStart":512.6999998092651,"fetchStart":512.6999998092651,"redirectEnd":0,"redirectStart":0,"requestStart":627.8999998569489,"responseEnd":662.1999998092651,"responseStart":661.5999999046326,"secureConnectionStart":512.6999998092651},{"duration":185.5,"initiatorType":"script","name":"https://www.google-analytics.com/analytics.js","startTime":747.3999998569489,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":747.3999998569489,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":932.8999998569489,"responseStart":0,"secureConnectionStart":0},{"duration":215.79999995231628,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":754,"connectEnd":754,"connectStart":754,"domainLookupEnd":754,"domainLookupStart":754,"fetchStart":754,"redirectEnd":0,"redirectStart":0,"requestStart":933.1999998092651,"responseEnd":969.7999999523163,"responseStart":969.1999998092651,"secureConnectionStart":754}],"fetchStart":1,"domainLookupStart":1,"domainLookupEnd":1,"connectStart":1,"connectEnd":1,"requestStart":23,"responseStart":197,"responseEnd":228,"domLoading":200,"domInteractive":967,"domContentLoadedEventStart":967,"domContentLoadedEventEnd":1027,"domComplete":1504,"loadEventStart":1505,"loadEventEnd":1505,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":934.0999999046326},{"name":"bigPipe.sidebar-id.end","time":934.8999998569489},{"name":"bigPipe.activity-panel-pipe-id.start","time":935.0999999046326},{"name":"bigPipe.activity-panel-pipe-id.end","time":938.5},{"name":"activityTabFullyLoaded","time":1047.1999998092651}],"measures":[],"correlationId":"a9593a964dcfe7","effectiveType":"4g","downlink":9.8,"rtt":0,"serverDuration":90,"dbReadsTimeInMs":16,"dbConnsTimeInMs":24,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}
Followup to review discussion:
The optimizer removes constant expressions from GROUP BY clause. This is done by JOIN::optimize_stage2() calling remove_const(), which happens after JOIN::optimize_inner()|make_join_statistics()|estimate_post_group_cardinality() calls.
So, in estimate_post_group_cardinality() the GROUP BY is still the original one.
Examples for debugging: example-dataset-1.sql
from
(
bank,
department
) T;
with a constant table:
from
(
bank,
department
) T;