here Bold code is main issue for slow fetching
Select distinct isnull(V.VendorItemId,0) as VendorItemId , isnull(V.VendorItemNumber,I.Ord_guide) as VendorItemNumber,
isnull(V.VendorItemDescription,I.Pkg_desc) as VendorItemDescription,
Id=(select Min(k.Id) from dbo.InvoiceImportDataPrice k where K.Ord_guide=I.Ord_guide and K.Price=I.Price
and k.Pkg_desc=I.Pkg_desc and k.Store_name=I.Store_name and k.InvoiceImportHistoryId = 128),
I.InvoiceImportHistoryId,I.Price,isnull(V.CurrentPrice,0) as CurrentPrice,ISNULL(ISNULL(V.CurrentPrice, 0) - ISNULL(I.Price, 0), 0) AS differencePrice,
I.Invoice_No, I.Sold_to, I.Store_name, I.Deliv_date,
( CASE WHEN isnull(I.Qty_ship,'0')='0' THEN '-0' else I.Qty_ship END ) AS Qty_ship, I.Ship_um,
( CASE WHEN isnull(I.Qty_ord,'0')='0' THEN '-0' else I.Qty_ord END ) AS Qty_ord, I.Item_no, I.Weight,
( CASE WHEN isnull(I.Amount,'0')='0' THEN '-0' else I.Amount END ) AS Amount, I.Pkg_desc, I.Tax_amount, I.Ord_guide
,(select count(*) FROM dbo.InvoiceImportDataPrice IP where IP.Pkg_desc=V.VendorItemDescription
and IP.Item_no= V.VendorItemNumber and IP.Price = V.CurrentPrice)
as PriceDiscrepancyCount
from dbo.InvoiceImportDataPrice I
left join dbo.VendorItems V WITH (NOLOCK) on V.VendorItemNumber = I.Ord_guide
where (I.InvoiceImportHistoryId = 128 and VendorItemDescription != '"Sales Tax"') or
(I.InvoiceImportHistoryId = 128 and isnull(V.VendorItemDescription,'')='')
What I have tried:
when i off both code then data fetch fast but i need both code. total no of records is 2833