百万级别数据批量生成:
先创建视图:
-------------------------------------------------------------------------------------------------------
create view v_rand
as
select re=stuff(rtrim(cast(rand() as numeric(7,6))),1,2,'')
go
再创建函数:
Code
--生成随机编号的函数
CREATE FUNCTION f_RANDBH(@BHLen int)
RETURNS varchar(50)
AS
BEGIN
DECLARE @r varchar(50)
IF NOT(ISNULL(@BHLen,0) BETWEEN 1 AND 50)
SET @BHLen=10
SELECT @r=CHAR(65
+(SUBSTRING(re,1,1)
+SUBSTRING(re,2,1)
+SUBSTRING(re,3,1))%26)
+CHAR(65
+(SUBSTRING(re,4,1)
+SUBSTRING(re,5,1)
+SUBSTRING(re,6,1))%26)
FROM v_RAND
WHILE LEN(@r)<@BHLen
SELECT @r=@r+CHAR(65
+(SUBSTRING(re,1,1)
+SUBSTRING(re,2,1)
+SUBSTRING(re,3,1))%26)
+CHAR(65
+(SUBSTRING(re,4,1)
+SUBSTRING(re,5,1)
+SUBSTRING(re,6,1))%26)
FROM v_RAND
RETURN(LEFT(@r,@BHLen))
END
下面是执行SQL生成百万级随机数:P4 2.4 1G 内存的机子在 sql2000里面 生成100万 需要大概3分钟
Code
--生成8位长度的100W条记录的随机数,插入数据库.(这里是六位数字 开头四位字符 结尾2位字符)
USE tempdb 用数据库
GO
CREATE TABLE tb(id varchar(12))
CREATE UNIQUE INDEX IX_tb ON tb(id)
WITH IGNORE_DUP_KEY -- --忽略重复记录的索引
GO
DECLARE @dt datetime
SET @dt = GETDATE()
SET NOCOUNT OFF
DECLARE @row int
SET @row = 1000000 --记录总数为100W
WHILE @row >0
BEGIN
RAISERROR('need %d rows', 10, 1, @row) WITH NOWAIT
SET ROWCOUNT @row
INSERT tb SELECT
id = dbo.f_RANDBH(4)+RIGHT(100000000 + CONVERT(bigint, ABS(CHECKSUM(NEWID()))), 6)+dbo.f_RANDBH(2)
--FROM syscolumns c1, syscolumns c2 -- SET NOCOUNT ON 49 Second
FROM syscolumns c1, sysobjects o -- SET NOCOUNT ON 47 Second
SET @row = @row - @@ROWCOUNT --设置下次循环需要插入的记录数量
END
SELECT BeginDate = @dt, EndDate = GETDATE(), Second = DATEDIFF(Second, @dt, GETDATE())
GO
SELECT COUNT(*) FROM tb
GO
这个执行百万级方法来自
http://www.cnblogs.com/adandelion/articles/542534.html