我有兩個表:爲什麼在使用JOIN而不是WHERE時MySQL很慢?
CREATE TABLE `test_sample` (
`idtest_sample` varchar(50) NOT NULL,
`test_samplecol` varchar(45) DEFAULT NULL,
UNIQUE KEY `idtest_sample_UNIQUE` (`idtest_sample`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
和
CREATE TABLE `new_table` (
`idnew_table` int(11) NOT NULL,
UNIQUE KEY `idnew_table_UNIQUE` (`idnew_table`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
第一個表中包含500萬條記錄,而第二個只有10條記錄。
這個查詢的執行的持續時間是5秒以上:
SELECT * FROM test_sample
INNER JOIN new_table
ON test_sample.idtest_sample = new_table.idnew_table
在執行該查詢立即(小於0.001秒):
SELECT * FROM test_sample
WHERE test_sample.idtest_sample IN
('3','1597','25963','170596','196485',
'545963','999999','1265896','1569485','1999999')
爲什麼第一查詢需要太長?
如果您嘗試'SELECT test_sample。* FROM ...'您的第一個查詢,該怎麼辦? – fthiella
對這兩個查詢運行[EXPLAIN](http://dev.mysql.com/doc/refman/5.0/en/explain.html)。 –
即使第二張表具有良好的索引? –