3 ms·
I should add for people who are not aware, the same concept is very important to bear in mind if there is a WHERE EXISTS or WHERE NOT EXISTS subclause to your q
by seanhunter 3y ago
I should add for people who are not aware, the same concept is very important to bear in mind if there is a WHERE EXISTS or WHERE NOT EXISTS subclause to your query. Something like
select
u.username as good_user
from
users u
where
not exists (
select 1 from naughty_users n where n.id = u.id
)
…is generally going to be much much faster than what a lot of people instinctively do which is to select the username or user id from the inner query. Since you’re just checking for (non)existence it doesn’t matter what the inner query returns so select 1 will answer the query from the index (assuming the id is indexed which if it isn’t you have bigger problems).
If you spend a while looking at explain plans for your queries you will spot common patterns like this where you can avoid a table scan often.