我有以下查询来支持按员工姓名排序的员工分页

SELECT rowNumAlias
    ,Employee.employeeId
    ,Employee.NAME
FROM (
    SELECT row_number() OVER (
            ORDER BY Employee.NAME ASC
            ) rowNumAlias
        ,employeeId
        ,NAME
    FROM Employee
    ) employeeData
INNER JOIN Employee ON Employee.employeeId = employeeData.employeeId
WHERE rowNumAlias BETWEEN ? AND ?

其中参数rownumalias可以是1到100之间的任意整数
此查询在我的SQL Server数据库(有100万条记录)上大约需要7秒。有没有方法可以最小化查询执行时间?

最佳答案

你可以这样尝试:

SELECT * FROM (
SELECT (SELECT row_number() OVER (ORDER BY e2.NAME ASC) FROM Employee e2 WHERE Employee.employeeId = E2.employeeId) rowNumAlias,
    ,Employee.employeeId
    ,Employee.NAME
FROM Employee
) e3 WHERE e3.rowNumAlias BETWEEN ? AND ?

关于sql - 该查询的性能调优?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25970467/

10-13 06:37