我正在使用由此处成员之一提供的解决方案来遍历所有记录(当前大约为1000)并对每条记录执行大约60个SELECT查询。这大约有6万个计算结果。这是每天一次。我担心的是,在这种情况下,所有记录的“循环”是否是最有效和最快的。通过SQL表循环并执行多个查询的最快方法
DECLARE @LastCustomerID CHAR(10)
SET @LastCustomerID = 0
-- define the customer ID to be handled now
DECLARE @CustomerIDToHandle CHAR(10)
-- select the next customer to handle
SELECT TOP 1 @CustomerIDToHandle = bl_id
FROM dbo.bl
WHERE bl_id > @LastCustomerID AND status = 'a'
ORDER BY bl_id
-- as long as we have customers......
WHILE @CustomerIDToHandle IS NOT NULL
BEGIN
-- call your sproc. this is where i have the 60 SQL SELECT queries defined
-- in another stored procedure called myStoredProc. it just takes the parameter
-- @CustomerIDToHandle provided here and uses it in the where clauses
EXEC myStoredProc @CustomerIDToHandle
-- set the last customer handled to the one we just handled
SET @LastCustomerID = @CustomerIDToHandle
SET @CustomerIDToHandle = NULL
-- select the next customer to handle
SELECT TOP 1 @CustomerIDToHandle = bl_id
FROM dbo.bl
WHERE bl_id > @LastCustomerID AND status = 'a'
ORDER BY bl_id
END
问题是这是否是执行上述场景的最有效方式。我正在寻找替代品,如果有任何... – dido 2012-04-03 15:44:50
“最有效和最快”的方法是重写'myStoredProc',并在那里做一切与你在'dbo.bl'的连接。 – 2012-04-03 15:45:44
几乎肯定不是。正如你正确地计算出你正在执行60,000个选择查询。你对这些查询的输出做了什么,是否有必要一次完成这一个客户? SQL是基于集合的,并且在可能的情况下应该避免循环,几乎总是有一个更高效的基于集合的解决方案。 – GarethD 2012-04-03 15:46:16