[MDEV-10006] optimizer doesn't convert outer join to inner on views with WHERE clause Created: 2016-04-28 Updated: 2017-05-29 Resolved: 2016-05-11 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | Optimizer |
| Affects Version/s: | 10.1.11 |
| Fix Version/s: | 10.1.15 |
| Type: | Bug | Priority: | Major |
| Reporter: | Hartmut Holzgraefe | Assignee: | Sergei Petrunia |
| Resolution: | Fixed | Votes: | 0 |
| Labels: | None | ||
| Sub-Tasks: |
|
| Description |
|
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:
The plans for the plain query, and for the VIEW without a WHERE condition inside the definition, look like this: Plain query:
View without WHERE:
So these two are identical. For the VIEW with the extra WHERE 1=1 in its definition the plan looks like this though:
So here table reordering didn't happen, and a "Using where" full scan of the VIEW results has to be done .... |
| Comments |
| Comment by Sergei Petrunia [ 2016-05-04 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Compaing these two queries: EXPLAIN EXTENDED SELECT * FROM v1 WHERE v3 = 4; relevant parts of EXPLAIN EXTENDED output:
So, it looks as if outer-to-inner join conversion worked the same way for both queries. However, if I put a breakpoint into choose_plan(), before the my_qsort2 call, I see the difference: EXPLAIN EXTENDED SELECT * FROM v1 WHERE v3 = 4;
EXPLAIN EXTENDED SELECT * FROM v2 WHERE v3 = 4;
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2016-05-04 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
That is, the join order of "t3,t1,t2" will not be picked for "FROM v3" query, because the optimizer receives a constraint that t3 must be after table t2. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2016-05-05 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
The dependency comes from t3's TABLE::dep_tables. t3->dep_tables=2 (this is t2). TABLE::dep_tables is filled by simplify_joins. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2016-05-05 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
VIEW handling is unified with derived table handling, so the issue can be seen
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2016-05-05 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Debugging simplify_joins:
same content here: https://gist.github.com/spetrunia/d531a08dc748fb01c694710885ac2bc7 Summary:
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2016-05-05 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
This patch fixes the issue and passes the test suite:
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2016-05-05 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
elenst, I need
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Elena Stepanova [ 2016-05-11 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
I ran comparison tests on the patched version against 10.1 without the patch and transformation tests with ExecuteAsView, haven't got any mismatches. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2016-05-11 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
No changes were requested during the code review. The patch will be pushed as is. |