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":738.5,"ttfb":224,"pageVisibility":"visible","entityId":70490,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":1,"journeyId":"771d2412-9fa3-4624-8d6d-cb49d8bd3e68","navigationType":0,"readyForUser":818.4000000059605,"redirectCount":0,"resourceLoadedEnd":835.8000000119209,"resourceLoadedStart":229.40000000596046,"resourceTiming":[{"duration":8.800000011920929,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2bv2/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":229.40000000596046,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":229.40000000596046,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":238.2000000178814,"responseStart":0,"secureConnectionStart":0},{"duration":8.899999976158142,"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":229.7000000178814,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":229.7000000178814,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":238.59999999403954,"responseStart":0,"secureConnectionStart":0},{"duration":75.5,"initiatorType":"script","name":"https://jira.mariadb.org/s/e9b27a47da5fb0f74a35acd57e9847fb-CDN/lu2bv2/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":229.80000001192093,"connectEnd":229.80000001192093,"connectStart":229.80000001192093,"domainLookupEnd":229.80000001192093,"domainLookupStart":229.80000001192093,"fetchStart":229.80000001192093,"redirectEnd":0,"redirectStart":0,"requestStart":229.80000001192093,"responseEnd":305.30000001192093,"responseStart":305.30000001192093,"secureConnectionStart":229.80000001192093},{"duration":149.09999999403954,"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":230,"connectEnd":230,"connectStart":230,"domainLookupEnd":230,"domainLookupStart":230,"fetchStart":230,"redirectEnd":0,"redirectStart":0,"requestStart":230,"responseEnd":379.09999999403954,"responseStart":379.09999999403954,"secureConnectionStart":230},{"duration":152,"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":230.30000001192093,"connectEnd":230.30000001192093,"connectStart":230.30000001192093,"domainLookupEnd":230.30000001192093,"domainLookupStart":230.30000001192093,"fetchStart":230.30000001192093,"redirectEnd":0,"redirectStart":0,"requestStart":230.30000001192093,"responseEnd":382.30000001192093,"responseStart":382.30000001192093,"secureConnectionStart":230.30000001192093},{"duration":152.30000001192093,"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":230.40000000596046,"connectEnd":230.40000000596046,"connectStart":230.40000000596046,"domainLookupEnd":230.40000000596046,"domainLookupStart":230.40000000596046,"fetchStart":230.40000000596046,"redirectEnd":0,"redirectStart":0,"requestStart":230.40000000596046,"responseEnd":382.7000000178814,"responseStart":382.7000000178814,"secureConnectionStart":230.40000000596046},{"duration":152.40000000596046,"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":230.59999999403954,"connectEnd":230.59999999403954,"connectStart":230.59999999403954,"domainLookupEnd":230.59999999403954,"domainLookupStart":230.59999999403954,"fetchStart":230.59999999403954,"redirectEnd":0,"redirectStart":0,"requestStart":230.59999999403954,"responseEnd":383,"responseStart":383,"secureConnectionStart":230.59999999403954},{"duration":212.7999999821186,"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":230.7000000178814,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":230.7000000178814,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":443.5,"responseStart":0,"secureConnectionStart":0},{"duration":152.59999999403954,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":230.80000001192093,"connectEnd":230.80000001192093,"connectStart":230.80000001192093,"domainLookupEnd":230.80000001192093,"domainLookupStart":230.80000001192093,"fetchStart":230.80000001192093,"redirectEnd":0,"redirectStart":0,"requestStart":230.80000001192093,"responseEnd":383.40000000596046,"responseStart":383.40000000596046,"secureConnectionStart":230.80000001192093},{"duration":212.40000000596046,"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":231.09999999403954,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":231.09999999403954,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":443.5,"responseStart":0,"secureConnectionStart":0},{"duration":152.7999999821186,"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":231.2000000178814,"connectEnd":231.2000000178814,"connectStart":231.2000000178814,"domainLookupEnd":231.2000000178814,"domainLookupStart":231.2000000178814,"fetchStart":231.2000000178814,"redirectEnd":0,"redirectStart":0,"requestStart":231.2000000178814,"responseEnd":384,"responseStart":384,"secureConnectionStart":231.2000000178814},{"duration":602.4000000059605,"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":232,"connectEnd":232,"connectStart":232,"domainLookupEnd":232,"domainLookupStart":232,"fetchStart":232,"redirectEnd":0,"redirectStart":0,"requestStart":232,"responseEnd":834.4000000059605,"responseStart":834.4000000059605,"secureConnectionStart":232},{"duration":603.7000000178814,"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":232.09999999403954,"connectEnd":232.09999999403954,"connectStart":232.09999999403954,"domainLookupEnd":232.09999999403954,"domainLookupStart":232.09999999403954,"fetchStart":232.09999999403954,"redirectEnd":0,"redirectStart":0,"requestStart":232.09999999403954,"responseEnd":835.8000000119209,"responseStart":835.8000000119209,"secureConnectionStart":232.09999999403954},{"duration":380.2000000178814,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":454.5,"connectEnd":454.5,"connectStart":454.5,"domainLookupEnd":454.5,"domainLookupStart":454.5,"fetchStart":454.5,"redirectEnd":0,"redirectStart":0,"requestStart":454.5,"responseEnd":834.7000000178814,"responseStart":834.7000000178814,"secureConnectionStart":454.5},{"duration":145.40000000596046,"initiatorType":"script","name":"https://www.google-analytics.com/analytics.js","startTime":733,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":733,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":878.4000000059605,"responseStart":0,"secureConnectionStart":0}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":44,"responseStart":224,"responseEnd":226,"domLoading":227,"domInteractive":941,"domContentLoadedEventStart":941,"domContentLoadedEventEnd":987,"domComplete":1160,"loadEventStart":1160,"loadEventEnd":1161,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":920.8000000119209},{"name":"bigPipe.sidebar-id.end","time":921.5999999940395},{"name":"bigPipe.activity-panel-pipe-id.start","time":921.8000000119209},{"name":"bigPipe.activity-panel-pipe-id.end","time":922.8000000119209},{"name":"activityTabFullyLoaded","time":1003.2000000178814}],"measures":[],"correlationId":"e6918568718a66","effectiveType":"4g","downlink":10,"rtt":0,"serverDuration":107,"dbReadsTimeInMs":16,"dbConnsTimeInMs":23,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}