I am running two very similar queries on two very similar tables, but one query is running about three times as slow as the other query. The funny thing is, the slow query is on the smaller table. All the indexes are new and up to date. myisamchk has been run to optimize the tables and indexes.
Can someone please explain why one query would be slower than another, nearly identical query.
Here are the specifics:
table one:
Code:
CREATE TABLE one (
cui1 VARCHAR(8) NOT NULL DEFAULT '',
cui2 VARCHAR(8) DEFAULT NULL,
soc VARCHAR(7) NOT NULL DEFAULT '',
cot CHAR(3) DEFAULT NULL,
cof INT(5) DEFAULT NULL,
coa TINYTEXT,
cui2name VARCHAR(255) DEFAULT NULL,
KEY one_index (cui1,cui2,cof,cot)
) TYPE=MyISAM;
query one:
Code:
SELECT cui2, cui2name, cof
FROM one
WHERE cui1 = 'C0002170' AND cui2 IS NOT NULL
AND cof > 2 AND cot <> 'LQ' AND cot <> 'LQB'
ORDER BY cui2;
explain select for this query on table one:
Code:
+------+------+--------------+-----------+---------+--------+--------+-------------+
|table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+------+--------------+-----------+---------+--------+--------+-------------+
|one | range| one_index | one_index | 17 | [NULL] | 1023 | Using where |
+------+------+--------------+-----------+---------+--------+--------+-------------+
Now here is the info for the second table and the second query.
Code:
CREATE TABLE two (
cui1 VARCHAR(8) NOT NULL DEFAULT '',
cui2 VARCHAR(8) NOT NULL DEFAULT'',
cof INT(8) DEFAULT NULL,
new_cof INT(8) default NULL,
cui1name VARCHAR(255) DEFAULT NULL,
cui2name VARCHAR(255) DEFAULT NULL,
PRIMARY KEY (cui1,cui2),
KEY two_index (cui1,cof)
) TYPE=MyISAM;
Here is the query on table two:
Code:
SELECT cui2, cui2name, cof
FROM two
WHERE cui1 = 'C0002170' AND cof > 2
ORDER BY cof DESC;
And here is the explain select for the query on table two:
Code:
+-------+-------+--------------------+-----------+---------+--------+------+-------------+
| table | type | possible_keys | key | key_len | ref | rows | Extra |
+-------+-------+--------------------+-----------+---------+--------+------+-------------+
| two | range | PRIMARY,two_index | two_index | 13 | [NULL] | 241 | Using where |
+-------+-------+--------------------+-----------+---------+--------+------+-------------+
Table one has 13.9 million rows and the query returns in 0.13 seconds. Table two has 1.3 million rows and the query takes three times as long (0.39 seconds). I really need to get the query on the second table down to 0.1 seconds. Why in the world is it taking so long if the similar query on the similar (but bigger) table is taking much less time?
Thanks.