Is there a way to make this select retrieve the same results with a single seek?
I'd try this but I have no idea if it will be more efficient. You need the DISTINCT
to remove duplicates, so the UNION ALL
might be more appropriate, no need for two distinct operations:
SELECT DISTINCT
JobRunId = @JobRunID,
d.*
FROM dbo.TestUpload
CROSS APPLY
( SELECT
ThingAName AS Name,
ThingAType AS [Type],
ThingAGranularity AS Granularity
UNION -- or UNION ALL
SELECT
ThingBName,
ThingBType,
ThingBGranularity
) AS d
WHERE JobRunId = @JobRunID ;
UNION ALL
plan:
UNION
plan:
Use cross apply to unpivot columns into rows
SELECT --DISTINCT most probably
JobRunId,
ut.Name,
ut.[Type],
ut.Granularity
FROM dbo.TestUpload
CROSS APPLY (
SELECT ThingBName AS Name,
ThingBType AS [Type],
ThingBGranularity AS Granularity
UNION ALL
SELECT ThingAName AS Name,
ThingAType AS [Type],
ThingAGranularity AS Granularity
) ut
WHERE JobRunId = @JobRunID