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)
);
Use JOIN for such queries:
Tables should have Indexes by these columns for fast search.