SQL Query is taking long time to run
Hi All,SQL Query is taking long time to run in my report. It is using Clustered index seek when I am querying for 2 days on invoice_itemized table. It is returning 99 thousand records. It is returning 2524094 records and it is using clustered index scan when I am querying for one month. For one month It is taking 7 minutes.Basically it is not using the index defined for the store_Id, status and datetime columns in the invoice_totals table. Can I force the sql query to use the specified index?
Please suggest me to improve the performance....
SQL Query=========
SELECTInvoice_Totals.Store_Id,Invoice_Totals.DateTime InvoiceDate,Invoice_Totals.Invoice_Number,Invoice_Totals.CustNum,Invoice_Totals.Cashier_Id,Invoice_Itemized.ItemNum,Invoice_Itemized.DiffItemName,Invoice_Itemized.Quantity,Invoice_Itemized.origPricePer,Invoice_Itemized.PricePer,Invoice_Totals.Discount InvDiscPercent,Invoice_Itemized.LineDisc LineDiscPercent,CASE WHEN Invoice_Totals.Total_Price <> 0 AND Inventory.ItemType=7 AND Invoice_Totals.Discount = 0 THEN ((Invoice_Itemized.PricePer * Invoice_Itemized.Quantity) * -1) / ((Invoice_Totals.Total_Price) + ((Invoice_Itemized.PricePer * Invoice_Itemized.Quantity) * -1))WHEN Invoice_Totals.Total_Price <> 0 AND Inventory.ItemType=7 THEN ((Invoice_Itemized.PricePer * Invoice_Itemized.Quantity) * -1) / ((Invoice_Totals.Total_Price / Invoice_Totals.Discount) + ((Invoice_Itemized.PricePer * Invoice_Itemized.Quantity) * -1)) ELSE 0 END CoupDiscPercent
,CASE WHEN Invoice_Totals.Discount > 0 AND Invoice_Itemized.origPricePer <> 0 THEN ((pricePer*Discount) * Quantity) ELSE 0 END InvDiscAmt,CASE WHEN origPriceper = 0 THEN 0 WHEN LineDisc = 0 THEN ((OrigpricePer-PricePer)*Quantity) WHEN LineDisc = 1 THEN 0 ELSE (OrigPricePer - ROUND(((PricePer/(1-LineDisc))),2))*Quantity END LineDiscAmt,CASEWHEN origPriceper=0 THEN 0 WHEN LineDisc = 0 THEN 0 WHEN LineDisc =1 THEN OrigPricePer ELSE ((PricePer/(1-lineDisc))-Priceper)*Quantity END ManLDDiscAmt,CASE WHEN origPriceper = 0 THEN (OrigPricePer-PricePer)* Quantity ELSE 0 END CoupnDiscAmt,Invoice_Itemized_ItemNotes.NotesFROM Invoice_TotalsINNER JOIN Invoice_Itemized ON Invoice_Totals.Invoice_Number = Invoice_Itemized.Invoice_Number AND Invoice_Totals.Store_Id = Invoice_Itemized.Store_IdINNER JOIN Inventory ON Invoice_Itemized.ItemNum = Inventory.ItemNum AND Invoice_Itemized.Store_Id = Inventory.Store_IdLEFT JOIN Invoice_Itemized_ItemNotes ON Invoice_Itemized.Invoice_Number = Invoice_Itemized_ItemNotes.Invoice_Number AND Invoice_Itemized.Store_Id = Invoice_Itemized_ItemNotes.Store_Id AND Invoice_Itemized.LineNum = Invoice_Itemized_ItemNotes.LineNumWHEREInvoice_Totals.Store_Id in (N'000',N'002',N'003',N'004',N'005',N'006',N'008',N'009',N'011',N'013',N'015',N'018',N'021',N'022',N'024',N'025',N'026',N'028',N'030',N'031',N'032',N'034',N'040',N'041',N'043',N'048',N'052',N'054',N'055',N'056',N'057',N'058',N'060',N'062',N'065',N'067',N'068',N'070',N'076',N'077',N'084',N'085',N'087',N'089',N'091',N'092',N'093',N'094',N'096',N'098',N'099',N'1001',N'1003',N'1004',N'101',N'103',N'105',N'108',N'109',N'115',N'117',N'122',N'123',N'124',N'125',N'126',N'129',N'130',N'132',N'133',N'140',N'143',N'144',N'146',N'149',N'152',N'154',N'157',N'159',N'162',N'163',N'165',N'167',N'168',N'170',N'171',N'173',N'176',N'178',N'179',N'180',N'184',N'190',N'200',N'201',N'202',N'204',N'206',N'207',N'210',N'211',N'213',N'214',N'216',N'217',N'218',N'222',N'223',N'231',N'233',N'236',N'241',N'243',N'260',N'261',N'263',N'273',N'276',N'280',N'281',N'286',N'295',N'296',N'297',N'298',N'299',N'308',N'311',N'322',N'324',N'330',N'335',N'337',N'339',N'341',N'343',N'345',N'347',N'351',N'353',N'355',N'357',N'358',N'366',N'371',N'372',N'374',N'375',N'376',N'377',N'379',N'381',N'389',N'390',N'391',N'392',N'394',N'396',N'397',N'400',N'402',N'406',N'408',N'409',N'410',N'412',N'413',N'414',N'415',N'416',N'419',N'445',N'446',N'447',N'448',N'453',N'459',N'460',N'461',N'465',N'466',N'467',N'470',N'473',N'474',N'475',N'478',N'479',N'481',N'483',N'484',N'485',N'487',N'488',N'489',N'490',N'491',N'495',N'501',N'502',N'503',N'505',N'506',N'509',N'511',N'512',N'514',N'516',N'518',N'519',N'521',N'522',N'523',N'525',N'526',N'528',N'529',N'530',N'535',N'540',N'545',N'550',N'555',N'560',N'570',N'575',N'576',N'577',N'580',N'581',N'582',N'583',N'588',N'595',N'601',N'605',N'630',N'635',N'645',N'655',N'660',N'670',N'675',N'685',N'696',N'701',N'703',N'705',N'710',N'715',N'720',N'725') AND Invoice_Totals.Status <> 'V'AND Invoice_Totals.DateTime BETWEEN '8/11/2008' AND '9/11/2008'ORDER BYInvoice_Totals.Store_Id,Invoice_Totals.Invoice_Number
I posted query text plan in the recent post
September 11th, 2008 5:55pm
Hi Friend,
instead of using IN() make aalias for invoice_totals and use joins
Free Windows Admin Tool Kit Click here and download it now
October 13th, 2010 12:04pm