为什么这个嵌套的SQL代码如此之慢

时间:2015-11-10 02:30:43

标签: sql-server adodb

将ADODB记录集与SQL Server 2016一起使用 - 此代码运行比我执行select查询慢,然后通过循环记录集手动进行聚合..任何想法为什么?

SELECT SUM(wk1) AS wk1, SUM(wk2) AS wk2, SUM(wk3) AS wk3
    , SUM(recCount) AS recCount, MIN(Segment) AS Segment, Bucket
FROM (
    SELECT *, 1 AS recCount, NTILE(8) OVER (ORDER BY NEWID()) AS Bucket
    FROM [Data]
    WHERE (1 = 1 AND [Area] = 'EAST') AND Segment = 2
    ) AS q
GROUP BY Bucket

1 个答案:

答案 0 :(得分:0)

似乎是

NTILE(8) OVER (ORDER BY NEWID()) 

片。如果我改为使用

(ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) % 8) + 1

似乎要快得多。