I have a query that takes about 30+ seconds to run in query analyzer. When I run it from .NET code the query times out. Even if I could get it to work from .NET, 30 seconds is way too long. I was hoping someone could help improve my query without losing the functionality of it.
Now if I take out the part that is hurting the performance and run it alone, it runs fast. It's just that combining it with the rest of it is killing my query. It does return the correct results, it's just too slow to use. Any ideas?
Code:
[b]/*Begin Query*/ [/b]
Select Reqid, UserID, Attention, PurchasingStatus, RequestorID, DivID, Division.DivisionID, Requisition.BranchID as reqBranchID, Branch.BranchID, SecID, Section.SectionID, Status, FirstName, LastName, EmergencyOrder, Division.Description as divDesc, Branch.Description as branDesc, Section.Description as secDesc, awVendor
FROM Requisition inner join Division on Requisition.DivID = Division.DivisionID
inner join Users on Requisition.RequestorID = Users.UserID
left outer join Branch on Requisition.BranchID = Branch.BranchID
left outer join Section on Section.SectionID = Requisition.SecID
WHERE Status > 499
and Status < 900
and (PurchasingStatus = 'Purchased - Procard' or PurchasingStatus = 'Paid - Unencumbered')
/* The following is killing my query performance */
and Reqid in
(Select Items.ReqID from Items where Items.ReqID = Requisition.Reqid
and (Items.Quantity > (Select Sum(ReceiveHistory.Quantity)
from ReceiveHistory where ReceiveHistory.ItemID = Items.ItemID)
or (Select Sum(ReceiveHistory.Quantity) from ReceiveHistory where ReceiveHistory.ItemID = Items.ItemID) is null))
/* The preceding is killing my query performance */
and LastName like '%Dall%'
[b]/*End Query*/ [/b]
Now if I take out the part that is hurting the performance and run it alone, it runs fast. It's just that combining it with the rest of it is killing my query. It does return the correct results, it's just too slow to use. Any ideas?