group by sum 优化 DBCC FREEPROCCACHE DBCC DROPCLEANBUFFERS set statistics io on SET STATISTICS time ON
SELECT top 10 [UserID] , sum([Score]) score FROM [users] group by UserID order by Score desc
500w数据. userid, 非聚焦索引 score 非聚焦索引
时间11秒. 测试电脑配置 amd640(3.0); 8G内存;
------解决方案--------------------
SQL code
-->try
select t.[UserID],sum(t.[Score]) Score
from
(
SELECT top 10 [UserID],[Score]
FROM [users]
group by UserID
order by Score desc
) t
------解决方案-------------------- 不好意思,上面那个写的有问题
------解决方案-------------------- 这还可以再优化?
------解决方案--------------------
SQL code
-->try
SELECT top 10 [UserID] into #tmp
FROM [users] group by UserID
select [UserID], sum([Score]) Score from [users]
where [UserID] in (select t.[UserID] from #tmp t)
order by Score desc
--drop table #tmp
------解决方案-------------------- 发一下生成表结构的脚本看看
------解决方案--------------------
------解决方案--------------------
SQL code
SELECT TOP 10
[UserID] ,
SUM([Score]) score
FROM [users]
GROUP BY UserID
ORDER BY Score DESC
------解决方案-------------------- userid, 非聚焦索引 score 非聚焦索引