2

我想我已经破解了一种生成随机数的方法。但是 SQL Server 中有效循环这个的最佳方法是什么?我有以下 SQL:

DECLARE @Random1 INT;
DECLARE @Random2 INT;
DECLARE @Random3 INT;
DECLARE @Random4 INT;
DECLARE @Random5 INT;
DECLARE @Random6 INT;

DECLARE @Upper INT;
DECLARE @Lower INT;

---- This will create a random number between 1 and 49
SET @Lower = 1 ---- The lowest random number
SET @Upper = 49 ---- The highest random number
SELECT @Random1 = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
SELECT @Random2 = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
SELECT @Random3 = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
SELECT @Random4 = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
SELECT @Random5 = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
SELECT @Random6 = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
--SELECT @Random;

INSERT INTO [lotto].[dbo].[CustomerSelections]
           ([draw_date]
           ,[val1]
           ,[val2]
           ,[val3]
           ,[val4]
           ,[val5]
           ,[val6])
     VALUES
           (
           '2013-07-05'
           ,@Random1
           ,@Random2
           ,@Random3
           ,@Random4
           ,@Random5
           ,@Random6 
           )

让 SQL Server 运行此 SQL 100,000 次的最佳方法是什么?

4

2 回答 2

12

在一个没有循环的语句中完成所有操作将是最有效的方法。

INSERT INTO [lotto].[dbo].[CustomerSelections]
           ([draw_date]
           ,[val1]
           ,[val2]
           ,[val3]
           ,[val4]
           ,[val5]
           ,[val6])
SELECT TOP (100000 )
           '2013-07-05',
           1 + ABS(CRYPT_GEN_RANDOM(8) % 49),
           1 + ABS(CRYPT_GEN_RANDOM(8) % 49),
           1 + ABS(CRYPT_GEN_RANDOM(8) % 49),
           1 + ABS(CRYPT_GEN_RANDOM(8) % 49),
           1 + ABS(CRYPT_GEN_RANDOM(8) % 49),
           1 + ABS(CRYPT_GEN_RANDOM(8) % 49)
FROM master..spt_values v1,
     master..spt_values v2
于 2013-07-01T21:15:27.973 回答
2

这花了 4 秒:

SET NOCOUNT ON
DECLARE @intFlag INT
SET @intFlag = 1
WHILE (@intFlag <=100000)
BEGIN

--Do Stuff
DECLARE @Random1 INT, @Random2 INT, @Random3 INT, @Random4 INT, @Random5 INT, @Random6 INT, @Upper INT, @Lower INT
---- This will create a random number between 1 and 49
SET @Lower = 1 ---- The lowest random number
SET @Upper = 49 ---- The highest random number
SELECT @Random1 = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
SELECT @Random2 = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
SELECT @Random3 = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
SELECT @Random4 = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
SELECT @Random5 = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
SELECT @Random6 = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
--SELECT @Random;

INSERT INTO #test  ([draw_date],[val1],[val2],[val3],[val4],[val5],[val6])
     VALUES ('2013-07-05',@Random1,@Random2,@Random3,@Random4,@Random5,@Random6)  

SET @intFlag = @intFlag + 1
END
GO

更新:这运行得很快,因为它是在一个临时表中,直接循环到实际表中会是一个慢得多的过程,所以你想从你的临时表中将集合插入到你的最终表中:

INSERT INTO Table
SELECT *
FROM #test

作为参考,一个简单的 WHILE 循环外壳:

DECLARE @intFlag INT
SET @intFlag = 1
WHILE (@intFlag <=5000)
BEGIN  
--Do Stuff

--Iterate
SET @intFlag = @intFlag + 1
END
GO
于 2013-07-01T21:08:42.447 回答