动态SQL运行方式太长

时间:2016-12-01 22:54:06

标签: sql sql-server tsql dynamic-sql

我正在尝试运行一个应该加载超过200M记录的动态SQL查询。我试图一次加载所有记录,但得到了System.OutOfMemory'因此异常决定将时间间隔分成几个月。即使我运行了一个月,以下也会运行一个多小时。我对日期进行了硬编码,然后在没有' While'它运行正常。有人可以检查并告诉我是否有错误。  以下是我的查询

DECLARE @FromDate VARCHAR(MAX)='',
        @params nvarchar(max) = N'@StartDate Date out,@EndDate Date out,@FromDate date out,@ToDate date out',
        @SQL NVARCHAR(MAX)=’’,
        @ToDate VARCHAR(MAX)='', 
        @StartDate VARCHAR(MAX)=’’,
        @EndDate VARCHAR(MAX)=’’;

SELECT  @FromDate= DATEADD(DAY,-365, GETUTCDATE()), @ToDate =     GETUTCDATE();
SELECT @StartDate=@FromDate, @EndDate=CAST(DATEADD(MONTH,1, @FromDate) AS DATE);
SET @SQL = '
WHILE ''' + @FromDate + ''' < ''' + @ToDate + '''
   BEGIN
   INSERT INTO dbo.Sales
   (
   ProductID,
   SaleDate,
   Quantity 
   )
 Select 
      ProductID,
      SaleDate,
      Quantity 
 from 
     OPENQUERY(TeraData1,''
        Select 
        PR_ID           AS   ProductID,
        SL_Date         AS   SaleDate,
        PR_QTY          AS   Quantity
    FROM
        Sales.Product
    where
SaleDate BETWEEN ' + @StartDate + 'AND '+ @EndDate + '
'')

SET  @FromDate =DATEADD(MONTH,1,'''+ @FromDate +''')
SET  @StartDate =DATEADD(MONTH,1,'''+ @StartDate +''')
SET  @EndDate =DATEADD(MONTH,1,'''+ @EndDate +''')
END’

 EXECUTE sp_executesql @SQL, @params,
                @FromDate=@FromDate,
                @ToDate=@ToDate,
                @StartDate = @StartDate out,
                @EndDate = @EndDate out;

1 个答案:

答案 0 :(得分:2)

DECLARE @FromDate DATE

SET @FromDate = DATEADD(YEAR,-1,GETUTCDATE())

WHILE @FromDate < GETUTCDATE()
BEGIN
   INSERT INTO dbo.Sales
   (
      ProductID,
      SaleDate,
      Quantity 
   )
 Select 
      ProductID,
      SaleDate,
      Quantity 
 from 
     OPENQUERY(TeraData1,
       Select 
          PR_ID           AS   ProductID,
          SL_Date         AS   SaleDate,
          PR_QTY          AS   Quantity
       FROM
          Sales.Product
       where
          SaleDate >= @FromDate AND SaleDate < DATEADD(MONTH,1,@FromDate)
    )

    SET  @FromDate =DATEADD(MONTH,1,@FromDate)
END

除非您想要动态传递链接服务器名称或其他列,否则看起来您拥有的变量/日期比您需要的多,并且您不需要动态SQL。

同时坚持使用适当的参数数据类型,而不是让SQL推断出你想要的值。

此外,您对BETWEEN的使用会使某些日期重复,因为它会包含在内,以修复比较的限制1方。

如果你想这样做动态地保持非动态SQL中的控制流并在这里传递参数就是一个例子:

DECLARE @FromDate DATE

SET @FromDate = DATEADD(YEAR,-1,GETUTCDATE())

WHILE @FromDate < GETUTCDATE()
BEGIN

    DECLARE @LinkedServerName NVARCHAR(MAX) = 'TeraData1'
    DECLARE @ParamDef NVARCHAR(MAX) = '@FromDate DATE'
    DECLARE @SQL NVARCHAR(MAX)

    SET @SQL = '
      INSERT INTO dbo.Sales
      (
         ProductID,
         SaleDate,
         Quantity 
      )
    Select 
        ProductID,
        SaleDate,
        Quantity 
    from 
        OPENQUERY(' + @LinkedServerName + ',
          Select 
             PR_ID           AS   ProductID,
             SL_Date         AS   SaleDate,
             PR_QTY          AS   Quantity
          FROM
             Sales.Product
          where
             SaleDate >= @FromDate AND SaleDate < DATEADD(MONTH,1,@FromDate)
       )'

    EXECUTE sp_executesql @SQL, @PramDef, @FromDate = @FromDate

    SET  @FromDate =DATEADD(MONTH,1,@FromDate)
END