这个表没有任何外键,ProductID 是Index,但不是唯一的,光执行"Delete from Product_tbl where ProductID = 108" 这个语句都要几十秒,甚至更长时间,这个表里目前有5百多万条数据,where ProductID = 108这个条件下的命中记录大概是30多万。请教有什么办法提高性能,我现在使用的是SqlServer 2005

解决方案 »

  1.   

    SET ROWCOUNT 1000
    Delete from Product_tbl where ProductID = 108
    WHILE @@ROWCOUNT>0
    Delete from Product_tbl where ProductID = 108
    SET ROWCOUNT 0用循環
      

  2.   

    这里正解。 对于大数据量的删除使用循环。 这样线上程序就不会有什么影响。
    set rowcount 行数  这个可以根据你的执行一次的时间进行设定。 
      

  3.   

    500W中删除30W数据都会慢,用此方法应该会快不少,楼主用select 查时花时间是多少ProductID--是非聚集索引还是?
      

  4.   

    ProductID这个字段是不唯一的
      

  5.   

    SET ROWCOUNT 1000
    Delete from Product_tbl where ProductID = 108
    WHILE @@ROWCOUNT>0
        Delete from Product_tbl where ProductID = 108
    SET ROWCOUNT 0sql循环
    哈好研究
      

  6.   

    -- Delete records with every 1000 deletes a commit 
    declare @loop int, @tel int  select @loop =  count(*) / 1000 + 1 from demo set @tel = 0 while  @tel < @loop begin     begin transaction      delete demo from (select top 1000 * from demo) as T1 where demo.date < dateadd(m,-1,getdate())     commit transaction     set @tel = @tel + 1 end  
    上面是循环删除 的另一种方法