我使用 SQL Server 2008,我的 SQL 查询如下所示:
select * from (
select row_number() over(PARTITION by tb01.P_USER order by tb04.P_data7 desc) Row
,tb01.P_USER as 'enterpriseID'
,tb01.P_ID as 'greenhouseID'
,tb01.P_NAME as 'greenhouseName'
,tb04.P_data1 as 'airTemp'
,tb04.P_data2 as 'airHi'
,tb04.P_data3 as 'soilTemp'
,tb04.P_data4 as 'soilHi'
,tb04.P_data5 as 'co'
,tb04.P_data6 as 'sun'
,tb04.P_data7 as 'pickingTime'
,tb05.P_data5 as 'params'
from TB04 tb04,TB01 tb01,TB05 tb05,TB12 tb12
where tb04.P_data8 = tb05.P_data2
and tb05.P_data1=tb01.P_ID
and tb01.P_USER = tb12.P_data1
and tb12.CodeId = '410621'
) result where Row between (3-1)*20+1 and 3*20
我想挑出其中的 20 个,但是当我运行这个 SQL 时,花了大约 8 秒,这当然超出了我们的预期。
有人能帮我吗?谢谢。
编辑:我删除了“tb01.P_USER 的 PARTITION”,它的查询速度翻了一番。现在执行计划列表如下:
排序占用了 98% 的查询时间,我该怎么办?如果必须,我应该删除过度功能并使用另一种更有效的方法吗?