Question : Query Slow in some circumstances

Step 1 below takes about 0.45 seconds and step 2 takes about 24 seconds. When I run them separately in a Query Analyser type tool they are both sub second. When they are run together from the application they take 24 seconds. I think sql is using the wrong query plan as the temp table has only just been created ? This is a 3 tier app and both the temp table and result set (and the asset table) contain about 5000 records. The asset table and all lookups are properly indexed


Select a.AssetID, 'AUD' As CurrencyCode, Sum(a.HomeAmount * (b.Sign)) As AssetValue  Into #t_A172_16_4_58_63167_Live  
From  AssetTransaction As a inner JOIN TransactionType As b On a.TransactionTypeID = b.TransactionTypeID
inner JOIN Asset As aass On a.AssetID = aass.AssetID inner JOIN CurrentPeriod As cp On aass.CompanyID = cp.CompanyID  
Where a.BookID = 2 And b.TransactionTypeClass In ('ADDITION') And cp.UserID = 'ADMIN'
And (a.ApplyDate <= cp.EndDate  OR  (a.TransactionTypeID = 1 And aass.DatePurchased <= cp.EndDate)) And a.BudgetID > 0 Group By a.AssetID

Select a.AssetID,a.Barcode,a.Spare8,a.AssetDesc,a.Notes,a.SerialNumber,k.CategoryName,m.LocationName,a.spcComputerName,a.Quantity,a.AssetRef,at0.AssetValue As OriginalValue
From  Asset As a inner JOIN Category As k On a.CategoryID = k.CategoryID inner JOIN Location As m On a.LocationID = m.LocationID
left JOIN #t_A172_16_4_58_63167_Live As at0 On a.AssetID = at0.AssetID  
Where a.BudgetID > 0 Order By DatePurchased

drop table #t_A172_16_4_58_63167_Live  


Answer : Query Slow in some circumstances

SQLOLEDB should always be the fastest because it use SQL native connection. It suppress one layer level.
Random Solutions  
 
programming4us programming4us