[MDEV-6041] ORDER BY+subqueries: subquery_table.key=outer_table.col is not recongized as binding Created: 2014-04-07 Updated: 2014-04-12 Resolved: 2014-04-12 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | None |
| Affects Version/s: | 5.3.12, 5.5.36, 10.0.10 |
| Fix Version/s: | 10.0.11 |
| Type: | Bug | Priority: | Major |
| Reporter: | Sergei Petrunia | Assignee: | Sergei Petrunia |
| Resolution: | Fixed | Votes: | 0 |
| Labels: | upstream, upstream-5.5 | ||
| Issue Links: |
|
||||||||
| Description |
|
Create a test dataset:
Then, check the plan:
The subquery uses "index" access, which is very inefficient. The estimate for #rows seems to come from the LIMIT clause and is very wrong in this case. The table is InnoDB (with extended keys). The index KEY(key1) is actually KEY(key1, id). The query has a restriction on key1 which makes it constant (tsubq.key1 = t1.a). After that, ORDER BY tsubq.id is achieved automatically. The problem seems to be specifically with references to outside of subquery. If I use a constant instead, the query plan is able to use key1:
|
| Comments |
| Comment by Sergei Petrunia [ 2014-04-07 ] | ||||||||||||
|
Initial investigation: The optimizer fails to detect that sorting is not needed, because TABLE::const_key_parts[ key1] = 0. I'm wondering if the fix is to make const_key_parts=1 for cases like this. TABLE::const_key_parts is modified by
Checked by
Suggestion: change the meaning of "const_key_parts" to be "including | ||||||||||||
| Comment by Sergei Petrunia [ 2014-04-08 ] | ||||||||||||
|
Committed a fix for the above suggestion. I'll also investigate why did it pick query plan with 'index' (as opposed to query plan with 'ref' + 'Using filesort'). | ||||||||||||
| Comment by Sergei Petrunia [ 2014-04-09 ] | ||||||||||||
|
Trying the original testcase on different vesions of MySQL/MariaDB: 5.3.13-MariaDB-log
5.5.37-MariaDB-log
5.6.16-debug-log
MySQL 5.5.32, MySQL 5.5.37
| ||||||||||||
| Comment by Sergei Petrunia [ 2014-04-09 ] | ||||||||||||
|
Looking at the above: MySQL 5.5 and MariaDB 5.3/5.5 had the same "old" query plan. MySQL 5.6 has a "new" query plan. We need to find out what has caused the difference. | ||||||||||||
| Comment by Sergei Petrunia [ 2014-04-11 ] | ||||||||||||
|
The change in 5.6 was caused by this patch (it is for 5.7 but it was backported to 5.6): revno: 4502 | ||||||||||||
| Comment by Sergei Petrunia [ 2014-04-11 ] | ||||||||||||
|
The patch has no testcase but looks reasonable. | ||||||||||||
| Comment by Sergei Petrunia [ 2014-04-11 ] | ||||||||||||
|
Will address the issue with cost calculations in | ||||||||||||
| Comment by Sergei Petrunia [ 2014-04-12 ] | ||||||||||||
|
Fix pushed to 10.0 |