sql server - SQL Efficiency: WHERE IN Subquery vs. JOIN then GROUP -
As an example, I want to get a list of all the items that have some tags applied to them. I can do any of the following:
Select item. Id, items. Item items from item where item items in item (id) select item items from item TIGID = 57 or item tag. TIGID = 55)
or
select item. Id, item. Skip Item ItemTag.ItemID = Item.ID on itemTag WHERE ItemTag.TagID = 57 or item tag. TIGID = 55 Group item item. Id, item. Name
Or something completely different
In general (assuming that there is a general rule), what is a more efficient approach?
select Item.ID, ItemName Item with WHERE Item ID IN (item tags from item tag tag = 57 or item tag. TIGID = 55)
or
SELECT Item.ID, Item Item Item Description Item item.ItemID = Item.ID, item tag, item tag, item item, tag.agid = 57, or ItemTag.TagID = 55 group item .id
The second D query will not compile, because it gives reference to Item.Name
without any group or group.
If we remove GROUP BY
from the query:
select items. Id, item. Items from item to item tag. ITAMID = item. Item tags on id where item tag. TIGID = 57 or item tag TIGID = 55
These are still different questions, unless If You can traceform another query in But this is a very less efficient For a more detailed performance comparison of this article in your blog, see: SQL Server
ItemTag.ItemId < / Code> is able to locate a
IN
position on a UNIQUE
/ code> a IN
column, and it is IN < / Code> will change the status to
JOIN
. ItemTag.ItemID
is not UNIQUE
, the first query will use a SEMI JOIN
algorithm, which is the SQL Server is quite skilled in
. JOIN
:
select item. Id, items. Add items to item (select DISTI Nct item id item item tag where item tag tagID = 57 or item tag .tigid = 55) tags on tags. ITEM = ITEM.ID
IN
or EXISTS
.
Comments
Post a Comment