--bi_delay_goods 推出滞销商品表,按照试销期表中字数值2,计算当天符合滞销条件的商品,门店,商品,日期,当前库存,滞销天数。if not  exists (select 1 from bi_send_log where busdate=@busdate and organ='BI' and tb_name='bi_delay_goods' )
begin
create table #t_delay(
code varchar(18) not null,
organ varchar(10) not null,
busdate datetime not null,
cur_sl money default 0 null,
delay_day int default 0)
create nonclustered index code on #t_delay(code)
 begin tran
declare cur_organ cursor for
select code from bi_organ where nature in(1,4,5)
open cur_organ
fetch cur_organ into @ls_organ
while (@@sqlstatus=0)
begin
--千套游标,
   declare cur_delay cursor for
select flag,number2 from sys_user_code where code='1006' and jb='1' and number2>0
open cur_delay
fetch cur_delay into @ls_code,@ld_num
while (@@sqlstatus=0)
begin
truncate table #t_delay
select @ld_date=dateadd(dd,convert(int,-@ld_num),@busdate)
insert into #t_delay(code,organ,busdate,cur_sl,delay_day)
select code,organ,@busdate,sum(amount),@ld_num from bi_cur_stock where sort like  @ls_code +  '%' and bi_busdate=@busdate
and organ=@ls_organ group by code,organ     delete from #t_delay where code not in(select code from bi_account where sort like @ls_code + '%' and busdate>=@ld_date and organ=@ls_organ)
delete from #t_delay where code not in(select code  from bi_comm_new where t_busdate>@ld_date and organ=@ls_organ) insert into bi_delay_goods(code,organ,busdate,cur_sl,delay_day)
select code,organ,busdate,cur_sl,delay_day from #t_delay /*
insert into bi_delay_goods(code,organ,busdate,cur_sl,delay_day)
select code,organ,@busdate,sum(amount),@ld_num from bi_cur_stock where sort like  @ls_code +  '%' and bi_busdate=@busdate
and organ=@ls_organ
and code  not in(select code from bi_account where sort like @ls_code + '%' and busdate>=@ld_date and organ=@ls_organ)
and code  not in(select code  from bi_comm_new where t_busdate>@ld_date and organ=@ls_organ)
group by code,organ
*/
            IF @@error<>0
               begin
                    rollback tran
               return 1
                     end fetch cur_delay into @ls_code,@ld_num
end
close cur_delay
deallocate cursor cur_delay
fetch cur_organ into @ls_organend
insert into bi_send_log(busdate,organ,tb_name,begin_date,end_date,status)values
(@busdate,'BI','bi_delay_goods',@busdate,getdate(),'1')
            IF @@error<>0
               begin
                    rollback tran
               return 1
                     endcommit tranclose cur_organ
deallocate cursor cur_organ
drop table #t_delay
end以上代码注释之前执行时间长达4小时,表数据有1200万。
修改为现在的语句后,6小时都未执行完成。
机器配置:IBM P550,2*4.2GCPU,32G内存;
bi_cur_stock表结构:
CREATE TABLE bi_cur_stock
(
 DEPART                         varchar(10)                 NOT NULL,
 CODE                           varchar(16)                 NOT NULL,
 ACCEPTID                       varchar(10)                 NOT NULL,
 AMOUNT                         numeric(12,4)               NOT NULL,
 SUM_COST                       numeric(12,2)               NOT NULL,
 VALIDATE                       datetime                    NULL,
 BUSDATE                        datetime                    NULL,
 PROVIDER                       varchar(10)                 NOT NULL,
 PRIORITY                       int                         NOT NULL,
 BARCODE                        varchar(18)                 NOT NULL,
 acctype                        varchar(1)                  NOT NULL,
 JYFS                           varchar(10)                 NOT NULL,
 TAX                            int                         NOT NULL,
 state1                         int                         NULL,
 state2                         int                         NULL,
 cost_statu                     int                         NOT NULL,
 sort                           varchar(10)                 NULL,
 bi_busdate                     datetime                    NOT NULL,
 organ                          varchar(10)                 NULL
)
索引:
alter table dbo.bi_cur_stock add constraint PK_bi_cur_stock primary key nonclustered (DEPART, CODE, ACCEPTID, BARCODE, bi_busdate) ;
create nonclustered index code on dbo.bi_cur_stock (CODE ) ;
create nonclustered index code_organ on dbo.bi_cur_stock (CODE , organ ) ;
create nonclustered index depart on dbo.bi_cur_stock (DEPART ) ;
create nonclustered index sort on dbo.bi_cur_stock (sort ) ;