Sql 在循环中一次获取一条记录需要很长时间。有办法绕过它吗?

Sql 在循环中一次获取一条记录需要很长时间。有办法绕过它吗?,sql,sql-server,performance,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.

这就是我所拥有的,需要永远。我想不出有什么不同的方法。 我试过更慢的游标。有什么想法吗?多谢各位

   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
declare@i int
声明@customer_sk int
声明@numrows int
声明@iprotable表(idx int主键标识(1,1),customer_sk int)
插入@iprotable
从IproProfile ipro INTERNAR join VWUSAGELAST 60天使用期中选择不同的ipro.Customer_SK。Customer_SK=ipro.Customer_SK
设置@i=1
设置@numrows=(从@iprotable中选择COUNT(*)
如果@numrows>0

虽然(@i你有索引吗?你在使用什么数据库引擎?你在使用什么数据库?你能解释一下你在尝试做什么,你的确切问题是什么吗?你的RDBMS是SQL Server吗?它是SQL Server 2008。我需要更新一个表,其中包含过去60天内每个客户的顶级分类。vwUsageLast60Days视图包含所有内容过去60天的分类。我正在做的是浏览表格,我需要更新获得客户id,然后根据客户id进行分类,并选择点击次数最多的分类。它工作正常,但速度非常慢。我不熟悉cte,但我认为这可能是一种方法?该表格有一个p一元密钥是客户密钥,没有其他索引。