1

我有一个存储过程,它采用“调查”对象的集合并将它们切碎到相应的表中。

“Survey”对象有两个子对象,“Erosions”和“Spans”。

这是我可能使用的 XML 示例:

<Surveys>
  <Survey>
    <SurveyID>35</SurveyID>
    <CulvertID>5</CulvertID>
    <PRNo>587180</PRNo>
    <MP>5.243</MP>
    <RefMiles>1.500</RefMiles>
    <PtID>ABCGEFGH</PtID>
    <FWVersion>10</FWVersion>
    <SurveyDate>2012-08-27</SurveyDate>
    <FutureComments>7c36fe43-78cb-436e-81cf-f416aa63c8fc</FutureComments>
    <Erosions>
      <Erosion>
        <ID>160</ID>
        <SurveyID>35</SurveyID>
        <Location>Test Erosion - 8/27/2012 - 1:19:04 PM</Location>
      </Erosion>
      <Erosion>
        <ID>161</ID>
        <SurveyID>35</SurveyID>
        <Location>Test Erosion - 8/27/2012 - 1:19:04 PM</Location>
      </Erosion>
    </Erosions>
    <Spans>
      <Span>
        <ID>88</ID>
        <SurveyID>35</SurveyID>
        <Material>Test Span - 8/27/2012 - 1:19:04 PM</Material>
      </Span>
      <Span>
        <ID>89</ID>
        <SurveyID>35</SurveyID>
        <Material>Test Span - 8/27/2012 - 1:19:04 PM</Material>
      </Span>
    </Spans>
  </Survey>
</Surveys>

我已将存储过程定义上传到以下位置(太大,无法粘贴到此处)。 下载 SQL

当我发送上面提供的 XML 示例时,该过程大约需要 10 秒才能完成,而且只有一个非常小的 XML 字符串。更新/插入包含任何“VARBINARY(MAX)”列中的值的记录可能需要几分钟时间。

我真的是一个 Xquery 新手......不知道我做错了什么,但性能绝对糟糕。有什么建议么?

编辑解决方案:

按照下面的@wBob 建议,我为我的存储过程提出了这个 SQL。我最终重新定义了我桌子上的 PK,使其更加“准确”。它不是生成的 int ID,而是基于实际的数据字段。这允许我使用合并来使用由实际数据值定义的外键插入子对象,而不是我必须检索的生成数字。

4

2 回答 2

3

我已经尝试过改变你的(相当大的)proc。请记住,这是演示原理的示例脚本(即 MERGE with OUTPUT 子句),由您完成和测试它。例如,您需要测试所有列是否正确插入/更新,检查您的数据类型,以及删除 NULLIF 的位置等。我还将添加 TRY/CATCH 错误和事务处理。请注意,我还根据surveyId 进行了合并-不确定这是否正确?

如果我是你,在开始之前我会花一些时间熟悉MERGEOUTPUT子句,但是祝你好运,让我们知道你的进展情况。

我还附上了我的完整测试台的链接。此脚本是安全的,并且可以在您的 tempdb 中完全重新运行。

ALTER PROCEDURE [dbo].[sp_SaveSurvey]
    @XMLobject XML
AS
BEGIN

    SET NOCOUNT ON

    CREATE TABLE #tmp_survey ( surveyId INT PRIMARY KEY, erosions XML, spans XML )

    BEGIN TRAN

------------------------------------------------------------------------------------------------
-- MERGE dbo.CULV_StreamCrossingSurvey START
------------------------------------------------------------------------------------------------

    SET IDENTITY_INSERT dbo.CULV_StreamCrossingSurvey ON

    ;MERGE dbo.CULV_StreamCrossingSurvey AS target
    USING
    (
    SELECT
        CAST(NULLIF(X.survey.value('(SurveyID/text())[1]', 'VARCHAR(200)'), '') AS INT) AS SurveyID,
        CAST(NULLIF(X.survey.value('(CulvertID/text())[1]', 'VARCHAR(200)'), '') AS INT) AS CulvertID,
        NULLIF(X.survey.value('(County/text())[1]', 'VARCHAR(50)'), '') AS County,
        NULLIF(X.survey.value('(RoadName/text())[1]', 'VARCHAR(50)'), '') AS RoadName,
        CAST(NULLIF(X.survey.value('(PRNo/text())[1]', 'VARCHAR(50)'), '') AS INT) AS PRNo,
        CAST(NULLIF(X.survey.value('(MP/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,3)) AS MP,
        CAST(NULLIF(X.survey.value('(RefMiles/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,3)) AS RefMiles,
        NULLIF(X.survey.value('(PtID/text())[1]', 'VARCHAR(8)'), '') AS PtID,
        CAST(NULLIF(X.survey.value('(FWVersion/text())[1]', 'VARCHAR(50)'), '') AS SMALLINT) AS FWVersion,
        CAST(NULLIF(X.survey.value('(Latitude/text())[1]', 'VARCHAR(200)'), '') AS FLOAT) AS Latitude,
        CAST(NULLIF(X.survey.value('(Longitude/text())[1]', 'VARCHAR(200)'), '') AS FLOAT) AS Longitude,
        NULLIF(X.survey.value('(Waterway/text())[1]', 'VARCHAR(50)'), '') AS Waterway,
        NULLIF(X.survey.value('(SiteID/text())[1]', 'VARCHAR(50)'), '') AS SiteID,
        NULLIF(X.survey.value('(Observers/text())[1]', 'VARCHAR(50)'), '') AS Observers,
        CAST(NULLIF(X.survey.value('(SurveyDate/text())[1]', 'VARCHAR(200)'), '') AS DATE) AS SurveyDate,
        NULLIF(X.survey.value('(AdjacentLandowner/text())[1]', 'VARCHAR(MAX)'), '') AS AdjacentLandowner,
        NULLIF(X.survey.value('(LocationComments/text())[1]', 'VARCHAR(MAX)'), '') AS LocationComments,
        CAST(NULLIF(X.survey.value('(CrossingType/text())[1]', 'VARCHAR(50)'), '') AS SMALLINT) AS CrossingType,
        NULLIF(X.survey.value('(StructureShape/text())[1]', 'VARCHAR(50)'), '') AS StructureShape,
        NULLIF(X.survey.value('(InletStructure/text())[1]', 'VARCHAR(50)'), '') AS InletStructure,
        NULLIF(X.survey.value('(OutletStructure/text())[1]', 'VARCHAR(50)'), '') AS OutletStructure,
        NULLIF(X.survey.value('(OutletType/text())[1]', 'VARCHAR(50)'), '') AS OutletType,
        NULLIF(X.survey.value('(SubstrateInStructure/text())[1]', 'VARCHAR(50)'), '') AS SubstrateInStructure,
        NULLIF(X.survey.value('(GeneralCondition/text())[1]', 'VARCHAR(50)'), '') AS GeneralCondition,
        CAST(NULLIF(X.survey.value('(PluggedPercent/text())[1]', 'VARCHAR(MAX)'), '') AS SMALLINT) AS PluggedPercent,
        NULLIF(X.survey.value('(PluggedLocation/text())[1]', 'VARCHAR(50)'), '') AS PluggedLocation,
        CAST(NULLIF(X.survey.value('(CrushedPercent/text())[1]', 'VARCHAR(50)'), '') AS SMALLINT) AS CrushedPercent,
        NULLIF(X.survey.value('(CrushedLocation/text())[1]', 'VARCHAR(50)'), '') AS CrushedLocation,
        CAST(NULLIF(X.survey.value('(IsRustedThrough/text())[1]', 'VARCHAR(10)'), '') AS BIT) AS IsRustedThrough,
        NULLIF(X.survey.value('(StructureInterior/text())[1]', 'VARCHAR(50)'), '') AS StructureInterior,
        CAST(NULLIF(X.survey.value('(StructureWaterDepthInlet/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS StructureWaterDepthInlet,
        CAST(NULLIF(X.survey.value('(StructureWaterDepthOutlet/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS StructureWaterDepthOutlet,
        CAST(NULLIF(X.survey.value('(StructureEmbeddedDepthInlet/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS StructureEmbeddedDepthInlet,
        CAST(NULLIF(X.survey.value('(StructureEmbeddedDepthOutlet/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS StructureEmbeddedDepthOutlet,
        CAST(NULLIF(X.survey.value('(StructureWaterVelocityInlet/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS StructureWaterVelocityInlet,
        CAST(NULLIF(X.survey.value('(StructureWaterVelocityOutlet/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS StructureWaterVelocityOutlet,
        CAST(NULLIF(X.survey.value('(StructureWaterVelocityMeasured/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS StructureWaterVelocityMeasured,
        NULLIF(X.survey.value('(StructureWaterVelocityMeasuredWith/text())[1]', 'VARCHAR(50)'), '') AS StructureWaterVelocityMeasuredWith,
        CAST(NULLIF(X.survey.value('(IsPerched/text())[1]', 'VARCHAR(10)'), '') AS BIT) AS IsPerched,
        CAST(NULLIF(X.survey.value('(PerchHeight/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS PerchHeight,
        NULLIF(X.survey.value('(StreamFlow/text())[1]', 'VARCHAR(50)'), '') AS StreamFlow,
        CAST(NULLIF(X.survey.value('(IsScourPoolPresent/text())[1]', 'VARCHAR(10)'), '') AS BIT) AS IsScourPoolPresent,
        CAST(NULLIF(X.survey.value('(ScourPoolLength/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS ScourPoolLength,
        CAST(NULLIF(X.survey.value('(ScourPoolWidth/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS ScourPoolWidth,
        CAST(NULLIF(X.survey.value('(ScourPoolDepth/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS ScourPoolDepth,
        CAST(NULLIF(X.survey.value('(IsUpstreamPondPresent/text())[1]', 'VARCHAR(10)'), '') AS BIT) AS IsUpstreamPondPresent,
        CAST(NULLIF(X.survey.value('(UpstreamPondLength/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS UpstreamPondLength,
        CAST(NULLIF(X.survey.value('(UpstreamPondWidth/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS UpstreamPondWidth,
        CAST(NULLIF(X.survey.value('(RiffleWaterDepth/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS RiffleWaterDepth,
        CAST(NULLIF(X.survey.value('(RiffleBankfullWidth/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS RiffleBankfullWidth,
        CAST(NULLIF(X.survey.value('(RiffleWettedWidth/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS RiffleWettedWidth,
        CAST(NULLIF(X.survey.value('(RiffleWaterVelocity/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS RiffleWaterVelocity,
        NULLIF(X.survey.value('(RiffleMeasuredWith/text())[1]', 'VARCHAR(50)'), '') AS RiffleMeasuredWith,
        NULLIF(X.survey.value('(RiffleSubstrate/text())[1]', 'VARCHAR(50)'), '') AS RiffleSubstrate,
        NULLIF(X.survey.value('(RoadSurface/text())[1]', 'VARCHAR(50)'), '') AS RoadSurface,
        NULLIF(X.survey.value('(RoadCondition/text())[1]', 'VARCHAR(50)'), '') AS RoadCondition,
        CAST(NULLIF(X.survey.value('(RoadWidth/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS RoadWidth,
        NULLIF(X.survey.value('(LocationOfLowPoint/text())[1]', 'VARCHAR(50)'), '') AS LocationOfLowPoint,
        NULLIF(X.survey.value('(RunOffPath/text())[1]', 'VARCHAR(50)'), '') AS RunOffPath,
        CAST(NULLIF(X.survey.value('(FillDepthUpStream/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS FillDepthUpStream,
        CAST(NULLIF(X.survey.value('(FillDepthDownStream/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS FillDepthDownStream,
        NULLIF(X.survey.value('(SlopeUpStream/text())[1]', 'VARCHAR(50)'), '') AS SlopeUpStream,
        NULLIF(X.survey.value('(SlopeDownStream/text())[1]', 'VARCHAR(50)'), '') AS SlopeDownStream,
        CAST(NULLIF(X.survey.value('(ApproachLengthLeft/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS ApproachLengthLeft,
        CAST(NULLIF(X.survey.value('(ApproachLengthRight/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(6,1)) AS ApproachLengthRight,
        NULLIF(X.survey.value('(ApproachSlopeLeft/text())[1]', 'VARCHAR(50)'), '') AS ApproachSlopeLeft,
        NULLIF(X.survey.value('(ApproachSlopeRight/text())[1]', 'VARCHAR(50)'), '') AS ApproachSlopeRight,
        NULLIF(X.survey.value('(VegetationDitchLeft/text())[1]', 'VARCHAR(50)'), '') AS VegetationDitchLeft,
        NULLIF(X.survey.value('(VegetationDitchRight/text())[1]', 'VARCHAR(50)'), '') AS VegetationDitchRight,
        CAST(NULLIF(X.survey.value('(IsErosionPresent/text())[1]', 'VARCHAR(10)'), '') AS BIT) AS IsErosionPresent,
        CAST(NULLIF(X.survey.value('(IsErosionCorrectable/text())[1]', 'VARCHAR(10)'), '') AS BIT) AS IsErosionCorrectable,
        NULLIF(X.survey.value('(ErosionExtent/text())[1]', 'VARCHAR(50)'), '') AS ErosionExtent,
        NULLIF(X.survey.value('(ErosionNotes/text())[1]', 'VARCHAR(MAX)'), '') AS ErosionNotes,
        CAST(NULLIF(X.survey.value('(IsPrioritySite/text())[1]', 'VARCHAR(10)'), '') AS BIT) AS IsPrioritySite,
        NULLIF(X.survey.value('(PriorityReason/text())[1]', 'VARCHAR(50)'), '') AS PriorityReason,
        NULLIF(X.survey.value('(PriorityComments/text())[1]', 'VARCHAR(MAX)'), '') AS PriorityComments,
        CAST(NULLIF(X.survey.value('(FutureVisit/text())[1]', 'VARCHAR(10)'), '') AS BIT) AS FutureVisit,
        NULLIF(X.survey.value('(FutureComments/text())[1]', 'VARCHAR(MAX)'), '') AS FutureComments,
        CAST(NULLIF(X.survey.value('(NonNativeInvasiveSpecies/text())[1]', 'VARCHAR(10)'), '') AS BIT) AS NonNativeInvasiveSpecies,
        NULLIF(X.survey.value('(SpeciesObserved/text())[1]', 'VARCHAR(MAX)'), '') AS SpeciesObserved,
        CAST(NULLIF(X.survey.value('(IsHeadChanged/text())[1]', 'VARCHAR(10)'), '') AS BIT) AS IsHeadChanged,
        NULLIF(X.survey.value('(HeadChangeComments/text())[1]', 'VARCHAR(MAX)'), '') AS HeadChangeComments,
        CAST(NULLIF(X.survey.value('(IsBackwatered/text())[1]', 'VARCHAR(10)'), '') AS BIT) AS IsBackwatered,
        NULLIF(X.survey.value('(BackwaterComments/text())[1]', 'VARCHAR(MAX)'), '') AS BackwaterComments,
        CAST(NULLIF(X.survey.value('(IsOvertopping/text())[1]', 'VARCHAR(10)'), '') AS BIT) AS IsOvertopping,
        NULLIF(X.survey.value('(OvertoppingComments/text())[1]', 'VARCHAR(MAX)'), '') AS OvertoppingComments,
        CAST(NULLIF(X.survey.value('(IsSubstrateEntireLength/text())[1]', 'VARCHAR(10)'), '') AS BIT) AS IsSubstrateEntireLength,
        CAST(NULLIF(X.survey.value('(CalculatedPassability/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(4,1)) AS CalculatedPassability,
        CAST(NULLIF(X.survey.value('(DefinedPassability/text())[1]', 'VARCHAR(50)'), '') AS NUMERIC(4,1)) AS DefinedPassability,
        NULLIF(X.survey.value('(PassabilityComments/text())[1]', 'VARCHAR(MAX)'), '') AS PassabilityComments,
        NULLIF(X.survey.value('(PhotoInlet/text())[1]', 'VARBINARY(MAX)'), '') AS PhotoInlet,
        NULLIF(X.survey.value('(PhotoInletFileName/text())[1]', 'VARCHAR(200)'), '') AS PhotoInletFileName,
        NULLIF(X.survey.value('(PhotoOutlet/text())[1]', 'VARBINARY(MAX)'), '') AS PhotoOutlet,
        NULLIF(X.survey.value('(PhotoOutletFileName/text())[1]', 'VARCHAR(200)'), '') AS PhotoOutletFileName,
        NULLIF(X.survey.value('(PhotoUpstream/text())[1]', 'VARBINARY(MAX)'), '') AS PhotoUpstream,
        NULLIF(X.survey.value('(PhotoUpstreamFileName/text())[1]', 'VARCHAR(200)'), '') AS PhotoUpstreamFileName,
        NULLIF(X.survey.value('(PhotoDownstream/text())[1]', 'VARBINARY(MAX)'), '') AS PhotoDownstream,
        NULLIF(X.survey.value('(PhotoDownstreamFileName/text())[1]', 'VARCHAR(200)'), '') AS PhotoDownstreamFileName,
        NULLIF(X.survey.value('(PhotoRoadApproachLeft/text())[1]', 'VARBINARY(MAX)'), '') AS PhotoRoadApproachLeft,
        NULLIF(X.survey.value('(PhotoRoadApproachLeftFileName/text())[1]', 'VARCHAR(200)'), '') AS PhotoRoadApproachLeftFileName,
        NULLIF(X.survey.value('(PhotoRoadApproachRight/text())[1]', 'VARBINARY(MAX)'), '') AS PhotoRoadApproachRight,
        NULLIF(X.survey.value('(PhotoRoadApproachRightFileName/text())[1]', 'VARCHAR(200)'), '') AS PhotoRoadApproachRightFileName,
        Erosions = (CASE WHEN CAST(X.survey.query('Erosions') AS VARCHAR(MAX)) = '' THEN NULL
        ELSE X.survey.query('Erosions') END),
        Spans = (CASE WHEN CAST(X.survey.query('Spans') AS VARCHAR(MAX)) = '' THEN NULL
        ELSE X.survey.query('Spans') END)
    FROM @XMLobject.nodes('Surveys/Survey') AS X(survey)
    ) AS source ON source.SurveyId = target.SurveyId

    WHEN NOT MATCHED BY TARGET 
        THEN 
        INSERT ( surveyId, CulvertID, County, RoadName, PRNo, MP, RefMiles, PtID, FWVersion, Latitude, Longitude, Waterway, SiteID, Observers, SurveyDate, AdjacentLandowner, LocationComments, CrossingType, StructureShape, InletStructure, OutletStructure, OutletType, SubstrateInStructure, GeneralCondition, PluggedPercent, PluggedLocation, CrushedPercent, CrushedLocation, IsRustedThrough, StructureInterior, StructureWaterDepthInlet, StructureWaterDepthOutlet, StructureEmbeddedDepthInlet, StructureEmbeddedDepthOutlet, StructureWaterVelocityInlet, StructureWaterVelocityOutlet, StructureWaterVelocityMeasured, StructureWaterVelocityMeasuredWith, IsPerched, PerchHeight, StreamFlow, IsScourPoolPresent, ScourPoolLength, ScourPoolWidth, ScourPoolDepth, IsUpstreamPondPresent, UpstreamPondLength, UpstreamPondWidth, RiffleWaterDepth, RiffleBankfullWidth, RiffleWettedWidth, RiffleWaterVelocity, RiffleMeasuredWith, RiffleSubstrate, RoadSurface, RoadCondition, RoadWidth, LocationOfLowPoint, RunOffPath, FillDepthUpStream, FillDepthDownStream, SlopeUpStream, SlopeDownStream, ApproachLengthLeft, ApproachLengthRight, ApproachSlopeLeft, ApproachSlopeRight, VegetationDitchLeft, VegetationDitchRight, IsErosionPresent, IsErosionCorrectable, ErosionExtent, ErosionNotes, IsPrioritySite, PriorityReason, PriorityComments, FutureVisit, FutureComments, NonNativeInvasiveSpecies, SpeciesObserved, IsHeadChanged, HeadChangeComments, IsBackwatered, BackwaterComments, IsOvertopping, OvertoppingComments, IsSubstrateEntireLength, CalculatedPassability, DefinedPassability, PassabilityComments, PhotoInlet, PhotoInletFileName, PhotoOutlet, PhotoOutletFileName, PhotoUpstream, PhotoUpstreamFileName, PhotoDownstream, PhotoDownstreamFileName, PhotoRoadApproachLeft, PhotoRoadApproachLeftFileName, PhotoRoadApproachRight, PhotoRoadApproachRightFileName )
        VALUES ( surveyId, CulvertID, County, RoadName, PRNo, MP, RefMiles, PtID, FWVersion, Latitude, Longitude, Waterway, SiteID, Observers, SurveyDate, AdjacentLandowner, LocationComments, CrossingType, StructureShape, InletStructure, OutletStructure, OutletType, SubstrateInStructure, GeneralCondition, PluggedPercent, PluggedLocation, CrushedPercent, CrushedLocation, IsRustedThrough, StructureInterior, StructureWaterDepthInlet, StructureWaterDepthOutlet, StructureEmbeddedDepthInlet, StructureEmbeddedDepthOutlet, StructureWaterVelocityInlet, StructureWaterVelocityOutlet, StructureWaterVelocityMeasured, StructureWaterVelocityMeasuredWith, IsPerched, PerchHeight, StreamFlow, IsScourPoolPresent, ScourPoolLength, ScourPoolWidth, ScourPoolDepth, IsUpstreamPondPresent, UpstreamPondLength, UpstreamPondWidth, RiffleWaterDepth, RiffleBankfullWidth, RiffleWettedWidth, RiffleWaterVelocity, RiffleMeasuredWith, RiffleSubstrate, RoadSurface, RoadCondition, RoadWidth, LocationOfLowPoint, RunOffPath, FillDepthUpStream, FillDepthDownStream, SlopeUpStream, SlopeDownStream, ApproachLengthLeft, ApproachLengthRight, ApproachSlopeLeft, ApproachSlopeRight, VegetationDitchLeft, VegetationDitchRight, IsErosionPresent, IsErosionCorrectable, ErosionExtent, ErosionNotes, IsPrioritySite, PriorityReason, PriorityComments, FutureVisit, FutureComments, NonNativeInvasiveSpecies, SpeciesObserved, IsHeadChanged, HeadChangeComments, IsBackwatered, BackwaterComments, IsOvertopping, OvertoppingComments, IsSubstrateEntireLength, CalculatedPassability, DefinedPassability, PassabilityComments, PhotoInlet, PhotoInletFileName, PhotoOutlet, PhotoOutletFileName, PhotoUpstream, PhotoUpstreamFileName, PhotoDownstream, PhotoDownstreamFileName, PhotoRoadApproachLeft, PhotoRoadApproachLeftFileName, PhotoRoadApproachRight, PhotoRoadApproachRightFileName )

    WHEN MATCHED
        THEN UPDATE
            SET target.CulvertID = source.CulvertID

    -- DELETE section
    --WHEN NOT MATCHED BY SOURCE 
    --  THEN DELETE

    OUTPUT source.surveyId, source.Erosions, source.Spans INTO #tmp_survey
    --OUTPUT $action
    ;


-- MERGE dbo.CULV_StreamCrossingSurvey END
------------------------------------------------------------------------------------------------



------------------------------------------------------------------------------------------------
-- DELETEs START
-- Remove existing erosion and span records
------------------------------------------------------------------------------------------------

DELETE e
FROM dbo.CULV_StreamCrossingErosion e
    INNER JOIN #tmp_survey s ON e.SurveyID = s.surveyId

DELETE e
FROM dbo.Culvert_StreamCrossingSpan e
    INNER JOIN #tmp_survey s ON e.SurveyID = s.surveyId

-- DELETEs END
------------------------------------------------------------------------------------------------



------------------------------------------------------------------------------------------------
-- MERGE dbo.CULV_StreamCrossingErosion START
------------------------------------------------------------------------------------------------

;MERGE dbo.CULV_StreamCrossingErosion AS target
USING
(
    SELECT
          t.surveyId
        , NULLIF(E.erosion.value('(Location/text())[1]', 'VARCHAR(50)'), '') AS Location
        , E.erosion.value('(Length/text())[1]', 'NUMERIC(6,1)') AS Length
        , E.erosion.value('(Width/text())[1]', 'NUMERIC(6,1)') AS Width
        , E.erosion.value('(Depth/text())[1]', 'NUMERIC(6,1)') AS Depth
        , E.erosion.value('(IsReachingStream/text())[1]', 'BIT') AS IsReachingStream
        , NULLIF(E.erosion.value('(MaterialEroded/text())[1]', 'VARCHAR(50)'), '') AS MaterialEroded
    FROM #tmp_survey t
        CROSS APPLY erosions.nodes('Erosions/Erosion') AS E(erosion)
) AS source ON source.SurveyId = target.SurveyId

WHEN NOT MATCHED BY TARGET 
    THEN 
    INSERT ( surveyId, Location, Length, Width, Depth, IsReachingStream, MaterialEroded )
    VALUES ( surveyId, Location, Length, Width, Depth, IsReachingStream, MaterialEroded )

WHEN MATCHED
    THEN UPDATE
        SET 
        target.Location = source.Location,
        target.Length = source.Length,
        target.Width = source.Width,
        target.Depth = source.Depth,
        target.IsReachingStream = source.IsReachingStream,
        target.MaterialEroded = source.MaterialEroded
;

;MERGE dbo.Culvert_StreamCrossingSpan AS target
USING
(
    SELECT
        t.surveyId,
        S.span.value('(SpanNumber/text())[1]', 'SMALLINT') AS SpanNumber,
        S.span.value('(Length/text())[1]', 'NUMERIC(6,1)') AS Length,
        S.span.value('(Width/text())[1]', 'NUMERIC(6,1)') AS Width,
        S.span.value('(Height/text())[1]', 'NUMERIC(6,1)') AS Height,
        NULLIF( S.span.value('(Material/text())[1]', 'VARCHAR(50)'), '') AS Material
    FROM #tmp_survey t
        CROSS APPLY spans.nodes('Spans/Span') AS S(span)

) AS source ON source.SurveyId = target.SurveyId

WHEN NOT MATCHED BY TARGET 
    THEN 
    INSERT ( surveyID, SpanNumber, Length, Width, Height, Material )
    VALUES ( surveyID, SpanNumber, Length, Width, Height, Material )

WHEN MATCHED
    THEN UPDATE
        SET 
        target.SpanNumber = source.SpanNumber,
        target.Length = source.Length,
        target.Width = source.Width,
        target.Height = source.Height,
        target.Material = source.Material
;

-- MERGE dbo.CULV_StreamCrossingErosion END
------------------------------------------------------------------------------------------------


-- !!TODO Add TRY/CATCH error and transaction handling

COMMIT

RETURN

END
GO
于 2012-09-20T23:30:50.547 回答
1

我可以推荐一些更改。首先,您不需要.query 和.value。.query 返回 XML,所以如果你想要 XML,那么你可以使用 .query。.value 返回一个标量值,这就是您需要在此处使用的值,例如

SELECT
    CAST(NULLIF(X.survey.value('(SurveyID/text())[1]', 'VARCHAR(200)'), '') AS INT) AS SurveyID,
    CAST(NULLIF(X.survey.value('(CulvertID/text())[1]', 'VARCHAR(200)'), '') AS INT) AS CulvertID,

此外,添加 text() 构造函数和 1 个序数(如上),这应该更有效

其次,我认为您不需要游标。.nodes 返回一个结果集,因此您可以直接切入临时表,然后将 INSERT / UPDATE / DELETE 切入主表。如果您使用的是 SQL 2008 及更高版本,则可以使用 MERGE,它将所有操作合二为一。如果你有勇气的话,你甚至可以直接从 .nodes 结果集中 MERGE 到主表中,如果这有意义的话?

如果您没有从这些更改中获得所需的性能,您可以考虑使用 OPENXML,它有时对于较大的 XML 文档可能会更快。请注意,OPENXML 有一个众所周知的特性,它在调用时会自动占用服务器内存的 1/8,因此您必须始终记住调用 sp_xml_removedocument。

尝试进行这些更改,并让我们知道您是如何做到的。

PS 您使用了正确的术语“粉碎”,请忽略有关 SAX 的说明。SQL Server 在其中内置了非常强大且快速的用于分解 XML 的组件。

于 2012-09-19T22:40:08.133 回答