mysql - Is querying with NOT IN faster then querying with IN? -
let's presume following simple situation:
i have 2 tables, category table contains 2 fields, categoryid
, , categorygroup
, , ads table contains 2 fields, adid
, , category_categoryid
link category table.
all rows in category table grouped in 2 separate groups: buy or rent. so, each row in table has in categorygroup
either string buy or string rent.
let's want count how many ads have in ads sale.
i have 2 ways this:
do
not in
query this:select count(adid) total ads category_categoryid not in (select categoryid category categorygroup = 'rent')
or 'in' query this:
select count(adid) total ads category_categoryid in (select categoryid category categorygroup = 'buy')
i've tested both queries, , seems me, not in
query performs way faster in
type of query.
(0.45 secs not in
on table of ~900.000 rows, , around 45 categories, while 1.1 secs in
on same dataset)
is incidental, or not in
queries perform faster in simmilar situations?
in ( select ... )
, not in ( select ... )
perhaps never efficient way code something. 1 may faster other because select
has fewer rows other, not because of not
.
assuming ad in 1 category, efficient.
select count(ads.adid) total, ads.categoryid ads join category c on c.categoryid = ads.categoryid c.categorygroup = 'buy' group ads.categoryid
if ad can in multiple categories, have puzzle: should ad both 'buy' , 'rent' included or excluded count? anyway, leading replacing in
exists
alternative optimization:
select count(adid) total, categoryid ads exists ( select * category categoryid = ads.categoryid , categorygroup = 'buy' ) group categoryid
(sorry, can't stand unnecessarily redundant column names category_categoryid
.)
perform explain select ...
on various choices more insight.
Comments
Post a Comment