-1

我正在尝试从此存储过程中删除游标,但不确定在有效语句中运行这种操作的最佳最新最佳实践。

任何人都可以提供任何伪代码来实现从开发人员的角度消除这些问题吗?

    --Generate the channel date from a specified date
DECLARE @ConvDate DATETIME
SET @ConvDate = DateAdd(day,-100,getDate())
WHILE DateDiff(day,GetDate(), @ConvDate ) < 0 
BEGIN
    EXEC mltGenerateChannelData @ConvDate
    SET @ConvDate = DateAdd(day, 1, @ConvDate)
END


        CREATE PROCEDURE [dbo].[mltGenerateChannelData] (@ConvDate DATETIME)  AS  
        BEGIN  

           DECLARE @ChannelId INT, 
           @URLSignature Varchar(30), 
           @RawSQL VARCHAR(2000), 
           @SQLQuery VARCHAR(4000), 
           @ThisUTMId BIGINT 

           DECLARE cursChannels CURSOR STATIC FOR 
            SELECT 
                ChannelId, 
                URLSignature, 
                RawSQL  
            FROM dbo.TrackingChannel_tbl (NOLOCK) 
            WHERE ProcessVisitDate = 1  

            SET @ConvDate = dbo.datePart_fn(@ConvDate)  

         --Clear out any existing data for this conversion date  
         DELETE FROM TrackingChannelDailyTotal_tbl 
         WHERE TrackingDate = @ConvDate  

         OPEN cursChannels  

         FETCH cursChannels INTO @ChannelId, @URLSignature, @RawSQL  

         CREATE TABLE #UTM 
         (trpUTMID BIGINT PRIMARY KEY, 
         TotalMArgin MONEY, 
         RawURLRequest Varchar(2000), 
         Keywords VARCHAR(1000), 
         VisitDate DATETIME, 
         RefererURL VARCHAR(2000))  

         INSERT INTO #UTM (trputmid, TotalMargin)  
            SELECT trpUTMID, SUM(b.TotalMArgin)  
            FROM TrackingConversion_tbl c(NOLOCK), Booking_tbl b(NOLOCK)               
            WHERE c.BookingId = b.BookingId  
            AND c.BookedDate >= @ConvDate  
            GROUP BY trputmid  

         UPDATE u  
            SET RawURLRequest = v.RawURLRequest,  
             Keywords = v.Keywords,  
                   VisitDate = v.VisitDate,  
                   RefererURL = v.RefererURL  
             FROM #UTM u,    
            TrackingVisit_tbl (NOLOCK)  v   
         WHERE v.trpUTMID = u.trpUTMId  

         CREATE TABLE #UTM2  (trpUTMID BIGINT PRIMARY KEY)  

         WHILE @@FETCH_STATUS = 0  
         BEGIN  

           Print 'Processing Channel Id : ' + Convert(varchar(10), @ChannelId)  

           TRUNCATE TABLE #UTM2  


           SET @SQLQuery = ' INSERT INTO #UTM2 (trpUTMId)  
                             SELECT u.TrpUTMId  
                             FROM #UTM u               
                             WHERE u.VisitDate >= ''' + COnvert(varchar,@ConvDate) + '''   
                             AND u.VisitDate < DateAdd(day,1,''' + Convert(varchar,@ConvDate) + ''') '  


           IF @URLSignature <> ''  
           BEGIN              
            SET @SQLQuery = @SQLQuery + 'AND u.RawURLRequest like ''%' + @URLSignature + '%'' '    
           END  

           IF @RawSQL <> ''  
           BEGIN  
            SET @SQLQuery = @SQLQuery + @RawSQL  
           END  

           EXEC (@SQLQuery)  

           INSERT INTO TrackingChannelDailyTotal_tbl (ChannelId, TrackingDate, Conversions, TotalMargin)  
            SELECT @ChannelId, @ConvDate, Count(u1.trpUTMID), IsNUll(SUM(TotalMargin),0)  
            FROM #UTM u1, #UTM2 u2  
            WHERE u1.TRputmid = u2.trputmid  


           FETCH cursChannels INTO @ChannelId, @URLSignature, @RawSQL  

         END  

         CLOSE cursChannels  
         DEALLOCATE cursChannels  
4

2 回答 2

0

如果您使用 SSMS 工具 (http://www.ssmstoolspack.com/) 和 'Include Actual Execution plan' 与查询,您可以打破查询中的瓶颈,采取每个部分并尝试隔离和改进询问。

您可能会发现问题出在查询的不同部分,而不是光标。

于 2012-06-21T15:36:09.727 回答
-1

如果旧服务器是指内存或处理能力低,并且如果升级硬件作为 DBA 是可行的,我会建议升级硬件。

于 2012-06-22T11:23:08.220 回答