怎么会这么慢?
自己试着编的小东西,在一个5万条记录的表里一秒一条记录,应该还能更快,请高手给指点下,哪里写的不好,还可以优化呀,请多指教!
declare @t int,@c int,@d int,@d1 int,@x int,@j numeric,@m numeric,@m1 numeric,@h int,@h1 int,@h2 int,@kh1 as char(10)
declare @yyy as char(10),@z int,@z1 int
declare @ddd as int
declare @str varchar(4)
declare @i varchar(10),@ii varchar(10)
declare @p varchar(50)
declare @t1 int,@tt int
declare @tttt4 table(产品编号 char(13),票号 char(16),序号 int,客户 char(8))
set @yyy= '20040101 '--月份
set @str= 'A01C '
while cast(substring(@yyy,5,2) as int) <13
begin
set @c=0 /*在下面输入每月的金额*/
set @m=(case when substring(@yyy,5,2)= '01 ' then 2863755 when substring(@yyy,5,2)= '02 ' then 3400709 when substring(@yyy,5,2)= '03 ' then 3153385 when substring(@yyy,5,2)= '04 ' then 3215216 when substring(@yyy,5,2)= '05 ' then 3246132 when substring(@yyy,5,2)= '06 ' then 4142682 when substring(@yyy,5,2)= '07 ' then 3709865 when substring(@yyy,5,2)= '08 ' then 3579694 when substring(@yyy,5,2)= '09 ' then 3508100 when substring(@yyy,5,2)= '10 ' then 3400709 when substring(@yyy,5,2)= '11 ' then 4617806 when substring(@yyy,5,2)= '12 ' then 5550144 end)
set @j=round(@m/cast(day(dateadd(mm,1,@yyy)-1) as int),2)--日平均金额
set @ddd=0
while (select cast(day(dateadd(mm,1,@yyy)-1) as int))> @ddd
begin
set @d1=1
set @m1=0
set @i=substring(convert(varchar(8),cast(convert(varchar,(@yyy+@ddd),112) as datetime)-30*cast(6+rand()*(9-6) as int),112),3,6)
set @ii=substring(convert(varchar(8),cast(convert(varchar,(@yyy+@ddd),112) as datetime)-5,112),3,6)
while @j> @m1
begin
SET @h1=0
set @d=cast(1+rand()*(4-1) as int)
set @kh1=(case when @d=1 then (select top 1 客户编码 from kh where 档案号= 'A ' order by newid()) when @d=2 then (select top 1 客户编码 from kh where 档案号 in( 'B ', 'C ') order by newid()) end)
set @p=(@str+left(convert(varchar,(@yyy+@ddd),112),8)+(case when len(@d1)=1 then '000 ' when len(@d1)=2 then '00 ' when len(@d1)=3 then '0 ' end)+cast(@d1 as char(4)))
if @d=1
begin
set @t1=cast(1+rand()*(20-1) as int)
set @x=1