Bug #55979 | Key not used if IN() list contains mix of strings and integers | ||
---|---|---|---|
Submitted: | 13 Aug 2010 22:05 | Modified: | 16 Aug 2010 8:44 |
Reporter: | Bruno Barberi Gnecco | Email Updates: | |
Status: | Verified | Impact on me: | |
Category: | MySQL Server: Optimizer | Severity: | S5 (Performance) |
Version: | 5.1.39 | OS: | Linux |
Assigned to: | CPU Architecture: | Any | |
Tags: | performance |
[13 Aug 2010 22:05]
Bruno Barberi Gnecco
[14 Aug 2010 11:18]
James Day
Bruno, don't use strings when your primary key is an integer. Remove all of the quotes.
[16 Aug 2010 8:44]
Hartmut Holzgraefe
Also hits SELECTS, so i took the freedom to update the bug synopsis. How to reproduce easily: DROP TABLE IF EXISTS t1; CREATE TABLE `t1` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `totalhits` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ); INSERT INTO t1 SELECT NULL, 0; INSERT INTO t1 SELECT NULL, 0 FROM t1; INSERT INTO t1 SELECT NULL, 0 FROM t1; INSERT INTO t1 SELECT NULL, 0 FROM t1; INSERT INTO t1 SELECT NULL, 0 FROM t1; INSERT INTO t1 SELECT NULL, 0 FROM t1; INSERT INTO t1 SELECT NULL, 0 FROM t1; INSERT INTO t1 SELECT NULL, 0 FROM t1; INSERT INTO t1 SELECT NULL, 0 FROM t1; INSERT INTO t1 SELECT NULL, 0 FROM t1; INSERT INTO t1 SELECT NULL, 0 FROM t1; EXPLAIN SELECT * FROM t1 WHERE id IN (1,2); EXPLAIN SELECT * FROM t1 WHERE id IN ('1','2'); EXPLAIN SELECT * FROM t1 WHERE id IN (1,'2'); Result: mysql> EXPLAIN SELECT * FROM t1 WHERE id IN (1,2); +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+ | 1 | SIMPLE | t1 | range | PRIMARY | PRIMARY | 4 | NULL | 2 | Using where | +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+ 1 row in set (0.00 sec) mysql> EXPLAIN SELECT * FROM t1 WHERE id IN ('1','2'); +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+ | 1 | SIMPLE | t1 | range | PRIMARY | PRIMARY | 4 | NULL | 2 | Using where | +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+ 1 row in set (0.00 sec) mysql> EXPLAIN SELECT * FROM t1 WHERE id IN (1,'2'); +----+-------------+-------+------+---------------+------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+------+-------------+ | 1 | SIMPLE | t1 | ALL | PRIMARY | NULL | NULL | NULL | 1024 | Using where | +----+-------------+-------+------+---------------+------+---------+------+------+-------------+ 1 row in set (0.00 sec) So the primary key on the integer column `id` can be used as index if the values in the IN() list are either all integers or all strings, but not when mixing integers and strings.
[23 Aug 2010 16:08]
Marc ALFF
Category "Performance schema" is for the performance schema (5.5), not for general performance issues. Changing category to MySQL server / optimizer