Thursday, February 2, 2012

SQL Server: Query optimization

While you are joining multiple queries instead of using filteration by where clause use filteration at the time of joining. this will increase performance specially when they are millions of rows in ur database

For example

SELECT ST.STId,DistPro.ProductId ,RawST.BatchNo,RawST.ExpiryDate,RawSt.Quantity,RawST.Bonus,RawSt.Price,RawSt.DiscAmount,RawSt.NetAmount,HOPro.TP
FROM RawStockTransaction AS RawST INNER JOIN
InvoiceTransactions ST on RAWSt.DistInvoiceNo=ST.DistInvoiceNo and RawSt.InvoiceTypeId=ST.InvoiceTypeId INNER JOIN
DistProducts DistPro on DistPro.DistProductCode=RawST.DistProductCode Inner join
DistCustomers DistCus on ST.CustomerId=DistCus.CustomerId And RawST.DistCustomerCode=DistCus.DistCustomerCode
Left outer join HOProducts HOPro on DistPro.HOProductId=HOPro.HOProductId
Left outer join
(Select DistInvoiceNo,ST.InvoiceTypeId ,DistCus.DistCustomerCode, DistPro.DistProductCode
From InvoiceTransactions ST Inner Join InvoiceTransactionBody STB on St.StId=STB.STID Inner join
DistProducts DistPro on DistPro.ProductId=STB.ProductId Inner join
DistCustomers DistCus on ST.CustomerId=DistCus.CustomerId
Where ST.DistributorId=@DistributorId And ST.InvoiceDate between @FromDate and @ToDate
)as ec
on ec.DistInvoiceNo=RawST.DistInvoiceNo and ec.InvoiceTypeId=RawST.InvoiceTypeId
And ec.DistCustomerCode=DistCus.DistCustomerCode
And ec.DistProductCode=DistPro.DistProductCode
Where RawST.FileId=@FileId And ST.InvoiceDate between @FromDate and @ToDate And ST.DistributorId=@DistributorId
And ec.DistInvoiceNo is null and ec.InvoiceTypeId is null
and ec.DistCustomerCode is null and ec.DistProductCode is null

This query take 1.5 to 2 min to run for fetching 50k rows.
If we use filteration like this one it increase its performance and fetch data in seconds
SELECT ST.STId,DistPro.ProductId ,RawST.BatchNo,RawST.ExpiryDate,RawSt.Quantity,RawST.Bonus,RawSt.Price,RawSt.DiscAmount,RawSt.NetAmount,HOPro.TP
FROM RawStockTransaction AS RawST INNER JOIN
InvoiceTransactions ST on RAWSt.DistInvoiceNo=ST.DistInvoiceNo and RawSt.InvoiceTypeId=ST.InvoiceTypeId And ST.DistributorId=@DistributorId INNER JOIN
DistProducts DistPro on DistPro.DistProductCode=RawST.DistProductCode And DistPro.DistributorId=@DistributorId Inner join
DistCustomers DistCus on ST.CustomerId=DistCus.CustomerId And RawST.DistCustomerCode=DistCus.DistCustomerCode And DistCus.DistributorId=@DistributorId
Left outer join HOProducts HOPro on DistPro.HOProductId=HOPro.HOProductId
Left outer join
(Select DistInvoiceNo,ST.InvoiceTypeId ,DistCus.DistCustomerCode, DistPro.DistProductCode
From InvoiceTransactions ST Inner Join InvoiceTransactionBody STB on St.StId=STB.STID Inner join
DistProducts DistPro on DistPro.ProductId=STB.ProductId And DistPro.DistributorId=@DistributorId Inner join
DistCustomers DistCus on ST.CustomerId=DistCus.CustomerId And DistCus.DistributorId=@DistributorId
Where ST.DistributorId=@DistributorId And ST.InvoiceDate between @FromDate and @ToDate
)as ec
on ec.DistInvoiceNo=RawST.DistInvoiceNo and ec.InvoiceTypeId=RawST.InvoiceTypeId
And ec.DistCustomerCode=DistCus.DistCustomerCode
And ec.DistProductCode=DistPro.DistProductCode
Where RawST.FileId=@FileId And ST.InvoiceDate between @FromDate and @ToDate
And ec.DistInvoiceNo is null and ec.InvoiceTypeId is null
and ec.DistCustomerCode is null and ec.DistProductCode is null

Note: To see query execution time Instead of running query in query editor write query in stored procedure and see its performance