Hey everyone,
I am attempting to optimize the sql statements I am currently using.
I have a series of about 5 complex statements. 3 out of 5 of these run very quickly. (I used a timer function to find the time to process.)
However the other 2 run very slowly (6-8 seconds, where as the others take only a few milliseconds). I am analyzing the statements and have found a probably root cause and am now working on optimizing them, but I need your help.
The primary reason I found (from testing) is the "OR" part of each query. When I remove the portion after the "OR" the speed goes to the normal speed of about 39 milliseconds.
My concern is getting these to run more quickly.
The reason for the OR statement is to show historical data for orders that have been filled, but were not filled in the past. Any dates in the SQL statement are generated dynamically, so they do not always show today's date.
Any suggestions?
[code=sql]
SELECT DISTINCT tblRequest.Cust omerTracking, tblLocations.Lo cationCode, tblRequest.ReqD ate, tblRequest.ReqT ime, [Prefix] & tblModel_1.Mode lName AS PendingModel, tblRequest.ETA, tblRequest.Pend ingCauses, tblRequestDetai l.Quantity*[stdQuantity] AS ReqQty, qry_Qtys.Outsta nding AS QtyOutstanding, tblRequestType. RequestType, tblInvoice.Invo iceDate, tblInvoice.Trac kingNumber
FROM tblInvoice RIGHT JOIN (((qry_Qtys INNER JOIN (tblRequestType RIGHT JOIN (tblModel AS tblModel_1 INNER JOIN ((((tblCustomer INNER JOIN tblRequest ON tblCustomer.Cus tomerID = tblRequest.Cust omerID) INNER JOIN tblRequestDetai l ON tblRequest.Requ estID = tblRequestDetai l.RequestID) LEFT JOIN tblLocations ON tblRequest.Loca tionID = tblLocations.Lo cationID) INNER JOIN ((tblModel INNER JOIN tblProduct ON tblModel.ModelI D = tblProduct.Mode lID) INNER JOIN tblModelDetail ON tblModel.ModelI D = tblModelDetail. ModelID) ON tblRequestDetai l.ProductID = tblProduct.Prod uctID) ON tblModel_1.Mode lID = tblModelDetail. IncludesModelID ) ON tblRequestType. RequestTypeID = tblRequest.Requ estTypeID) ON qry_Qtys.Reques tID = tblRequest.Requ estID) INNER JOIN tblProduct AS tblProduct_1 ON (tblModel_1.Mod elID = tblProduct_1.Mo delID) AND (qry_Qtys.Produ ctID = tblProduct_1.Pr oductID)) LEFT JOIN tblInvoiceDetai l ON tblRequest.Requ estID = tblInvoiceDetai l.RequestID) ON tblInvoice.Invo iceID = tblInvoiceDetai l.InvoiceID
WHERE (((tblRequest.R eqDate)<#2/4/2008#) AND (([tblRequestDetai l].[Quantity]*[stdQuantity])>0) AND ((qry_Qtys.Outs tanding)>0) AND ((tblRequestTyp e.RequestTypeID )<>3) AND ((tblRequestDet ail.Cleared)=0) AND ((tblRequest.Cu stomerID)=1) AND ((tblRequest.Ca nceled)<>-1))
OR
(((tblRequest.R eqDate)<#2/4/2008#) AND (([tblRequestDetai l].[Quantity]*[stdQuantity])>0) AND ((qry_Qtys.Outs tanding)>=0) AND ((tblInvoice.In voiceDate)>#2/4/2008#) AND ((tblRequestTyp e.RequestTypeID )<>3) AND ((tblRequestDet ail.Cleared)=0) AND ((tblRequest.Cu stomerID)=1) AND ((tblRequest.Ca nceled)<>-1))
ORDER BY [Prefix] & tblModel_1.Mode lName, tblRequest.Cust omerTracking;
[/code]
I suppose that I could choose to only include this OR statement if the user input date is not today's date. This would be a partial fix, but I would still like to increase the speed otherwise.
[code=sql]
SELECT DISTINCT tblRequest.Cust omerTracking, tblLocations.Lo cationCode, tblRequest.ReqD ate, tblRequest.ReqT ime, [Prefix] & tblModel_1.Mode lName AS PendingModel, tblRequest.ETA, tblRequest.Pend ingCauses, tblRequestDetai l.Quantity*[stdQuantity] AS ReqQty, qry_Qtys.Outsta nding AS QtyOutstanding, tblRequestType. RequestType, tblInvoice.Invo iceDate, tblInvoice.Trac kingNumber
FROM tblInvoice RIGHT JOIN (((qry_Qtys INNER JOIN (tblRequestType RIGHT JOIN (tblModel AS tblModel_1 INNER JOIN ((((tblCustomer INNER JOIN tblRequest ON tblCustomer.Cus tomerID = tblRequest.Cust omerID) INNER JOIN tblRequestDetai l ON tblRequest.Requ estID = tblRequestDetai l.RequestID) LEFT JOIN tblLocations ON tblRequest.Loca tionID = tblLocations.Lo cationID) INNER JOIN ((tblModel INNER JOIN tblProduct ON tblModel.ModelI D = tblProduct.Mode lID) INNER JOIN tblModelDetail ON tblModel.ModelI D = tblModelDetail. ModelID) ON tblRequestDetai l.ProductID = tblProduct.Prod uctID) ON tblModel_1.Mode lID = tblModelDetail. IncludesModelID ) ON tblRequestType. RequestTypeID = tblRequest.Requ estTypeID) ON qry_Qtys.Reques tID = tblRequest.Requ estID) INNER JOIN tblProduct AS tblProduct_1 ON (tblModel_1.Mod elID = tblProduct_1.Mo delID) AND (qry_Qtys.Produ ctID = tblProduct_1.Pr oductID)) LEFT JOIN tblInvoiceDetai l ON tblRequest.Requ estID = tblInvoiceDetai l.RequestID) ON tblInvoice.Invo iceID = tblInvoiceDetai l.InvoiceID
WHERE (((tblRequest.R eqDate)<#2/4/2008#) AND (([tblRequestDetai l].[Quantity]*[stdQuantity])>0) AND ((qry_Qtys.Outs tanding)>0) AND ((tblRequestTyp e.RequestTypeID )<>3) AND ((tblRequestDet ail.Cleared)=0) AND ((tblRequest.Cu stomerID)=1) AND ((tblRequest.Ca nceled)<>-1))
IF inputDate < Date() THEN --FOR EXAMPLE
OR
(((tblRequest.R eqDate)<#2/4/2008#) AND (([tblRequestDetai l].[Quantity]*[stdQuantity])>0) AND ((qry_Qtys.Outs tanding)>=0) AND ((tblInvoice.In voiceDate)>#2/4/2008#) AND ((tblRequestTyp e.RequestTypeID )<>3) AND ((tblRequestDet ail.Cleared)=0) AND ((tblRequest.Cu stomerID)=1) AND ((tblRequest.Ca nceled)<>-1))
END IF
ORDER BY [Prefix] & tblModel_1.Mode lName, tblRequest.Cust omerTracking;
[/code]
I am attempting to optimize the sql statements I am currently using.
I have a series of about 5 complex statements. 3 out of 5 of these run very quickly. (I used a timer function to find the time to process.)
However the other 2 run very slowly (6-8 seconds, where as the others take only a few milliseconds). I am analyzing the statements and have found a probably root cause and am now working on optimizing them, but I need your help.
The primary reason I found (from testing) is the "OR" part of each query. When I remove the portion after the "OR" the speed goes to the normal speed of about 39 milliseconds.
My concern is getting these to run more quickly.
The reason for the OR statement is to show historical data for orders that have been filled, but were not filled in the past. Any dates in the SQL statement are generated dynamically, so they do not always show today's date.
Any suggestions?
[code=sql]
SELECT DISTINCT tblRequest.Cust omerTracking, tblLocations.Lo cationCode, tblRequest.ReqD ate, tblRequest.ReqT ime, [Prefix] & tblModel_1.Mode lName AS PendingModel, tblRequest.ETA, tblRequest.Pend ingCauses, tblRequestDetai l.Quantity*[stdQuantity] AS ReqQty, qry_Qtys.Outsta nding AS QtyOutstanding, tblRequestType. RequestType, tblInvoice.Invo iceDate, tblInvoice.Trac kingNumber
FROM tblInvoice RIGHT JOIN (((qry_Qtys INNER JOIN (tblRequestType RIGHT JOIN (tblModel AS tblModel_1 INNER JOIN ((((tblCustomer INNER JOIN tblRequest ON tblCustomer.Cus tomerID = tblRequest.Cust omerID) INNER JOIN tblRequestDetai l ON tblRequest.Requ estID = tblRequestDetai l.RequestID) LEFT JOIN tblLocations ON tblRequest.Loca tionID = tblLocations.Lo cationID) INNER JOIN ((tblModel INNER JOIN tblProduct ON tblModel.ModelI D = tblProduct.Mode lID) INNER JOIN tblModelDetail ON tblModel.ModelI D = tblModelDetail. ModelID) ON tblRequestDetai l.ProductID = tblProduct.Prod uctID) ON tblModel_1.Mode lID = tblModelDetail. IncludesModelID ) ON tblRequestType. RequestTypeID = tblRequest.Requ estTypeID) ON qry_Qtys.Reques tID = tblRequest.Requ estID) INNER JOIN tblProduct AS tblProduct_1 ON (tblModel_1.Mod elID = tblProduct_1.Mo delID) AND (qry_Qtys.Produ ctID = tblProduct_1.Pr oductID)) LEFT JOIN tblInvoiceDetai l ON tblRequest.Requ estID = tblInvoiceDetai l.RequestID) ON tblInvoice.Invo iceID = tblInvoiceDetai l.InvoiceID
WHERE (((tblRequest.R eqDate)<#2/4/2008#) AND (([tblRequestDetai l].[Quantity]*[stdQuantity])>0) AND ((qry_Qtys.Outs tanding)>0) AND ((tblRequestTyp e.RequestTypeID )<>3) AND ((tblRequestDet ail.Cleared)=0) AND ((tblRequest.Cu stomerID)=1) AND ((tblRequest.Ca nceled)<>-1))
OR
(((tblRequest.R eqDate)<#2/4/2008#) AND (([tblRequestDetai l].[Quantity]*[stdQuantity])>0) AND ((qry_Qtys.Outs tanding)>=0) AND ((tblInvoice.In voiceDate)>#2/4/2008#) AND ((tblRequestTyp e.RequestTypeID )<>3) AND ((tblRequestDet ail.Cleared)=0) AND ((tblRequest.Cu stomerID)=1) AND ((tblRequest.Ca nceled)<>-1))
ORDER BY [Prefix] & tblModel_1.Mode lName, tblRequest.Cust omerTracking;
[/code]
I suppose that I could choose to only include this OR statement if the user input date is not today's date. This would be a partial fix, but I would still like to increase the speed otherwise.
[code=sql]
SELECT DISTINCT tblRequest.Cust omerTracking, tblLocations.Lo cationCode, tblRequest.ReqD ate, tblRequest.ReqT ime, [Prefix] & tblModel_1.Mode lName AS PendingModel, tblRequest.ETA, tblRequest.Pend ingCauses, tblRequestDetai l.Quantity*[stdQuantity] AS ReqQty, qry_Qtys.Outsta nding AS QtyOutstanding, tblRequestType. RequestType, tblInvoice.Invo iceDate, tblInvoice.Trac kingNumber
FROM tblInvoice RIGHT JOIN (((qry_Qtys INNER JOIN (tblRequestType RIGHT JOIN (tblModel AS tblModel_1 INNER JOIN ((((tblCustomer INNER JOIN tblRequest ON tblCustomer.Cus tomerID = tblRequest.Cust omerID) INNER JOIN tblRequestDetai l ON tblRequest.Requ estID = tblRequestDetai l.RequestID) LEFT JOIN tblLocations ON tblRequest.Loca tionID = tblLocations.Lo cationID) INNER JOIN ((tblModel INNER JOIN tblProduct ON tblModel.ModelI D = tblProduct.Mode lID) INNER JOIN tblModelDetail ON tblModel.ModelI D = tblModelDetail. ModelID) ON tblRequestDetai l.ProductID = tblProduct.Prod uctID) ON tblModel_1.Mode lID = tblModelDetail. IncludesModelID ) ON tblRequestType. RequestTypeID = tblRequest.Requ estTypeID) ON qry_Qtys.Reques tID = tblRequest.Requ estID) INNER JOIN tblProduct AS tblProduct_1 ON (tblModel_1.Mod elID = tblProduct_1.Mo delID) AND (qry_Qtys.Produ ctID = tblProduct_1.Pr oductID)) LEFT JOIN tblInvoiceDetai l ON tblRequest.Requ estID = tblInvoiceDetai l.RequestID) ON tblInvoice.Invo iceID = tblInvoiceDetai l.InvoiceID
WHERE (((tblRequest.R eqDate)<#2/4/2008#) AND (([tblRequestDetai l].[Quantity]*[stdQuantity])>0) AND ((qry_Qtys.Outs tanding)>0) AND ((tblRequestTyp e.RequestTypeID )<>3) AND ((tblRequestDet ail.Cleared)=0) AND ((tblRequest.Cu stomerID)=1) AND ((tblRequest.Ca nceled)<>-1))
IF inputDate < Date() THEN --FOR EXAMPLE
OR
(((tblRequest.R eqDate)<#2/4/2008#) AND (([tblRequestDetai l].[Quantity]*[stdQuantity])>0) AND ((qry_Qtys.Outs tanding)>=0) AND ((tblInvoice.In voiceDate)>#2/4/2008#) AND ((tblRequestTyp e.RequestTypeID )<>3) AND ((tblRequestDet ail.Cleared)=0) AND ((tblRequest.Cu stomerID)=1) AND ((tblRequest.Ca nceled)<>-1))
END IF
ORDER BY [Prefix] & tblModel_1.Mode lName, tblRequest.Cust omerTracking;
[/code]
Comment