I recently just noticed that WHERE EXISTS
can slow down the operation badly. E.g.
select SQL_NO_CACHE count(*) as aggregate
from `table_1`
where exists (select *
from `table_2`
where `table_1`.`fk_id` = `table_2`.`id`
and `name` = 'john');
79 seconds
VS
select SQL_NO_CACHE count(*) as aggregate
from `table_1`
inner join `table_2` on `table_1`.`fk_id` = `table_2`.`id`
where `table_2`.`name` = 'John';
72 milliseconds
table_1
has total 4,723,710 recordstable_2
has total 1,236 records
Just wanted to know, since the performance of WHERE EXISTS
so bad, under what scenario do we need to use this?
table_1
, where thetable_2
name must be John