I'm trying to optimize a MySQL table for a query of the form:
SELECT Col1, Col2 FROM MyTable WHERE Col3 IN('Val1','Val2','Val3',...)
I've added an index to the table:
ALTER MyTable ADD INDEX(`COL3`)
and verified that it was created, but when I analyze the query using EXPLAIN
the type is still "ALL" (indicating that all rows are retrieved and searched). What am I doing wrong?
Update:
I've tried EXPLAIN SELECT Col1, Col2 FROM MyTable WHERE Col3 = 'Val1'
. Type is still "ALL". Here are the actual CREATE TABLE
, ALTER TABLE
and query:
CREATE TABLE RXNCONSO
(
RXCUI varchar(8) NOT NULL,
LAT varchar (3) DEFAULT 'ENG' NOT NULL,
TS varchar (1),
LUI varchar(8),
STT varchar (3),
SUI varchar (8),
ISPREF varchar (1),
RXAUI varchar(8) NOT NULL,
SAUI varchar (50),
SCUI varchar (50),
SDUI varchar (50),
SAB varchar (20) NOT NULL,
TTY varchar (20) NOT NULL,
CODE varchar (50) NOT NULL,
STR varchar (3000) NOT NULL,
SRL varchar (10),
SUPPRESS varchar (1),
CVF varchar(50)
)
;
ALTER TABLE `RXNCONSO` ADD INDEX(`TTY`);
EXPLAIN SELECT RXCUI, STR FROM RXNCONSO
WHERE TTY IN ('SCD','SBD','GPCK','BPCK','SCDG','SBDG','SCDF','SBDF');
+----+-------------+----------+------+---------------+------+---------+------+---------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+---------------+------+---------+------+---------+-------------+
| 1 | SIMPLE | RXNCONSO | ALL | NULL | NULL | NULL | NULL | 1139206 | Using where |
+----+-------------+----------+------+---------------+------+---------+------+---------+-------------+
1 row in set (0.00 sec)
Update 2:
Here's the result of running
SELECT TTY, count(1) FROM RXNCONSO
WHERE TTY IN ('SCD','SBD','GPCK','BPCK','SCDG','SBDG','SCDF','SBDF')
GROUP BY TTY WITH ROLLUP;
+------+----------+
| TTY | count(1) |
+------+----------+
| BPCK | 526 |
| GPCK | 492 |
| SBD | 21732 |
| SBDF | 14593 |
| SBDG | 21140 |
| SCD | 36131 |
| SCDF | 14556 |
| SCDG | 16362 |
| NULL | 125532 |
+------+----------+
9 rows in set (0.38 sec)
show create table MyTable;
and theEXPLAIN
results. There are multiple possible reasons - one would be mixing unquoted numbers with quoted string (even numeric ones) in theIN
list.IN
list is usually solved asrange
access, unless theIN
list covers really big part of the table.SELECT TTY, count(1) FROM RXNCONSO WHERE TTY IN ('SCD','SBD','GPCK','BPCK','SCDG','SBDG','SCDF','SBDF') GROUP BY TTY WITH ROLLUP;
to see how many rows the query finds, if the total or the "Val1" is going to be too big, the index can not help much (unless you extend it to be covering).