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.TPFROM RawStockTransaction AS RawST INNER JOINInvoiceTransactions ST on RAWSt.DistInvoiceNo=ST.DistInvoiceNo and RawSt.InvoiceTypeId=ST.InvoiceTypeId And ST.DistributorId=@DistributorId INNER JOINDistProducts DistPro on DistPro.DistProductCode=RawST.DistProductCode And DistPro.DistributorId=@DistributorId Inner joinDistCustomers DistCus on ST.CustomerId=DistCus.CustomerId And RawST.DistCustomerCode=DistCus.DistCustomerCode And DistCus.DistributorId=@DistributorIdLeft outer join HOProducts HOPro on DistPro.HOProductId=HOPro.HOProductIdLeft outer join(Select DistInvoiceNo,ST.InvoiceTypeId ,DistCus.DistCustomerCode, DistPro.DistProductCodeFrom InvoiceTransactions ST Inner Join InvoiceTransactionBody STB on St.StId=STB.STID Inner joinDistProducts DistPro on DistPro.ProductId=STB.ProductId And DistPro.DistributorId=@DistributorId Inner joinDistCustomers DistCus on ST.CustomerId=DistCus.CustomerId And DistCus.DistributorId=@DistributorIdWhere ST.DistributorId=@DistributorId And ST.InvoiceDate between @FromDate and @ToDate)as econ ec.DistInvoiceNo=RawST.DistInvoiceNo and ec.InvoiceTypeId=RawST.InvoiceTypeIdAnd ec.DistCustomerCode=DistCus.DistCustomerCodeAnd ec.DistProductCode=DistPro.DistProductCodeWhere RawST.FileId=@FileId And ST.InvoiceDate between @FromDate and @ToDateAnd ec.DistInvoiceNo is null and ec.InvoiceTypeId is nulland 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