Bug #53149 MySQL doesn't use indexes on date column properly because of collation
Submitted: 26 Apr 2010 6:55 Modified: 26 Apr 2010 8:22
Reporter: Max Baryshnikov Email Updates:
Status: Duplicate Impact on me:
None 
Category:MySQL Server: Charsets Severity:S2 (Serious)
Version:5.5.4-m3 OS:FreeBSD
Assigned to: CPU Architecture:Any

[26 Apr 2010 6:55] Max Baryshnikov
Description:
MySQL can't use index on date column when collation_connection is set to any collation except for latin1 and binary.

How to repeat:
mysql> create table test_date_index (datecolumn date) ENGINE InnoDB;
Query OK, 0 rows affected (0.23 sec)

mysql> insert into test_date_index values ('2010-01-01');
Query OK, 1 row affected (0.00 sec)

mysql> insert into test_date_index values ('2010-01-02');
Query OK, 1 row affected (0.01 sec)

mysql> insert into test_date_index values ('2010-01-03');
Query OK, 1 row affected (0.00 sec)

mysql> set names utf8;
Query OK, 0 rows affected (0.00 sec)

mysql> alter table test_date_index add unique index (datecolumn);
Query OK, 0 rows affected (0.37 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> explain select * from test_date_index where datecolumn='2010-01-01';
+----+-------------+-----------------+-------+---------------+------------+---------+------+------+--------------------------+
| id | select_type | table           | type  | possible_keys | key        | key_len | ref  | rows | Extra                    |
+----+-------------+-----------------+-------+---------------+------------+---------+------+------+--------------------------+
|  1 | SIMPLE      | test_date_index | index | NULL          | datecolumn | 4       | NULL |    3 | Using where; Using index |
+----+-------------+-----------------+-------+---------------+------------+---------+------+------+--------------------------+
1 row in set (0.00 sec)

mysql> explain select * from test_date_index where datecolumn = BINARY '2010-01-01';
+----+-------------+-----------------+-------+---------------+------------+---------+-------+------+-------------+
| id | select_type | table           | type  | possible_keys | key        | key_len | ref   | rows | Extra       |
+----+-------------+-----------------+-------+---------------+------------+---------+-------+------+-------------+
|  1 | SIMPLE      | test_date_index | const | datecolumn    | datecolumn | 4       | const |    1 | Using index |
+----+-------------+-----------------+-------+---------------+------------+---------+-------+------+-------------+
1 row in set (0.00 sec)

mysql> set names latin1;
Query OK, 0 rows affected (0.00 sec)

mysql> explain select * from test_date_index where datecolumn='2010-01-01';
+----+-------------+-----------------+-------+---------------+------------+---------+-------+------+-------------+
| id | select_type | table           | type  | possible_keys | key        | key_len | ref   | rows | Extra       |
+----+-------------+-----------------+-------+---------------+------------+---------+-------+------+-------------+
|  1 | SIMPLE      | test_date_index | const | datecolumn    | datecolumn | 4       | const |    1 | Using index |
+----+-------------+-----------------+-------+---------------+------------+---------+-------+------+-------------+
1 row in set (0.00 sec)

mysql> set names binary;
Query OK, 0 rows affected (0.00 sec)

mysql> explain select * from test_date_index where datecolumn='2010-01-01';
+----+-------------+-----------------+-------+---------------+------------+---------+-------+------+-------------+
| id | select_type | table           | type  | possible_keys | key        | key_len | ref   | rows | Extra       |
+----+-------------+-----------------+-------+---------------+------------+---------+-------+------+-------------+
|  1 | SIMPLE      | test_date_index | const | datecolumn    | datecolumn | 4       | const |    1 | Using index |
+----+-------------+-----------------+-------+---------------+------------+---------+-------+------+-------------+
1 row in set (0.00 sec)

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.5.4-m3  |
+-----------+
1 row in set (0.00 sec)
[26 Apr 2010 8:22] Sveta Smirnova
Please do not submit the same bug more than once. An existing bug report already describes this very problem. Even if you feel that your issue is somewhat different, the resolution is likely
to be the same. Because of this, we hope you add your comments to the original bug instead.

Thank you for your interest in MySQL.

Duplicate of bug #52849