We write a query that include unpivot, partition by and order by.
Query is:
SELECT PersonId
,SalaryDate
,ID
,Type
,SalaryValue
,ROW_NUMBER() OVER (
PARTITION BY PersonId ORDER BY SalaryValue
) AS rn
FROM (
SELECT lp.PersonId
,lp.SalaryDate
,lp.Salary1
,lp.Salary2
,lp.Salary3
,lp.ID
FROM rdd.Salaries AS lp WITH (NOLOCK)
WHERE lp.SalaryDate > DATEADD(day, - 31, getdate())
) AS t
unpivot(SalaryValue FOR Type IN (
lp.Salary1
,lp.Salary2
,lp.Salary3
)) AS UnpivotTable
The query returns about 68.000.000 rows and execution time is 20 minutes.
Can I improve the query's performance or rewrite effectively?
What is the alternative of partition by?
Best Answer
You may find that the following index and query rewrite performs better, because it sorts per person rather than once over the whole set, and row estimates are more likely to be accurate:
You can omit the
OPTION
clause if you find a parallel query is generated naturally, or if you find non-parallel performance is good enough. The desired plan shape is roughly as follows: