[MDEV-5293] outer join, join buffering, and order by - invalid query plan Created: 2013-11-15 Updated: 2014-01-29 Resolved: 2013-11-21 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | None |
| Affects Version/s: | 10.0.5, 5.3.12, 5.5.33a |
| Fix Version/s: | 5.5.35, 10.0.7, 5.3.13 |
| Type: | Bug | Priority: | Major |
| Reporter: | Sergei Petrunia | Assignee: | Sergei Petrunia |
| Resolution: | Fixed | Votes: | 0 |
| Labels: | None | ||
| Issue Links: |
|
||||||||||||||||
| Description |
|
A combination of outer join, join buffering, and order by causes invalid query plans to be generated. Test dataset:
Query and EXPLAIN:
The query plan uses both "Using filesort" and "Using join buffer". It will produce the results in the wrong order. I'll elaborate why:
|
| Comments |
| Comment by Sergei Petrunia [ 2013-11-15 ] |
|
Analysis: The optimizer protects itself from getting "Using join buffer" together with General approach is:
== Piece #1 == At the end of make_join_readinfo() there is a piece of code that starts If a join buffer is used to join a table the ordering by an index The code there changes "filesort on the first table" plan into a For our example, the change will not be done, because sort_by_tab==NULL, join->get_sort_by_join_tab()==NULL, because JOIN::sort_by_table==NULL. It is sort_by_table= get_sort_by_table(order, group_list, select_lex->leaf_tables); Locally, it makes sense - when get_sort_by_table() is called, it sees == Piece #2 == // Can't use sort on head table if using join buffering It switches to using "Using temporary; Using filesort" when JOIN::full_join is set by get_best_combination(), it will receive a value of join->best_positions[tablenr].use_join_buffer==true (the actual condition is more complex, but that's the idea). In turn, the value of POSITION::use_join_buffer comes from best_access_path(). best_uses_jbuf= test(!disable_jbuf && !((s->table->map & ... Note that best_uses_jbuf==FALSE when the table is an inner table of outer join. Anyhow, if this example didn't have LEFT JOIN, Piece#2 would have taken care of |
| Comment by Sergei Petrunia [ 2013-11-18 ] |
|
Committed a patch. |
| Comment by Daniel Bartholomew [ 2014-01-29 ] |
|
http://bazaar.launchpad.net/~maria-captains/maria/5.5/revision/3963.1.1 |