Bug #8539 MySQL choose ref type, though range is more efficient
Submitted: 16 Feb 2005 11:07 Modified: 9 Mar 2005 12:37
Reporter: Victoria Reznichenko Email Updates:
Status: Duplicate Impact on me:
None 
Category:MySQL Server Severity:S3 (Non-critical)
Version:4.0 OS:Linux (Linux)
Assigned to: Assigned Account CPU Architecture:Any

[16 Feb 2005 11:07] Victoria Reznichenko
Description:
MySQL avoids ranged scan if there is another possible index, even if ranged scan is more efficient in this case.

EXPLAIN SELECTs:

mysql>  desc select count(*) from inc_performance where time_start >= '2005-02-04 16:30:00' AND inc_performance.time_start < '2005-02-15 16:30:00' and intv_type IN (1);
+-----------------+------+----------------------+-----------+---------+-------+-------+-------------+
| table           | type | possible_keys        | key       | key_len | ref   | rows  | Extra       |
+-----------------+------+----------------------+-----------+---------+-------+-------+-------------+
| inc_performance | ref  | time_start,intv_type | intv_type |       5 | const | 63286 | Using where |
+-----------------+------+----------------------+-----------+---------+-------+-------+-------------+
1 row in set (0.24 sec)

mysql>  select count(*) from inc_performance where time_start >= '2005-02-04 16:30:00' AND inc_performance.time_start < '2005-02-15 16:30:00' and intv_type IN (1);
+----------+
| count(*) |
+----------+
|     3168 |
+----------+
1 row in set (0.32 sec)

mysql> desc select count(*) from inc_performance use index (time_start) where time_start >= '2005-02-04 16:30:00' AND inc_performance.time_start < '2005-02-15 16:30:00' and intv_type IN (1);
+-----------------+-------+---------------+------------+---------+------+-------+-------------+
| table           | type  | possible_keys | key        | key_len | ref  | rows  | Extra       |
+-----------------+-------+---------------+------------+---------+------+-------+-------------+
| inc_performance | range | time_start    | time_start |       9 | NULL | 10455 | Using where |
+-----------------+-------+---------------+------------+---------+------+-------+-------------+
1 row in set (0.00 sec)

mysql> select count(*) from inc_performance use index (time_start) where time_start >= '2005-02-04 16:30:00' AND inc_performance.time_start < '2005-02-15 16:30:00' and intv_type IN (1);
+----------+
| count(*) |
+----------+
|     3168 |
+----------+
1 row in set (0.14 sec)

As you can see range for time_start is better than index for intv_type.

How to repeat:
1. restore uploaded table inc_performance.sql
2. run the above queries.
[16 Feb 2005 11:12] MySQL Verification Team
Table was uploaded to the ftp server as bug_8539.zip
[4 Mar 2005 11:50] Sergey Petrunya
The problem seems to be that if it is possible to read table with  
  a) range on index idx1 
  b) ref on another index idx2 
ref on idx2 is always used. (i.e. the choice is not cost based)
[4 Mar 2005 12:06] Sergey Petrunya
See also BUG#8749
[8 Mar 2005 12:17] Timour Katchaounov
This bug is duplicate of BUG#7425.
[8 Mar 2005 12:19] Timour Katchaounov
Sorry, my mistake - this bug is duplicate of BUG#8749.