-
3M records query timeout
I have an SQl Server 7.0 table with 3 million records this table is indexed on the ff. fields Terminal_Nb and Draw_Nb.
I need to run this query but always get timed out. Can you please help. How best to optimize the query. Thanks
Select distinct Terminal_No, Draw_nb,
(Select sum(Gross_Sales) from tickets where terminal_no = qrysales.Terminal_No and Draw_No = qrySales.Draw_Nb as Gross),
(Select sum(Gross_Sales) from tickets where terminal_no = qrysales.Terminal_No and Draw_No = qrySales.Draw_Nb and cancel_Date is not null as Cancels),
(Select sum(VAT) from tickets where terminal_no = qrysales.Terminal_No and Draw_No = qrySales.Draw_Nb as VATreturns),
(Select sum(Gross_Sales - VAT) from tickets where terminal_no = qrysales.Terminal_No and Draw_No = qrySales.Draw_Nb as ActualStake),
(Select sum(To_Pay) from tickets where terminal_no = qrysales.Terminal_No and Draw_No = qrySales.Draw_Nb and Payment_Date is not null as Payment)
from tickets qrySales
-
Can't you make some views on the server.
Then later do a select on them.
-
Paste the query into query analyzer then use the Index Tuning Wizard on the query menu.
This will tell you if there are nay indexes needed.