MySQL - left join or nested select for filtering non-existing rows? -
table structure catalog_product_category_bindings:
`productid` integer unsigned not null `categoryid` integer unsigned not null table structure catalog_products:
`id` integer unsigned not null auto_increment misc unrelated columns task: data of products not bound category (no entry in catalog_product_category_bindings).
query #1 (using left join):
select cp.* catalog_products cp left join catalog_product_category_bindings cpcb on cp.id = cpcb.productid cpcb.categoryid null query #2 (using nested select):
select cp.* catalog_products cp id not in (select productid catalog_product_category_bindings) both queries seem quite similar in terms of speed on tables (i don't have in there), believe second 1 worse in performance since loops on every id in catalog_products table , compares every productid catalog_product_category_bindings. not mention might not return , break query altogether (though happen if table truncated).
which better? i, personally, prefer #1 since fits better query builder , seems better.
since columns defined not null mysql optimizes not in() query anyway not exists. there's not difference between two.
here's must read topic.
Comments
Post a Comment