performance - Why MySQL is very slow when using JOIN instead of WHERE? -
i have 2 tables:
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
and
create table `new_table` ( `idnew_table` int(11) not null, unique key `idnew_table_unique` (`idnew_table`) ) engine=myisam default charset=utf8
the first table contains 5 million records while second 1 10 records.
the duration of execution of query more 5 seconds:
select * test_sample inner join new_table on test_sample.idtest_sample = new_table.idnew_table
while query executed (less 0.001 second):
select * test_sample test_sample.idtest_sample in ('3','1597','25963','170596','196485', '545963','999999','1265896','1569485','1999999')
why first query takes long?
did try see execution path ? desc {sql}, first 1 longer
the first query entire 5 millions rows each row of other table. while second query specific id have index (as they're part of primary key)
edit: here explain (simplified):
+----+-------------+-------------+--------+----------------------+------+ | id | select_type | table | type | possible_keys | key | +----+-------------+-------------+--------+----------------------+------+ | 1 | simple | new_table | system | idnew_table_unique | null | | 1 | simple | test_sample | | idtest_sample_unique | null | +----+-------------+-------------+--------+----------------------+------+ +----+-------------+-------------+-------+----------------------+----------------------+ | id | select_type | table | type | possible_keys | key | +----+-------------+-------------+-------+----------------------+----------------------+ | 1 | simple | test_sample | const | idtest_sample_unique | idtest_sample_unique | +----+-------------+-------------+-------+----------------------+----------------------+
as can see there 'all' scan on test_sample table (5 millions line)
you may want take here : http://hackmysql.com/case4
Comments
Post a Comment