| Bug #35819 | Multi column IN does not use index | ||
|---|---|---|---|
| Submitted: | 4 Apr 2008 2:32 | Modified: | 21 Oct 2009 5:08 |
| Reporter: | Peter Zaitsev (Basic Quality Contributor) | Email Updates: | |
| Status: | Duplicate | Impact on me: | |
| Category: | MySQL Server: Optimizer | Severity: | S3 (Non-critical) |
| Version: | 5.0.54, 5.0.56 | OS: | Any |
| Assigned to: | Assigned Account | CPU Architecture: | Any |
| Tags: | qc | ||
[4 Apr 2008 2:32]
Peter Zaitsev
[5 Apr 2008 3:56]
Mark Callaghan
This is not the only feature missing from IN-list optimization -- http://bugs.mysql.com/bug.php?id=31188
[5 Apr 2008 11:21]
Valeriy Kravchuk
Thank you for a problem report. Indeed, optimizer just refuses to use the index, so it may be a missing feature:
mysql> CREATE TABLE `rt` (
-> `i` int(11) default NULL,
-> `j` int(11) default NULL,
-> KEY `i` (`i`)
-> ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Query OK, 0 rows affected (0.09 sec)
mysql> insert into rt values(1,1);
Query OK, 1 row affected (0.03 sec)
mysql> insert into rt select rand()*1000, rand()*1000 from rt;
Query OK, 1 row affected (0.06 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into rt select rand()*1000, rand()*1000 from rt;
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
...
mysql> insert into rt select rand()*1000, rand()*1000 from rt;
Query OK, 32768 rows affected (0.50 sec)
Records: 32768 Duplicates: 0 Warnings: 0
mysql> analyze table rt;
+---------+---------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+---------+---------+----------+----------+
| test.rt | analyze | status | OK |
+---------+---------+----------+----------+
1 row in set (0.03 sec)
mysql> explain select * from rt where (i,j) in ((266,841),(407,511))\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: rt
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 65536
Extra: Using where
1 row in set (0.00 sec)
mysql> explain select * from rt force index(i) where (i,j) in ((266,841),(407,51
1))\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: rt
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 65536
Extra: Using where
1 row in set (0.00 sec)
mysql> explain select * from rt where (i) in (266,407)\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: rt
type: range
possible_keys: i
key: i
key_len: 5
ref: NULL
rows: 109
Extra: Using where
1 row in set (0.02 sec)
Workaround in this case is simple:
mysql> explain select * from rt where (i=266 and j=841) or (i=407 and j=511)\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: rt
type: range
possible_keys: i
key: i
key_len: 5
ref: NULL
rows: 109
Extra: Using where
1 row in set (0.03 sec)
But with longer IN() lists it will be problematic.
[26 Jun 2008 15:43]
Sergey Petrunya
This is an additional request for a feature optimization. The target version is MySQL 6.x.
[21 Oct 2009 5:08]
Timothy Smith
Duplicate of Bug#31188
