0

我有一个使用视图来提取 6 个平均值的存储过程。SQL 数据库是 SQL Server 2000。当我在查询分析器中运行它时,大约需要 9 秒。我该怎么做才能获得更好的性能?我应该使用 LINQ 返回行并以这种方式确定平均值吗?会更快吗?

这是我当前存储过程的一个示例:

create procedure [TestAvg]
(
    @CustomerNumber int
)
as

select
(select AVG(OrderTime) from OrderDetails where ProductID = 12 and DateDiff(day, DateFulfilled, GetDate()) <= 7 and CustomerNumber = @CustomerNumber) as P12D7,
(select AVG(OrderTime) from OrderDetails where ProductID = 12 and DateDiff(day, DateFulfilled, GetDate()) <= 30 and CustomerNumber = @CustomerNumber) as P12D30,
(select AVG(OrderTime) from OrderDetails where ProductID = 12 and DateDiff(day, DateFulfilled, GetDate()) <= 90 and CustomerNumber = @CustomerNumber) as P12D90,
(select AVG(OrderTime) from OrderDetails where ProductID = 16 and DateDiff(day, DateFulfilled, GetDate()) <= 7 and CustomerNumber = @CustomerNumber) as P16D7,
(select AVG(OrderTime) from OrderDetails where ProductID = 16 and DateDiff(day, DateFulfilled, GetDate()) <= 30 and CustomerNumber = @CustomerNumber) as P16D30,
(select AVG(OrderTime) from OrderDetails where ProductID = 16 and DateDiff(day, DateFulfilled, GetDate()) <= 90 and CustomerNumber = @CustomerNumber) as P16D90

另外,让我澄清一下上述观点。因为这是 SQL Server 2000,所以我不能使用索引视图,因为它确实使用了子查询。我想这可以重写为使用连接。但是,上次我们进行查询并重写它以使用连接时,数据丢失了(因为子查询可以返回一个空值,这将省略整行)。

4

2 回答 2

2

如果未聚合,离开数据库服务器的数据量是多少?执行该操作需要多长时间?数据大小的差异将指导服务器上的计算时间是否超过传输时间和本地计算。

另外 - 查看该DATEDIFF用法并将其更改为更容易使其可优化(尝试 DateFullfilled >= SomeCalculatedDate1 而不是DATEDIFF) - 检查您的执行计划以确保它能够使用索引搜索(最佳)或索引扫描(好)而不是 table_scan。

此外,确保在 CustomerNumber、ProduceID、DateFulfilled 上有一个索引。

于 2009-11-18T17:11:02.933 回答
1

我建议先将数据放入一个表变量中,也许是 2 个表变量,1 个用于 12,1 个用于 16 ProductID。根据这些表变量,根据需要计算 avgs,然后从 sp 返回 tose。

DECLARE @OrderDetails12 TABLE(
        DateFulfilled DATETIME,
        OrderTime FLOAT
)

INSERT INTO @OrderDetails12
SELECT  DateFulfilled,
        OrderTime
FROM    OrderDetails
WHERE   ProductID = 12
AND     DateDiff(day, DateFulfilled, GetDate()) <= 90
and CustomerNumber = @CustomerNumber

DECLARE @OrderDetails16 TABLE(
        DateFulfilled DATETIME,
        OrderTime FLOAT
)

INSERT INTO @OrderDetails16
SELECT  DateFulfilled,
        OrderTime
FROM    OrderDetails
WHERE   ProductID = 16
AND     DateDiff(day, DateFulfilled, GetDate()) <= 90
and CustomerNumber = @CustomerNumber

此外,在表上创建正确的索引也会有很大帮助。

于 2009-11-18T17:06:12.750 回答