samedi 28 février 2015

Querying a View with IN vs UNION performance

I have a view that the purpose is to create a regular table instead of a attribute-based representation. In doing so, it casts the varchar field into other varchars and some ints.


Today I'm running into an issue and not sure how to troubleshoot it.


If I try:



select * from vNormalTable where ItemId in (...query to get the affected item ids...)


it never completes (at least, not after 5 minutes). I've run the subquery, and it loads 9 numbers in less than a second.


If I try:



select * from vNormalTable where ItemId in (1, 2, 3, 4, 5, 6, 7, 8, 9)


It's the same thing. Nothing loads.


If I try:



select * from vNormalTable where ItemId = 1
union
select * from vNormalTable where ItemId = 2
union
....


It returns all 9 rows in less than a second.


The view itself is a bunch of left joins (well, just 10) with the item_attributes table (ie dbo.item_attributes as color where item_id = i.item_id = color.item_id and attribute_id = 10001), I don't see any special things happening other than the previously mentioned casting.


I don't know the inner workings of IN () (I always assumed it was the equivalent of = each item and union results, but that doesn't seem to be the case here). Is there anything to look for that causes it to fail only in the IN case? It was working fine before today but may have to do with a combination of bad data (doesn't seem to be the case here) and/or more records, don't know.


Aucun commentaire:

Enregistrer un commentaire