SQL server SQL 脚本执行慢问题
如下sql脚本,执行了很久(10多分钟)还没有完成,查了下,实际的记录只有183条,各位高手帮忙看看,如下代码写的是否有问题?
declare @material varchar(20)
declare @order varchar(20)
declare csmaterial cursor for select materialNO,orderNO from tb_order where orderNO in (select orderNO from tb_product where date between '2012-12-07 00:00:00.000' and '2012-12-10 00:00:00.000' and materialNO = '' and orderNO <> '' )
open csmaterial
fetch next from csmaterial into @material,@order
WHILE @@FETCH_STATUS = 0
begin
update tb_product set materialNO = @material where orderNO = @order and date between '2012-12-07 00:00:00.000' and '2012-12-10 00:00:00.000'
fetch next from csmaterial into @material,@order
end
close csmaterial
deallocate csmaterial
sql?server
脚本
------解决方案--------------------180条用游标问题不大,但是180万条你就惨了,你先要摒弃面向过程的思路,数据库是面向集合的,如非必要,不要一条一条处理,可以把逻辑放到where或者借助case when来解决。