昨天我问了一个关于CTE和运行总数计算的问题;
Calculating information by using values from previous line
我想出了一个解决方案,然而,当我将其应用于我的实际数据库(超过450万条记录)时,似乎要花费很长时间。在我停止它之前,它运行了3个多小时。然后我尝试在一个子集上运行它(CTEtest as (选择TOP100)),它已经运行了一个半小时。这是因为在选择前100名之前,它仍然需要遍历整个过程吗?或者我应该假设,如果这个查询100条记录需要2个小时,那么450万条记录需要几天?我如何优化这一点?
有没有办法查看查询还剩下多少时间?
发布于 2013-01-24 14:53:34
我认为您最好将运行总和作为相关子查询来执行。这将使您能够更好地管理索引以提高性能:
select memberid,
(select sum(balance - netamt) as runningsum
from txn_by_month t2
where t2.memberid = t.memberid and
t2.accountid <= t.accountid
) as RunningSum
from txn_by_month t
使用这种结构,txn_by_month(memberid, accountid, balance, netamt)
上的索引应该能够满足查询的这一部分,而不需要返回到原始数据。
https://stackoverflow.com/questions/14503895
复制相似问题