So this is a piece of a stored procedure that I'm tuning. I have found that this runs very slow. I takes about 3 hours. I think database may lack indexes or views. I believe that the join
s are the reason that it's so slow, but I don't know a way to split the joins up so Sql dosn't have a hard time processing them. I thought temp table. If you could help thanks.
SELECT ftrec.AccountName,
cast(ftrec.AssetId as varchar(50)) as AssetId,
va.Class as Category,
gt.Name as RegisteredGrade,
gtu.Name as FinishedGrade,
'Recycling' as SKU,
DATENAME(month,ftrec.CreatedOn) + ' ' + cast(DATEPART(yyyy,ftrec.CreatedOn) as varchar(50)) AS ServiceCompleteMonth,
ISNULL(CASE ftRec.RateTypeID
WHEN 1 THEN a.Weight
ELSE 1 END * ftRec.Rate,0) as ServiceTotal,
itwrec.Name as 'Warehouse',
asu.UserName as 'TechnicianName'
FROM itAsset a (nolock)
inner JOIN vAssetSimpleView va on a.AssetRecId = va.AssetId
inner JOIN dbo.itGradeTypes gtu ON gtu.RecID = a.Upgrade_Grade
inner JOIN itGradeTypes gt ON va.Grade = gt.RecId
inner JOIN FinancialTransactions ftRec (nolock) ON ((ftRec.AssetID = a.AssetRecID) OR (ftRec.FKeyID = a.OrigTransactionID and ftRec.AssetID = 0)) AND ftRec.FinancialTypeID = 1
INNER JOIN ittransactionprocess tpRec (nolock) on ftRec.fkeyid = tpRec.transactionid
inner JOIN dbo.itLotTrips lt ON a.OrigLotNumber = lt.LotNumber
inner JOIN dbo.itDocks d ON lt.DockID = d.DockID
inner JOIN dbo.itWarehouse itwrec ON d.WarehouseID = itwrec.WarehouseID
--LEFT JOIN itWarehouse itwrec (nolock) on ftrec.WarehouseID = itwrec.WarehouseID
inner JOIN Aspnet_users asu (nolock) ON asu.userid = tprec.ModifiedBy
WHERE ftrec.CreatedOn > @StartDate AND ftrec.CreatedOn < @EndDate
AND a.LockedStatus = 5
--and ftrec.Assetid = 285912