Bug #84024 | Optimizer thinks clustered primary key is not covering | ||
---|---|---|---|
Submitted: | 1 Dec 2016 2:40 | Modified: | 2 Dec 2016 9:07 |
Reporter: | Manuel Ung | Email Updates: | |
Status: | Verified | Impact on me: | |
Category: | MySQL Server: Optimizer | Severity: | S3 (Non-critical) |
Version: | 5.7.16, 5.6.34 | OS: | Any |
Assigned to: | CPU Architecture: | Any |
[1 Dec 2016 2:40]
Manuel Ung
[1 Dec 2016 4:25]
MySQL Verification Team
Hello Manuel, Thank you for the report and test case. Verified as described with 5.7.16 build. Thanks, Umesh
[1 Dec 2016 8:46]
Øystein Grøvlen
AFAICT, for the given query, the reason table scan is preferred over index look-up is that the table contains only one row. When there is only a few rows in the table, table scan with join-buffering is probably cheaper than primary index look-ups. If I increase the number of rows, PK look-ups will be used: mysql> insert into t3 select i+1, j from t3; Query OK, 1 row affected (0,00 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> insert into t3 select i+2, j from t3; Query OK, 2 rows affected (0,00 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> insert into t3 select i+4, j from t3; Query OK, 4 rows affected (0,00 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> explain select t3.j from (t1 left join t2 on t1.i = t2.i) left join t3 on t1.i = t3.i where t1.j > 0 and t1.j < 5000; +----+-------------+-------+------------+--------+---------------+---------+---------+---------------+------+----------+--------------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+--------+---------------+---------+---------+---------------+------+----------+--------------------------+ | 1 | SIMPLE | t1 | NULL | index | j | j | 5 | NULL | 4096 | 100.00 | Using where; Using index | | 1 | SIMPLE | t2 | NULL | eq_ref | PRIMARY | PRIMARY | 4 | bug84024.t1.i | 1 | 100.00 | Using index | | 1 | SIMPLE | t3 | NULL | eq_ref | PRIMARY | PRIMARY | 4 | bug84024.t1.i | 1 | 100.00 | NULL | +----+-------------+-------+------------+--------+---------------+---------+---------+---------------+------+----------+--------------------------+ 3 rows in set, 1 warning (0,00 sec)
[1 Dec 2016 22:22]
Manuel Ung
Maybe it makes sense? Although consider that: 1. You'll get the more "expensive" plan if you use a covered key instead, so that's kind of strange. 2. In practice, creating the join buffer might be more expensive than rereading the low cardinality t3 table (especially if it's empty).
[2 Dec 2016 8:50]
Øystein Grøvlen
Hi Manuel, I can agree that the cost model for join buffering is not perfect. If so, I think we should improve this model, not rely on heuristics about covering indexes versus table scan.
[2 Dec 2016 9:07]
Manuel Ung
Improving the cost model would be great if possible. Changing the heuristic just seemed like an easier/safe workaround since it takes effect on covering keys already.