在循环中一次获取一条记录需要很长时间。有办法解决吗?

时间:2013-11-14 18:21:49

标签: sql sql-server performance

这就是我所拥有的,它需要永远。我无法弄清楚这样做的另一种方式。 我尝试了更慢的游标。有任何想法吗?谢谢。

   declare @i int
    declare @customer_sk  int
    declare @numrows int
    declare @iprotable TABLE (idx int Primary Key IDENTITY(1,1),customer_sk int)
    INSERT @iprotable
    select distinct ipro.Customer_SK from IproProfile ipro inner join vwUsageLast60Days usage on usage.Customer_SK =ipro.Customer_SK 

    SET @i = 1
    SET @numrows = (SELECT COUNT(*) FROM @iprotable)
    IF @numrows > 0
        WHILE (@i <= (SELECT MAX(idx) FROM @iprotable))
        BEGIN
        SET @customer_sk = (SELECT customer_sk FROM @iprotable WHERE idx = @i)
        update IproProfile set TopClassification=x.clas
    from
    (
    select top 1 website web, classification clas, COUNT(classification)cnt, customer_sk cust_sk
    from vwusagelast60days group by website, customer_sk, classification
    having Customer_SK=@customer_sk
    order by cnt desc
    )x
    where Customer_SK=x.cust_sk
        set @i=@i+1
        end

0 个答案:

没有答案