我开发了一个跨sql-servers同步数据的工具。该工具负责更新或将源表中的记录插入目标表。应在目标表中不存在的行上执行插入。应对目标表中已存在的行执行更新。
之前我在代码中对表进行了比较,但事实证明这非常慢(至少我编写它的方式)。现在我使用一个 sql查询进行插入,使用一个进行更新。性能更好,但仍然很慢。下面是我的SQL命令。你看到任何可以优化的东西吗?例如。我可以使用外连接以外更快的东西吗?
更新命令:
UPDATE [Respondent]
SET [SampleGroupID] = ls.[SampleGroupID], [RespondentStatusTypeID] = ls.[RespondentStatusTypeID], [PilotTest] = ls.[PilotTest], [Username] = ls.[Username], [Password] = ls.[Password], [ImportUniqueID] = ls.[ImportUniqueID], [WORespID] = ls.[WORespID], [DateLastAnswered] = ls.[DateLastAnswered], [DateCreated] = ls.[DateCreated], [Email] = ls.[Email], [Phone] = ls.[Phone], [Name] = ls.[Name], [JobTitle] = ls.[JobTitle], [PhoneDirect] = ls.[PhoneDirect], [PhoneMobile] = ls.[PhoneMobile], [Fax] = ls.[Fax], [CompanyName] = ls.[CompanyName], [Department] = ls.[Department], [Address] = ls.[Address], [Address1] = ls.[Address1], [Address2] = ls.[Address2], [Zipcode] = ls.[Zipcode], [City] = ls.[City], [District] = ls.[District], [CountryCode] = ls.[CountryCode], [Country] = ls.[Country], [Info1] = ls.[Info1], [Info2] = ls.[Info2], [Info3] = ls.[Info3], [Info4] = ls.[Info4], [Info5] = ls.[Info5], [Info6] = ls.[Info6], [Info7] = ls.[Info7], [Info8] = ls.[Info8], [Info9] = ls.[Info9], [Info10] = ls.[Info10], [Info11] = ls.[Info11], [Info12] = ls.[Info12], [Info13] = ls.[Info13], [Info14] = ls.[Info14], [Info15] = ls.[Info15], [Info16] = ls.[Info16], [Info17] = ls.[Info17], [Info18] = ls.[Info18], [Info19] = ls.[Info19], [Info20] = ls.[Info20], [PublicIdentifier] = ls.[PublicIdentifier], [LockedByUser] = ls.[LockedByUser], [DialAttempts] = ls.[DialAttempts], [DialProbationExpires] = ls.[DialProbationExpires], [CATIScreenOptionID] = ls.[CATIScreenOptionID], [CATIPersonID] = ls.[CATIPersonID], [TotalDuration] = ls.[TotalDuration], [IsExternal] = ls.[IsExternal]
FROM [LinkedServerName].[DBName].[dbo].[Respondent] ls LEFT OUTER JOIN [Respondent] adm
ON ls.RespondentID = adm.RespondentID AND ls.SurveyID = adm.SurveyID
WHERE ls.SurveyID = 1061 AND adm.RespondentID IS NOT NULL
插入命令:
INSERT INTO [Respondent] ([RespondentID], [SurveyID], [SampleGroupID], [RespondentStatusTypeID], [PilotTest], [Username], [Password], [ImportUniqueID], [WORespID], [DateLastAnswered], [DateCreated], [Email], [Phone], [Name], [JobTitle], [PhoneDirect], [PhoneMobile], [Fax], [CompanyName], [Department], [Address], [Address1], [Address2], [Zipcode], [City], [District], [CountryCode], [Country], [Info1], [Info2], [Info3], [Info4], [Info5], [Info6], [Info7], [Info8], [Info9], [Info10], [Info11], [Info12], [Info13], [Info14], [Info15], [Info16], [Info17], [Info18], [Info19], [Info20], [PublicIdentifier], [LockedByUser], [DialAttempts], [DialProbationExpires], [CATIScreenOptionID], [CATIPersonID], [TotalDuration], [IsExternal])
SELECT ls.[RespondentID], ls.[SurveyID], ls.[SampleGroupID], ls.[RespondentStatusTypeID], ls.[PilotTest], ls.[Username], ls.[Password], ls.[ImportUniqueID], ls.[WORespID], ls.[DateLastAnswered], ls.[DateCreated], ls.[Email], ls.[Phone], ls.[Name], ls.[JobTitle], ls.[PhoneDirect], ls.[PhoneMobile], ls.[Fax], ls.[CompanyName], ls.[Department], ls.[Address], ls.[Address1], ls.[Address2], ls.[Zipcode], ls.[City], ls.[District], ls.[CountryCode], ls.[Country], ls.[Info1], ls.[Info2], ls.[Info3], ls.[Info4], ls.[Info5], ls.[Info6], ls.[Info7], ls.[Info8], ls.[Info9], ls.[Info10], ls.[Info11], ls.[Info12], ls.[Info13], ls.[Info14], ls.[Info15], ls.[Info16], ls.[Info17], ls.[Info18], ls.[Info19], ls.[Info20], ls.[PublicIdentifier], ls.[LockedByUser], ls.[DialAttempts], ls.[DialProbationExpires], ls.[CATIScreenOptionID], ls.[CATIPersonID], ls.[TotalDuration], ls.[IsExternal]
FROM [LinkedServerName].[DBName].[dbo].[Respondent] ls LEFT OUTER JOIN [Respondent] adm
ON ls.RespondentID = adm.RespondentID AND ls.SurveyID = adm.SurveyID
WHERE ls.SurveyID = 1061
AND adm.RespondentID IS NULL
答案 0 :(得分:1)
我之前曾试图建立这样一个系统,但事实证明这是一场灾难。我用来在数据库之间同步数据的是名为SQL data Compare,
的Redgate工具答案 1 :(得分:1)
如果使用SQL Server 2008或更高版本,则需要查看MERGE,以便将其归结为单个语句。
如果这是针对定期发生的事情,您是否考虑过使用复制等内置工具?