I have a table with devices, and a second table with performance counters. So I have device A which has lets say 1000 entries in the performance counters table. I am trying to get all the devices, 2500 of them, and their most recent performance counter. I have used an Outer Apply, which does accomplish the desired results, but the query takes 5 minutes to execute.
Is this the best way to get the info I am looking for or is there a better way? Is the performance of this query normal, and I am just asking too much? I am thinking of creating a third ‘temp’ table nightly that will hold this data so I can display and query the info quickly. What are the best practices for something like this?
Here is my current query:
select * from Device D
OUTER APPLY
( select top 1 *
FROM PerformanceCounterValues
where instance_id = D.pkid
order by collection_time desc
) as tbl2
—– Solved —-
Here is my final actual query, and it works in 0 seconds.
SELECT pkid, D.IDDevice, D.IDHardware, H.IDRecorder, D.Name, D.Description, H.Name as HardwareName, H.URI, R.HostName, R.Name as RecorderInstance, tbl2.collection_time, tbl2.raw_value, tbl2.calculated_value
FROM [Surveillance].[dbo].[PerformanceCounterInstance] PCI
JOIN [Surveillance].[dbo].[Devices] D on D.IDDevice = (SELECT REPLACE(STUFF([instance_name],1,CHARINDEX('[',[instance_name]),''),']',''))
JOIN [Surveillance].[dbo].[Hardware] H on H.IDHardware = D.IDHardware
JOIN [Surveillance].[dbo].[Recorders] R on R.IDRecorder = H.IDRecorder
OUTER APPLY
( select top 1 *
FROM [Surveillance].[dbo].[PerformanceCounterValue]
where instance_id = PCI.pkid
order by collection_time desc
) as tbl2
where category_name = 'VideoOS Recording Server Device Storage'
and D.Enabled = 1
It was just setting up the index that made the speed increase.
Enabled Index and used this query: