Here is the microsoft document for how to optimize a json column with out using OpenJson and only using Json_Value: https://learn.microsoft.com/en-us/sql/relational-databases/json/index-json-data?view=sql-server-ver15
My issue is that I have a JSON column that contains an array where I am trying to grab all keys called Test_ID from each element in the array to compare with a joined statements testId and, while it works, its relatively slow. Takes about 9 seconds for 400 rows. I am trying to speed this up exponentially and it seems the only way to do so is through the indexing mentioned in that article but I can not seem to figure out how to do it for an array.
My JSON is similar to this: '{"Property":{"Label":"0"},"Tests":[{"Test_ID":"GUID_HERE","Type":{"Label":" "},"Name":{"Label":" "},"Value":null,{"Test_ID":"GUID_HERE","Type":{"Label":" "},"Name":{"Label":" "},"Value":" "}]}'
Here is my scrubbed query
SELECT DISTINCT w.W_ID,
'Proc' ProcHeaderName, p.ProcNumber ProcValue,
'Class' ClassHeaderName, p.Class ClassValue
INTO #Procs
FROM proc p
LEFT JOIN (SELECT wt.W_ID, wt.TestId
from TestValue wt where wt.IsDeleted = 0) as wtRow on wtRow.W_ID in (SELECT ID FROM #tmp)
LEFT JOIN TableNameHere c on c.IsDeleted = 0 and c.col_ID in (SELECT col_ID FROM tmp)
WHERE p.IsDeleted = 0 and [dbo].[GetTestIdJson](c.Json, wtRow.TestId) = wtRow.TestId
AND p.ProcNumber + ',' + p.RNumber = JSON_VALUE(c.Json,'$.Property.Label') + ',' + JSON_VALUE(c.Json,'$.Property.Label')
GROUP BY wtRow.W_ID, p.ProcNumber, p.Class