Type:
Task
Priority:
Major
Resolution:
Unresolved
Fix Version/s:
None
Consider a CTE that is not recursive, not mergeable, and used multiple times in the query:
create table ten(a int primary key );
insert into ten values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
create table t10 (a int );
insert into t10 select T.a from ten T, ten T2, ten T3;
explain
with T as (
select distinct a from t10
)
select * from T as T1, T as T2 where T1.a+T2.a>1000;
EXPLAIN shows that each use of the CTE is completely independent, as if one has copy-pasted its definition:
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 1000
1 PRIMARY <derived3> ALL NULL NULL NULL NULL 1000 Using where; Using join buffer (flat, BNL join)
3 DERIVED t10 ALL NULL NULL NULL NULL 1000 Using temporary
2 DERIVED t10 ALL NULL NULL NULL NULL 1000 Using temporary
This can cause poor performance when the CTE in question costs a lot to compute.
Note that MySQL would re-use the CTE in this case:
+----+-------------+------------+------------+------+---------------+------+---------+------+------+----------+--------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+------------+------+---------------+------+---------+------+------+----------+--------------------------------------------+
| 1 | PRIMARY | <derived2> | NULL | ALL | NULL | NULL | NULL | NULL | 1000 | 100.00 | NULL |
| 1 | PRIMARY | <derived2> | NULL | ALL | NULL | NULL | NULL | NULL | 1000 | 100.00 | Using where; Using join buffer (hash join) |
| 2 | DERIVED | t10 | NULL | ALL | NULL | NULL | NULL | NULL | 1000 | 100.00 | Using temporary |
+----+-------------+------------+------------+------+---------------+------+---------+------+------+----------+--------------------------------------------+
It's interesting that if one defines a VIEW instead of the CTE and uses it multiple times, both MariaDB and MySQL will materialize the view as many times as it is used.
One can say that sharing the CTE may interfere with other optimizations, like derived condition pushdown or derived_with_keys , or Split-Materialized .
A suggestion off the top of the head: if neither of these optimizations were employed, can we still re-use the CTE?
{"report":{"fcp":870.1000000238419,"ttfb":172,"pageVisibility":"visible","entityId":131512,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":1,"journeyId":"16b4fca3-ae67-4e35-bfcf-333d4178300d","navigationType":0,"readyForUser":942.9000000357628,"redirectCount":0,"resourceLoadedEnd":778.8000000119209,"resourceLoadedStart":177.30000001192093,"resourceTiming":[{"duration":161.9000000357628,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2bv2/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":177.30000001192093,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":177.30000001192093,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":339.2000000476837,"responseStart":0,"secureConnectionStart":0},{"duration":162,"initiatorType":"link","name":"https://jira.mariadb.org/s/7ebd35e77e471bc30ff0eba799ebc151-CDN/lu2bv2/820016/12ta74/2380add21a9a1006587582385952de73/_/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":177.5,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":177.5,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":339.5,"responseStart":0,"secureConnectionStart":0},{"duration":170.79999995231628,"initiatorType":"script","name":"https://jira.mariadb.org/s/e9b27a47da5fb0f74a35acd57e9847fb-CDN/lu2bv2/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":177.70000004768372,"connectEnd":177.70000004768372,"connectStart":177.70000004768372,"domainLookupEnd":177.70000004768372,"domainLookupStart":177.70000004768372,"fetchStart":177.70000004768372,"redirectEnd":0,"redirectStart":0,"requestStart":177.70000004768372,"responseEnd":348.5,"responseStart":348.5,"secureConnectionStart":177.70000004768372},{"duration":226.39999997615814,"initiatorType":"script","name":"https://jira.mariadb.org/s/c32eb0da7ad9831253f8397e6cc26afd-CDN/lu2bv2/820016/12ta74/2380add21a9a1006587582385952de73/_/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":177.9000000357628,"connectEnd":177.9000000357628,"connectStart":177.9000000357628,"domainLookupEnd":177.9000000357628,"domainLookupStart":177.9000000357628,"fetchStart":177.9000000357628,"redirectEnd":0,"redirectStart":0,"requestStart":177.9000000357628,"responseEnd":404.30000001192093,"responseStart":404.30000001192093,"secureConnectionStart":177.9000000357628},{"duration":229.80000001192093,"initiatorType":"script","name":"https://jira.mariadb.org/s/bc0bcb146314416123c992714ee00ff7-CDN/lu2bv2/820016/12ta74/c92c0caa9a024ae85b0ebdbed7fb4bd7/_/download/contextbatch/js/atl.global,-_super/batch.js?locale=en","startTime":178.10000002384186,"connectEnd":178.10000002384186,"connectStart":178.10000002384186,"domainLookupEnd":178.10000002384186,"domainLookupStart":178.10000002384186,"fetchStart":178.10000002384186,"redirectEnd":0,"redirectStart":0,"requestStart":178.10000002384186,"responseEnd":407.9000000357628,"responseStart":407.9000000357628,"secureConnectionStart":178.10000002384186},{"duration":230.10000002384186,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bv2/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-en/jira.webresources:calendar-en.js","startTime":178.30000001192093,"connectEnd":178.30000001192093,"connectStart":178.30000001192093,"domainLookupEnd":178.30000001192093,"domainLookupStart":178.30000001192093,"fetchStart":178.30000001192093,"redirectEnd":0,"redirectStart":0,"requestStart":178.30000001192093,"responseEnd":408.4000000357628,"responseStart":408.4000000357628,"secureConnectionStart":178.30000001192093},{"duration":230.30000001192093,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bv2/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-localisation-moment/jira.webresources:calendar-localisation-moment.js","startTime":178.4000000357628,"connectEnd":178.4000000357628,"connectStart":178.4000000357628,"domainLookupEnd":178.4000000357628,"domainLookupStart":178.4000000357628,"fetchStart":178.4000000357628,"redirectEnd":0,"redirectStart":0,"requestStart":178.4000000357628,"responseEnd":408.7000000476837,"responseStart":408.7000000476837,"secureConnectionStart":178.4000000357628},{"duration":310.30000001192093,"initiatorType":"link","name":"https://jira.mariadb.org/s/b04b06a02d1959df322d9cded3aeecc1-CDN/lu2bv2/820016/12ta74/a2ff6aa845ffc9a1d22fe23d9ee791fc/_/download/contextbatch/css/jira.global.look-and-feel,-_super/batch.css","startTime":178.5,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":178.5,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":488.80000001192093,"responseStart":0,"secureConnectionStart":0},{"duration":230.30000001192093,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":178.80000001192093,"connectEnd":178.80000001192093,"connectStart":178.80000001192093,"domainLookupEnd":178.80000001192093,"domainLookupStart":178.80000001192093,"fetchStart":178.80000001192093,"redirectEnd":0,"redirectStart":0,"requestStart":178.80000001192093,"responseEnd":409.10000002384186,"responseStart":409.10000002384186,"secureConnectionStart":178.80000001192093},{"duration":310.0999999642372,"initiatorType":"link","name":"https://jira.mariadb.org/s/3ac36323ba5e4eb0af2aa7ac7211b4bb-CDN/lu2bv2/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":178.9000000357628,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":178.9000000357628,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":489,"responseStart":0,"secureConnectionStart":0},{"duration":230.70000004768372,"initiatorType":"script","name":"https://jira.mariadb.org/s/719848dd97ebe0663199f49a3936487a-CDN/lu2bv2/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":179,"connectEnd":179,"connectStart":179,"domainLookupEnd":179,"domainLookupStart":179,"fetchStart":179,"redirectEnd":0,"redirectStart":0,"requestStart":179,"responseEnd":409.7000000476837,"responseStart":409.7000000476837,"secureConnectionStart":179},{"duration":578.5,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bv2/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-js/jira.webresources:bigpipe-js.js","startTime":179.70000004768372,"connectEnd":179.70000004768372,"connectStart":179.70000004768372,"domainLookupEnd":179.70000004768372,"domainLookupStart":179.70000004768372,"fetchStart":179.70000004768372,"redirectEnd":0,"redirectStart":0,"requestStart":179.70000004768372,"responseEnd":758.2000000476837,"responseStart":758.2000000476837,"secureConnectionStart":179.70000004768372},{"duration":574.8000000119209,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bv2/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-init/jira.webresources:bigpipe-init.js","startTime":184.10000002384186,"connectEnd":184.10000002384186,"connectStart":184.10000002384186,"domainLookupEnd":184.10000002384186,"domainLookupStart":184.10000002384186,"fetchStart":184.10000002384186,"redirectEnd":0,"redirectStart":0,"requestStart":184.10000002384186,"responseEnd":758.9000000357628,"responseStart":758.9000000357628,"secureConnectionStart":184.10000002384186},{"duration":218.80000001192093,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":499.80000001192093,"connectEnd":499.80000001192093,"connectStart":499.80000001192093,"domainLookupEnd":499.80000001192093,"domainLookupStart":499.80000001192093,"fetchStart":499.80000001192093,"redirectEnd":0,"redirectStart":0,"requestStart":499.80000001192093,"responseEnd":718.6000000238419,"responseStart":718.6000000238419,"secureConnectionStart":499.80000001192093},{"duration":19.600000023841858,"initiatorType":"link","name":"https://jira.mariadb.org/s/d5715adaadd168a9002b108b2b039b50-CDN/lu2bv2/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":759,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":759,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":778.6000000238419,"responseStart":0,"secureConnectionStart":0},{"duration":19.30000001192093,"initiatorType":"link","name":"https://jira.mariadb.org/s/50bc9be5bfead1a25e72c1a9338c94f6-CDN/lu2bv2/820016/12ta74/e108c7645258ccb43280ed3404e3e949/_/download/contextbatch/css/com.atlassian.jira.plugins.jira-development-integration-plugin:0,-_super,-jira.view.issue,-jira.global,-jira.general,-jira.browse.project,-project.issue.navigator,-atl.general/batch.css?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&slack-enabled=true","startTime":759.5,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":759.5,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":778.8000000119209,"responseStart":0,"secureConnectionStart":0},{"duration":260.60000002384186,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bv2/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":760.5,"connectEnd":760.5,"connectStart":760.5,"domainLookupEnd":760.5,"domainLookupStart":760.5,"fetchStart":760.5,"redirectEnd":0,"redirectStart":0,"requestStart":760.5,"responseEnd":1021.1000000238419,"responseStart":1021.1000000238419,"secureConnectionStart":760.5},{"duration":264.5,"initiatorType":"script","name":"https://jira.mariadb.org/s/53a43b6764f587426c7bb9a150184c00-CDN/lu2bv2/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":761,"connectEnd":761,"connectStart":761,"domainLookupEnd":761,"domainLookupStart":761,"fetchStart":761,"redirectEnd":0,"redirectStart":0,"requestStart":761,"responseEnd":1025.5,"responseStart":1025.5,"secureConnectionStart":761},{"duration":265.80000001192093,"initiatorType":"script","name":"https://jira.mariadb.org/s/630dda803a823fdd5771c4338338e018-CDN/lu2bv2/820016/12ta74/e108c7645258ccb43280ed3404e3e949/_/download/contextbatch/js/com.atlassian.jira.plugins.jira-development-integration-plugin:0,-_super,-jira.view.issue,-jira.global,-jira.general,-jira.browse.project,-project.issue.navigator,-atl.general/batch.js?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&locale=en&slack-enabled=true","startTime":761.3000000119209,"connectEnd":761.3000000119209,"connectStart":761.3000000119209,"domainLookupEnd":761.3000000119209,"domainLookupStart":761.3000000119209,"fetchStart":761.3000000119209,"redirectEnd":0,"redirectStart":0,"requestStart":761.3000000119209,"responseEnd":1027.1000000238419,"responseStart":1027.1000000238419,"secureConnectionStart":761.3000000119209}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":15,"responseStart":172,"responseEnd":178,"domLoading":175,"domInteractive":1000,"domContentLoadedEventStart":1000,"domContentLoadedEventEnd":1045,"domComplete":1894,"loadEventStart":1894,"loadEventEnd":1895,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":980.2000000476837},{"name":"bigPipe.sidebar-id.end","time":981},{"name":"bigPipe.activity-panel-pipe-id.start","time":981.2000000476837},{"name":"bigPipe.activity-panel-pipe-id.end","time":983.8000000119209},{"name":"activityTabFullyLoaded","time":1059.2000000476837}],"measures":[],"correlationId":"a2a16e30a9e48e","effectiveType":"4g","downlink":10,"rtt":0,"serverDuration":92,"dbReadsTimeInMs":9,"dbConnsTimeInMs":18,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}