优化SQL Scheduler以从select语句插入数据

时间:2012-03-07 03:21:17

标签: sql-server-2008 query-optimization

任何人都知道如何优化SQL查询以从select语句插入数据,因此日志文件不会变得太快?我已经尝试过WITH(NOLOCK)了。它可以工作,但日志文件增长得非常快。

目前我有SQL Scheduler将数据从一个数据库移动到同一服务器和同一个实例下的另一个数据库。

TRUNCATE TABLE Mobile.dbo.[Customer] 

INSERT INTO Mobile.dbo.[Customer]
SELECT
    CST.[No], CST.[Name], CST.[Address], CST.[Address 2], CST.[Post Code], CST.[City], CST.[County] AS [State], 
    CST.[Country_Region Code] AS [Country Code], CST.[Contact], CST.[Phone No_], CST.[Fax No_], CST.[Telex No_] AS [Mobile No_],
    CST.[E-Mail], CST.[Home Page], CAST(CST.[Credit Limit] AS DECIMAL (38,2)) AS [Credit Limit], 
    CASE 
        WHEN (CAST(INV.[Amount] AS DECIMAL) IS NOT NULL) THEN CAST((INV.[Amount]) AS DECIMAL (38,2))
        ELSE '0.00'
    END AS [Balance],
    CST.[Salesperson Code], CST.[Payment Terms], CST.[Payment Code], CST.Zone Code]
FROM Live.dbo.[Customer] AS CST
WITH (NOLOCK)
LEFT JOIN(
    SELECT ORIamt.[Customer No_], SUM(ORIamt.[Amount]) AS [Amount]
    FROM (
        SELECT CLE.[Entry No_], CLE.[Customer No_], CLE.[Document No_], DCLE.[Amount]
        FROM Live.dbo.[Entry] AS CLE
        WITH (NOLOCK)
        LEFT JOIN(
            SELECT [Cust_ Entry No_], SUM([Amount]) AS [Amount]
            FROM Live.dbo.[Detailed Entry]
            WITH (NOLOCK)
            GROUP BY [Cust_ Entry No_]
        ) DCLE ON CLE.[Entry No_] = DCLE.[Cust_ Entry No_]
    ) AS ORIamt
    GROUP BY ORIamt.[Customer No_]
) AS INV ON INV.[Customer No_] = CST.[No_]

大家都知道,我有一个名为Mobile的数据库。实际上,该数据库仅用于显示数据。由于此数据库仅用于显示数据,因此我想知道如何优化我的SQL Scheduler,因此日志文件不会变得太快。

1 个答案:

答案 0 :(得分:0)

由于您正在截断Mobile.dbo.Customer,并且移动设备处于简单恢复模式,因此删除Mobile.dbo.Customer然后使用Select..into Mobile.dbo.Customer..而不是{{1}重新创建它是否可行}}?这种方法应该提供您所需的最少日志记录。