Let's take a small example (from main/derived_split_innodb.test) and look how LATERAL DERIVED is shown in EXPLAIN:
CREATE TABLE t1 (
|
n1 int(10) NOT NULL,
|
n2 int(10) NOT NULL,
|
c1 char(1) NOT NULL,
|
KEY c1 (c1),
|
KEY n1_c1_n2 (n1,c1,n2)
|
) ENGINE=InnoDB;
|
INSERT INTO t1 VALUES (0, 2, 'a'), (1, 3, 'a');
|
ANALYZE TABLE t1;
|
EXPLAIN SELECT t1.n1 FROM t1, (SELECT n1, n2 FROM t1 WHERE c1 = 'a' GROUP BY n1) as t
|
WHERE t.n1 = t1.n1 AND t.n2 = t1.n2 AND c1 = 'a' GROUP BY n1;
|
+------+-----------------+------------+-------+---------------+----------+---------+-------------------+------+--------------------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|
+------+-----------------+------------+-------+---------------+----------+---------+-------------------+------+--------------------------+
|
| 1 | PRIMARY | t1 | index | c1,n1_c1_n2 | n1_c1_n2 | 9 | NULL | 2 | Using where; Using index |
|
| 1 | PRIMARY | <derived2> | ref | key0 | key0 | 8 | j5.t1.n1,j5.t1.n2 | 2 | |
|
| 2 | LATERAL DERIVED | t1 | ref | c1,n1_c1_n2 | n1_c1_n2 | 4 | j5.t1.n1 | 1 | Using where; Using index |
|
+------+-----------------+------------+-------+---------------+----------+---------+-------------------+------+--------------------------+
|
Good so far
Now, try EXPLAIN FORMAT=JSON
{
|
"query_block": {
|
"select_id": 1,
|
"table": {
|
"table_name": "t1",
|
"access_type": "index",
|
"possible_keys": ["c1", "n1_c1_n2"],
|
"key": "n1_c1_n2",
|
"key_length": "9",
|
"used_key_parts": ["n1", "c1", "n2"],
|
"rows": 2,
|
"filtered": 100,
|
"attached_condition": "t1.c1 <=> 'a' and t1.c1 = 'a'",
|
"using_index": true
|
},
|
"table": {
|
"table_name": "<derived2>",
|
"access_type": "ref",
|
"possible_keys": ["key0"],
|
"key": "key0",
|
"key_length": "8",
|
"used_key_parts": ["n1", "n2"],
|
"ref": ["j5.t1.n1", "j5.t1.n2"],
|
"rows": 2,
|
"filtered": 100,
|
"materialized": {
|
"query_block": {
|
"select_id": 2,
|
"table": {
|
"table_name": "t1",
|
"access_type": "ref",
|
"possible_keys": ["c1", "n1_c1_n2"],
|
"key": "n1_c1_n2",
|
"key_length": "4",
|
"used_key_parts": ["n1"],
|
"ref": ["j5.t1.n1"],
|
"rows": 1,
|
"filtered": 100,
|
"attached_condition": "t1.c1 = 'a'",
|
"using_index": true
|
}
|
}
|
}
|
}
|
}
|
}
|
If one is attentive enough, they can see that "materialized" node has a ref access in it which refers to "j5.t1.n1". Table "t1"
is outside the subquery, which means the subquery is using LATERAL DERIVED optimization.
But this is not at all obvious. EXPLAIN FORMAT=JSON should make it clear what optimization was used.
{"report":{"fcp":823.5,"ttfb":190.89999997615814,"pageVisibility":"visible","entityId":70490,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":1,"journeyId":"b010608a-30bb-4e1b-82f5-f76c828f4279","navigationType":0,"readyForUser":905.3000000119209,"redirectCount":0,"resourceLoadedEnd":937.6999999880791,"resourceLoadedStart":196.5,"resourceTiming":[{"duration":126,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2bv2/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":196.5,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":196.5,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":322.5,"responseStart":0,"secureConnectionStart":0},{"duration":125.89999997615814,"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":196.80000001192093,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":196.80000001192093,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":322.69999998807907,"responseStart":0,"secureConnectionStart":0},{"duration":134.5,"initiatorType":"script","name":"https://jira.mariadb.org/s/e9b27a47da5fb0f74a35acd57e9847fb-CDN/lu2bv2/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":196.89999997615814,"connectEnd":196.89999997615814,"connectStart":196.89999997615814,"domainLookupEnd":196.89999997615814,"domainLookupStart":196.89999997615814,"fetchStart":196.89999997615814,"redirectEnd":0,"redirectStart":0,"requestStart":196.89999997615814,"responseEnd":331.39999997615814,"responseStart":331.39999997615814,"secureConnectionStart":196.89999997615814},{"duration":192.19999998807907,"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":197.19999998807907,"connectEnd":197.19999998807907,"connectStart":197.19999998807907,"domainLookupEnd":197.19999998807907,"domainLookupStart":197.19999998807907,"fetchStart":197.19999998807907,"redirectEnd":0,"redirectStart":0,"requestStart":197.19999998807907,"responseEnd":389.39999997615814,"responseStart":389.39999997615814,"secureConnectionStart":197.19999998807907},{"duration":195.5999999642372,"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":197.5,"connectEnd":197.5,"connectStart":197.5,"domainLookupEnd":197.5,"domainLookupStart":197.5,"fetchStart":197.5,"redirectEnd":0,"redirectStart":0,"requestStart":197.5,"responseEnd":393.0999999642372,"responseStart":393.0999999642372,"secureConnectionStart":197.5},{"duration":196,"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":197.5999999642372,"connectEnd":197.5999999642372,"connectStart":197.5999999642372,"domainLookupEnd":197.5999999642372,"domainLookupStart":197.5999999642372,"fetchStart":197.5999999642372,"redirectEnd":0,"redirectStart":0,"requestStart":197.5999999642372,"responseEnd":393.5999999642372,"responseStart":393.5999999642372,"secureConnectionStart":197.5999999642372},{"duration":196.10000002384186,"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":197.69999998807907,"connectEnd":197.69999998807907,"connectStart":197.69999998807907,"domainLookupEnd":197.69999998807907,"domainLookupStart":197.69999998807907,"fetchStart":197.69999998807907,"redirectEnd":0,"redirectStart":0,"requestStart":197.69999998807907,"responseEnd":393.80000001192093,"responseStart":393.80000001192093,"secureConnectionStart":197.69999998807907},{"duration":273.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":197.89999997615814,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":197.89999997615814,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":471.19999998807907,"responseStart":0,"secureConnectionStart":0},{"duration":196.10000002384186,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":198.0999999642372,"connectEnd":198.0999999642372,"connectStart":198.0999999642372,"domainLookupEnd":198.0999999642372,"domainLookupStart":198.0999999642372,"fetchStart":198.0999999642372,"redirectEnd":0,"redirectStart":0,"requestStart":198.0999999642372,"responseEnd":394.19999998807907,"responseStart":394.19999998807907,"secureConnectionStart":198.0999999642372},{"duration":273.19999998807907,"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":198.19999998807907,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":198.19999998807907,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":471.39999997615814,"responseStart":0,"secureConnectionStart":0},{"duration":196.5,"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":198.30000001192093,"connectEnd":198.30000001192093,"connectStart":198.30000001192093,"domainLookupEnd":198.30000001192093,"domainLookupStart":198.30000001192093,"fetchStart":198.30000001192093,"redirectEnd":0,"redirectStart":0,"requestStart":198.30000001192093,"responseEnd":394.80000001192093,"responseStart":394.80000001192093,"secureConnectionStart":198.30000001192093},{"duration":419.69999998807907,"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":199.19999998807907,"connectEnd":199.19999998807907,"connectStart":199.19999998807907,"domainLookupEnd":199.19999998807907,"domainLookupStart":199.19999998807907,"fetchStart":199.19999998807907,"redirectEnd":0,"redirectStart":0,"requestStart":199.19999998807907,"responseEnd":618.8999999761581,"responseStart":618.8999999761581,"secureConnectionStart":199.19999998807907},{"duration":649.8999999761581,"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":199.30000001192093,"connectEnd":199.30000001192093,"connectStart":199.30000001192093,"domainLookupEnd":199.30000001192093,"domainLookupStart":199.30000001192093,"fetchStart":199.30000001192093,"redirectEnd":0,"redirectStart":0,"requestStart":199.30000001192093,"responseEnd":849.1999999880791,"responseStart":849.1999999880791,"secureConnectionStart":199.30000001192093},{"duration":136.89999997615814,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":482.30000001192093,"connectEnd":482.30000001192093,"connectStart":482.30000001192093,"domainLookupEnd":482.30000001192093,"domainLookupStart":482.30000001192093,"fetchStart":482.30000001192093,"redirectEnd":0,"redirectStart":0,"requestStart":482.30000001192093,"responseEnd":619.1999999880791,"responseStart":619.1999999880791,"secureConnectionStart":482.30000001192093},{"duration":206.5,"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":731.1999999880791,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":731.1999999880791,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":937.6999999880791,"responseStart":0,"secureConnectionStart":0},{"duration":243.70000004768372,"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":732.0999999642372,"connectEnd":732.0999999642372,"connectStart":732.0999999642372,"domainLookupEnd":732.0999999642372,"domainLookupStart":732.0999999642372,"fetchStart":732.0999999642372,"redirectEnd":0,"redirectStart":0,"requestStart":732.0999999642372,"responseEnd":975.8000000119209,"responseStart":975.8000000119209,"secureConnectionStart":732.0999999642372},{"duration":249.9000000357628,"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":732.3999999761581,"connectEnd":732.3999999761581,"connectStart":732.3999999761581,"domainLookupEnd":732.3999999761581,"domainLookupStart":732.3999999761581,"fetchStart":732.3999999761581,"redirectEnd":0,"redirectStart":0,"requestStart":732.3999999761581,"responseEnd":982.3000000119209,"responseStart":982.3000000119209,"secureConnectionStart":732.3999999761581}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":27,"responseStart":191,"responseEnd":194,"domLoading":194,"domInteractive":960,"domContentLoadedEventStart":960,"domContentLoadedEventEnd":998,"domComplete":1804,"loadEventStart":1804,"loadEventEnd":1806,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":949},{"name":"bigPipe.sidebar-id.end","time":949.8999999761581},{"name":"bigPipe.activity-panel-pipe-id.start","time":950},{"name":"bigPipe.activity-panel-pipe-id.end","time":952.3000000119209},{"name":"activityTabFullyLoaded","time":1010.8999999761581}],"measures":[],"correlationId":"eba8ce8b3ae65d","effectiveType":"4g","downlink":10,"rtt":0,"serverDuration":92,"dbReadsTimeInMs":11,"dbConnsTimeInMs":19,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}