tags:

views:

61

answers:

3

Is there any optimizations I can do with this statement to get the count of used rows.

Note: The ProductOrderInfo table has over 40 million rows in it.

SELECT @TotalUsed = COUNT(*)
FROM ProductInfo WITH (NOLOCK)
WHERE ProductInfoId IN 
(
    SELECT ProductInfoId FROM ProductOrderInfo WITH (NOLOCK)
);
+1  A: 

Try this

SELECT @TotalUsed = COUNT(*) FROM ProductInfo WITH (NOLOCK) as p 
WHERE EXISTS ( SELECT ProductInfoId FROM ProductOrderInfo WITH (NOLOCK) 
WHERE ProductInfoId =p.ProductInfoId );
Madhivanan
Is the Exists faster then IN?
RPS
In most cases, YES
Madhivanan
+1  A: 

Assuming there are no orphan ProductInfoIds on ProductOrderInfo, the following:

SELECT @TotalUsed = COUNT(DISTINCT ProductInfoId) 
FROM ProductOrderInfo WITH (NOLOCK);

may be faster, as it only accesses one table.

Whichever query is being run, it should run faster if there is an index on ProductInfoID on ProductOrderInfo.

Mark Bannister
+2  A: 

Use JOIN for such queries:

SELECT @TotalUsed = COUNT(DISTINCT i.ProductInfoId) 
FROM ProductInfo i WITH (NOLOCK) 
JOIN ProductOrderInfo oi WITH (NOLOCK) 
    ON io.ProductInfoId = i.ProductInfoId

Tables should have Indexes by these columns for fast search.

Pavel Belousov
I think this query should return the number of ProductOrderInfo records with corresponding ProductInfo records (ie. the total number of order lines, barring orphan products - circa 40 million records) rather than the number of products which have had orders placed against them. Changing count(*) to count(distinct i.ProductInfoId) should return the number of products which have had orders placed against them.
Mark Bannister
Thanks a lot, it's my mistake.
Pavel Belousov