The optimizer can't reorder tables for OUTER JOINs, but when a later WHERE condition on a column of an outer table requires a non-NULL value it can convert the OUTER join into an INNER one, and then it can also make use of re-ordering.
The same works when the OUTER JOIN is inside a view definition, and a WHERE condition on a column of the outer table is applied on the view ... but apparently only if the view definition doesn't have a WHERE clause.
How to reproduce:
DROP TABLE IF EXISTS t1;
|
|
CREATE TABLE t1(i1 int primary key, v1 int, key(v1)) engine=myisam;
|
|
INSERT INTO t1 VALUES (1, 1);
|
INSERT INTO t1 VALUES (2, 2);
|
INSERT INTO t1 VALUES (3, 3);
|
INSERT INTO t1 VALUES (4, 4);
|
INSERT INTO t1 VALUES (5, 3);
|
INSERT INTO t1 VALUES (6, 6);
|
INSERT INTO t1 VALUES (7, 7);
|
INSERT INTO t1 VALUES (8, 8);
|
INSERT INTO t1 VALUES (9, 9);
|
|
DROP TABLE IF EXISTS t2;
|
|
CREATE TABLE t2(i2 int primary key, v2 int, key(v2)) engine=myisam;
|
|
INSERT INTO t2 VALUES (1, 1);
|
INSERT INTO t2 VALUES (2, 2);
|
INSERT INTO t2 VALUES (3, 3);
|
INSERT INTO t2 VALUES (4, 4);
|
INSERT INTO t2 VALUES (5, 3);
|
INSERT INTO t2 VALUES (6, 6);
|
INSERT INTO t2 VALUES (7, 7);
|
INSERT INTO t2 VALUES (8, 8);
|
INSERT INTO t2 VALUES (9, 9);
|
|
DROP TABLE IF EXISTS t3;
|
|
CREATE TABLE t3(i3 int primary key, v3 int, key(v3)) engine=myisam;
|
|
INSERT INTO t3 VALUES (2, 2);
|
INSERT INTO t3 VALUES (4, 4);
|
INSERT INTO t3 VALUES (6, 6);
|
INSERT INTO t3 VALUES (8, 8);
|
|
# view definition without WHERE
|
|
DROP VIEW IF EXISTS v1;
|
|
CREATE ALGORITHM=MERGE VIEW v1 AS
|
SELECT t1.i1 as i1, t1.v1 as v1,
|
t2.i2 as i2, t2.v2 as v2,
|
t3.i3 as i3, t3.v3 as v3
|
FROM t1
|
JOIN t2 on t1.i1 = t2.i2
|
LEFT JOIN t3 on t2.i2 = t3.i3
|
;
|
|
# view definition with WHERE
|
|
DROP VIEW IF EXISTS v2;
|
|
CREATE ALGORITHM=MERGE VIEW v2 AS
|
SELECT t1.i1 as i1, t1.v1 as v1,
|
t2.i2 as i2, t2.v2 as v2,
|
t3.i3 as i3, t3.v3 as v3
|
FROM t1 JOIN t2 on t1.i1 = t2.i2
|
LEFT JOIN t3 on t2.i2 = t3.i3
|
WHERE t1.i1 = t2.i2
|
AND 1 = 1
|
;
|
|
# query plan of plain query
|
|
EXPLAIN EXTENDED SELECT t1.i1 as i1, t1.v1 as v1,
|
t2.i2 as i2, t2.v2 as v2,
|
t3.i3 as i3, t3.v3 as v3
|
FROM t1
|
JOIN t2 on t1.i1 = t2.i2
|
LEFT JOIN t3 on t2.i2 = t3.i3
|
WHERE 1 = 1
|
AND t3.v3 = 4
|
;
|
|
# query plan for view without WHERE
|
|
EXPLAIN EXTENDED SELECT * FROM v1 WHERE v3 = 4;
|
|
# query plan for view with WHERE
|
|
EXPLAIN EXTENDED SELECT * FROM v2 WHERE v3 = 4;
|
The plans for the plain query, and for the VIEW without a WHERE condition inside the definition, look like this:
Plain query:
id select_type table type possible_keys key key_len ref rows filtered Extra
|
1 SIMPLE t3 ref PRIMARY,v3 v3 5 const 1 100.00
|
1 SIMPLE t1 eq_ref PRIMARY PRIMARY 4 test.t3.i3 1 100.00
|
1 SIMPLE t2 eq_ref PRIMARY PRIMARY 4 test.t3.i3 1 100.00
|
View without WHERE:
id select_type table type possible_keys key key_len ref rows filtered Extra
|
1 SIMPLE t3 ref PRIMARY,v3 v3 5 const 1 100.00
|
1 SIMPLE t1 eq_ref PRIMARY PRIMARY 4 test.t3.i3 1 100.00
|
1 SIMPLE t2 eq_ref PRIMARY PRIMARY 4 test.t3.i3 1 100.00
|
So these two are identical.
For the VIEW with the extra WHERE 1=1 in its definition the plan looks like this though:
id select_type table type possible_keys key key_len ref rows filtered Extra
|
1 SIMPLE t1 ALL PRIMARY NULL NULL NULL 9 100.00
|
1 SIMPLE t2 eq_ref PRIMARY PRIMARY 4 test.t1.i1 1 100.00
|
1 SIMPLE t3 eq_ref PRIMARY,v3 PRIMARY 4 test.t1.i1 1 100.00 Using where
|
So here table reordering didn't happen, and a "Using where" full scan of the VIEW results has to be done ....
{"report":{"fcp":761.2000000476837,"ttfb":170.10000014305115,"pageVisibility":"visible","entityId":56573,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":1,"journeyId":"8e85ce15-5d74-41e3-a7f6-0ba0a59d964c","navigationType":0,"readyForUser":858.2000000476837,"redirectCount":0,"resourceLoadedEnd":859.1000001430511,"resourceLoadedStart":176.40000009536743,"resourceTiming":[{"duration":148.79999995231628,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2bu7/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":176.40000009536743,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":176.40000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":325.2000000476837,"responseStart":0,"secureConnectionStart":0},{"duration":148.70000004768372,"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":176.60000014305115,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":176.60000014305115,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":325.30000019073486,"responseStart":0,"secureConnectionStart":0},{"duration":157.60000014305115,"initiatorType":"script","name":"https://jira.mariadb.org/s/fbf975c0cce4b1abf04784eeae9ba1f4-CDN/lu2bu7/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":176.70000004768372,"connectEnd":176.70000004768372,"connectStart":176.70000004768372,"domainLookupEnd":176.70000004768372,"domainLookupStart":176.70000004768372,"fetchStart":176.70000004768372,"redirectEnd":0,"redirectStart":0,"requestStart":176.70000004768372,"responseEnd":334.30000019073486,"responseStart":334.30000019073486,"secureConnectionStart":176.70000004768372},{"duration":258.39999985694885,"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":176.80000019073486,"connectEnd":176.80000019073486,"connectStart":176.80000019073486,"domainLookupEnd":176.80000019073486,"domainLookupStart":176.80000019073486,"fetchStart":176.80000019073486,"redirectEnd":0,"redirectStart":0,"requestStart":176.80000019073486,"responseEnd":435.2000000476837,"responseStart":435.2000000476837,"secureConnectionStart":176.80000019073486},{"duration":261.7999999523163,"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":176.90000009536743,"connectEnd":176.90000009536743,"connectStart":176.90000009536743,"domainLookupEnd":176.90000009536743,"domainLookupStart":176.90000009536743,"fetchStart":176.90000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":176.90000009536743,"responseEnd":438.7000000476837,"responseStart":438.7000000476837,"secureConnectionStart":176.90000009536743},{"duration":262.5,"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":177,"connectEnd":177,"connectStart":177,"domainLookupEnd":177,"domainLookupStart":177,"fetchStart":177,"redirectEnd":0,"redirectStart":0,"requestStart":177,"responseEnd":439.5,"responseStart":439.5,"secureConnectionStart":177},{"duration":263.7999999523163,"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":177.10000014305115,"connectEnd":177.10000014305115,"connectStart":177.10000014305115,"domainLookupEnd":177.10000014305115,"domainLookupStart":177.10000014305115,"fetchStart":177.10000014305115,"redirectEnd":0,"redirectStart":0,"requestStart":177.10000014305115,"responseEnd":440.90000009536743,"responseStart":440.90000009536743,"secureConnectionStart":177.10000014305115},{"duration":264.40000009536743,"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":177.20000004768372,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":177.20000004768372,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":441.60000014305115,"responseStart":0,"secureConnectionStart":0},{"duration":265.2999999523163,"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":177.40000009536743,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":177.40000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":442.7000000476837,"responseStart":0,"secureConnectionStart":0},{"duration":264.2000000476837,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":177.40000009536743,"connectEnd":177.40000009536743,"connectStart":177.40000009536743,"domainLookupEnd":177.40000009536743,"domainLookupStart":177.40000009536743,"fetchStart":177.40000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":177.40000009536743,"responseEnd":441.60000014305115,"responseStart":441.60000014305115,"secureConnectionStart":177.40000009536743},{"duration":265.2000000476837,"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":177.5,"connectEnd":177.5,"connectStart":177.5,"domainLookupEnd":177.5,"domainLookupStart":177.5,"fetchStart":177.5,"redirectEnd":0,"redirectStart":0,"requestStart":177.5,"responseEnd":442.7000000476837,"responseStart":442.7000000476837,"secureConnectionStart":177.5},{"duration":436,"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":178.20000004768372,"connectEnd":178.20000004768372,"connectStart":178.20000004768372,"domainLookupEnd":178.20000004768372,"domainLookupStart":178.20000004768372,"fetchStart":178.20000004768372,"redirectEnd":0,"redirectStart":0,"requestStart":178.20000004768372,"responseEnd":614.2000000476837,"responseStart":614.2000000476837,"secureConnectionStart":178.20000004768372},{"duration":623.2000000476837,"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":183,"connectEnd":183,"connectStart":183,"domainLookupEnd":183,"domainLookupStart":183,"fetchStart":183,"redirectEnd":0,"redirectStart":0,"requestStart":183,"responseEnd":806.2000000476837,"responseStart":806.2000000476837,"secureConnectionStart":183},{"duration":130.59999990463257,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":484.60000014305115,"connectEnd":484.60000014305115,"connectStart":484.60000014305115,"domainLookupEnd":484.60000014305115,"domainLookupStart":484.60000014305115,"fetchStart":484.60000014305115,"redirectEnd":0,"redirectStart":0,"requestStart":484.60000014305115,"responseEnd":615.2000000476837,"responseStart":615.2000000476837,"secureConnectionStart":484.60000014305115},{"duration":142.79999995231628,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bu7/820016/12ta74/e65b778d185daf5aee24936755b43da6/_/download/contextbatch/js/browser-metrics-plugin.contrib,-_super,-atl.general/batch.js?agile_global_admin_condition=true&jag=true&slack-enabled=true","startTime":716.3000001907349,"connectEnd":716.3000001907349,"connectStart":716.3000001907349,"domainLookupEnd":716.3000001907349,"domainLookupStart":716.3000001907349,"fetchStart":716.3000001907349,"redirectEnd":0,"redirectStart":0,"requestStart":716.3000001907349,"responseEnd":859.1000001430511,"responseStart":859.1000001430511,"secureConnectionStart":716.3000001907349},{"duration":237.5,"initiatorType":"script","name":"https://www.google-analytics.com/analytics.js","startTime":754.1000001430511,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":754.1000001430511,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":991.6000001430511,"responseStart":0,"secureConnectionStart":0}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":11,"responseStart":170,"responseEnd":174,"domLoading":173,"domInteractive":978,"domContentLoadedEventStart":978,"domContentLoadedEventEnd":1043,"domComplete":1157,"loadEventStart":1157,"loadEventEnd":1158,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":939.7000000476837},{"name":"bigPipe.sidebar-id.end","time":940.5},{"name":"bigPipe.activity-panel-pipe-id.start","time":940.7000000476837},{"name":"bigPipe.activity-panel-pipe-id.end","time":944.5},{"name":"activityTabFullyLoaded","time":1059.9000000953674}],"measures":[],"correlationId":"fef5e54150f74e","effectiveType":"4g","downlink":10,"rtt":0,"serverDuration":87,"dbReadsTimeInMs":13,"dbConnsTimeInMs":20,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}
Debugging simplify_joins:
Enter simplify_joins(join_list= join->join_list= {w2}, conds= "t3.v3 = 4")
table= w2,
table->on_expr= ((t1.i1 = t2.i2) and (1 = 1))
table->nested_join= ...
Enter simplify_joins(join_list= w2->nested_join={(nest_last_join)},
conds="((t1.i1 = t2.i2) and (1 = 1))"
{
table= (nest_last_join) // denote as $nest1
table->on_expr=NULL,
Enter simplify_joins(join_list= $nest1->nested_join={t3, (nest_last_join)},
conds="((t1.i1 = t2.i2) and (1 = 1))"
{
table= t3
table->on_expr= (t2.i2 = t3.i3)
table->outer_join=1
// we assign:
table->dep_tables |= table_on_expr_used_tables
// and it becomes:
table->dep_tables=6
// 6 = 4 (for t3 itself) + 2 (for t2)
// interesting:
table->dep_tables&= ~table->embedding->nested_join->used_tables;
// here the right part has used_tables=4
// although embedding's nested join contains t3 and a join nest
// which includes t1 and t2. I suppose, that used_tables value is incomplete
// ATM.
// Anyhow, after this, we have
table->dep_tables=2
// that is, "t3 must follow t2" which looks correct (outer join is not
// converted to inner, yet).
table= (nest_last_join); // denote as $nest2
table->on_expr= 0;
table->nested_join= {t1, t2}
Enter simplify_joins(join_list= $nest2->nested_join={t2, t1},
conds="((t1.i1 = t2.i2) and (1 = 1))"
{
table= t2
table->on_expr= "(t1.i1 = t2.i2)"
table->outer_join= 0
// inject table->on_expr into conds.
table= t1
retval= "((t1.i1 = t2.i2) and (1 = 1) and (t1.i1 = t2.i2))"
} // Leave simplify_joins(join_list=$nest2...)
// The list {t3, (nest_last_join)} is flattened into
// {t3, t2, t1}
retval= "((t1.i1 = t2.i2) and (1 = 1) and (t1.i1 = t2.i2))"
} // Leave simplify_joins(join_list=$nest1...)
// the list {nest_last_join(t3, t2, t1)} is flattened into
// {t3,t2,t1}
retval= "((t1.i1 = t2.i2) and (1 = 1) and (t1.i1 = t2.i2))"
} // Leave simplify_joins
// Now, the second call to simplify joins, with 'conds' as parameter:
Enter simplify_joins(join_list= w2->nested_join={t3, t2, t1},
conds="(t3.v3 = 4)"
{
table= t3
used_tables=4, not_null_tables=4
// we set: table->outer_join= 0;
// however, table->dep_tables=2 remains uncleaned
same content here: https://gist.github.com/spetrunia/d531a08dc748fb01c694710885ac2bc7
Summary: