2017-11-17 1 views
0

Diese Abfrage funktioniert nicht gut. Ich möchte wissen, ob dies in weniger CTEs und/oder ohne UNION möglich ist.SQL - Alternative zu UNION - JOIN möglich?

WITH SourceOutputNodes 
       AS (
       SELECT t.p.value('@OutputNodeID', 'uniqueidentifier') AS OutputNodeID, 
        t.p.value('@OutputNodeTypeID', 'int') AS OutputNodeTypeID, 
        t.p.value('@OutputTemplateID', 'uniqueidentifier') AS OutputTemplateID, 
        t.p.value('@OutputTemplateBookmarkID', 'uniqueidentifier') AS OutputTemplateBookmarkID, 
        t.p.value('@PublicNodeID', 'uniqueidentifier') AS PublicNodeID, 
        t.p.value('@Name', 'nvarchar(100)') AS Name, 
        t.p.value('@Description', 'nvarchar(300)') AS [Description], 
        t.p.value('@CreatedDate', 'datetime') AS CreatedDate, 
        coalesce(t.p.value('@CreatedByUserID', 'uniqueidentifier'), '00000000-0000-0000-0000-000000000000') AS CreatedByUserID, 
        t.p.value('@DeletedDate', 'datetime') AS DeletedDate, 
        t.p.value('@DeletedByUserID', 'uniqueidentifier') AS DeletedByUserID, 
        t.p.value('@ModifiedByUserID', 'uniqueidentifier') AS ModifiedByUserID, 
        t.p.value('@ModifiedDate', 'datetime') AS ModifiedDate, 
        t.p.value('@IsPublic', 'bit') AS IsPublic, 
        t.p.value('@DisplayOrder', 'int') AS DisplayOrder, 
        t.p.value('@RuleID', 'uniqueidentifier') AS RuleID, 
        t.p.value('@RuleDescription', 'nvarchar(max)') AS RuleDescription, 
        t.p.value('@DataItemID', 'uniqueidentifier') AS DataItemID, 
        t.p.value('@DataItemName', 'nvarchar(100)') AS DataItemName, 
        t.p.value('@DataItemOtherOptions', 'nvarchar(max)') AS DataItemOtherOptions, 
        t.p.value('@OutputDocRepeaterOptionID', 'int') AS OutputDocRepeaterOptionID, 
        t.p.value('@FormOptions', 'nvarchar(max)') AS FormOptions, 
        t.p.value('@FormID', 'uniqueidentifier') AS FormID, 
        t.p.value('@DefaultValue', 'nvarchar(max)') AS DefaultValue, 
        t.p.value('@GenericFieldDefinitionInspectionTypeID', 'uniqueidentifier') AS GenericFieldDefinitionInspectionTypeID, 
        t.p.value('@GenericFieldDefinitionRoleID', 'uniqueidentifier') AS GenericFieldDefinitionRoleID, 
        t.p.value('@PhotoGrid_ImagesToShow', 'nvarchar(50)') AS PhotoGrid_ImagesToShow, 
        t.p.value('@PhotoGrid_NumberColumns', 'int') AS PhotoGrid_NumberColumns, 
        t.p.value('@PhotoGrid_NumberRows', 'int') AS PhotoGrid_NumberRows, 
        t.p.value('@Photo_PublicOnly', 'bit') AS Photo_PublicOnly, 
        t.p.value('@Photo_LabelPosition', 'nvarchar(50)') AS Photo_LabelPosition, 
        t.p.value('@Photo_Number', 'int') AS Photo_Number, 
        t.p.value('@LinkedToNodeID', 'uniqueidentifier') AS LinkedToNodeID, 
        t.p.value('@UseNoPhotoPlaceholder', 'bit') AS UseNoPhotoPlaceholder, 
        t.p.value('@NoPhotoPlaceholderURL', 'nvarchar(max)') AS NoPhotoPlaceholderURL, 
        t.p.value('@Photo_HighlightHazards', 'bit') AS Photo_HighlightHazards, 
        t.p.value('@Photo_Level', 'int') AS Photo_Level, 
        t.p.value('@CommCov_Level', 'int') AS CommCov_Level 
       FROM [Promotion].Promotions AS p 
        CROSS APPLY p.OutputsXml.nodes('/Data/Outputs.OutputNodes/Outputs.OutputNodes') t(p) 
       WHERE p.PromotionID = 'xxxxx'),[email protected]), 

    OutputNodesNullBookmarks AS (
    SELECT * FROM SourceOutputNodes WHERE OutputTemplateBookmarkID IS NULL 
), 

    OutputNodesMatchingBookmarks AS (
SELECT o.* 
FROM SourceOutputNodes o 
INNER JOIN [Outputs].[OutputDocTemplateBookmarks] b 
ON o.OutputTemplateBookmarkID = b.[OutputDocTemplateBookmarkID] 
), 

    OutputNodes AS (
    SELECT * 
    FROM 
    OutputNodesNullBookmarks 
    UNION 
    SELECT * 
    FROM 
    OutputNodesMatchingBookmarks 
) 
    SELECT * FROM OutputNodes 
+0

Versuchen Sie, UNION zu UNION ALL zu ändern –

Antwort

0

Ich bin mir nicht sicher, dass erste CTE, aber die zweite, dritte und vierte könnte mit einem OR in Ihrer WHERE-Klausel mit einer korrelierten Unterabfrage kombiniert werden.

WITH SourceOutputNodes 
AS (
    SELECT t.p.value('@OutputNodeID', 'uniqueidentifier') AS OutputNodeID, 
     t.p.value('@OutputNodeTypeID', 'int') AS OutputNodeTypeID, 
     t.p.value('@OutputTemplateID', 'uniqueidentifier') AS OutputTemplateID, 
     t.p.value('@OutputTemplateBookmarkID', 'uniqueidentifier') AS OutputTemplateBookmarkID, 
     t.p.value('@PublicNodeID', 'uniqueidentifier') AS PublicNodeID, 
     t.p.value('@Name', 'nvarchar(100)') AS NAME, 
     t.p.value('@Description', 'nvarchar(300)') AS [Description], 
     t.p.value('@CreatedDate', 'datetime') AS CreatedDate, 
     coalesce(t.p.value('@CreatedByUserID', 'uniqueidentifier'), '00000000-0000-0000-0000-000000000000') AS CreatedByUserID, 
     t.p.value('@DeletedDate', 'datetime') AS DeletedDate, 
     t.p.value('@DeletedByUserID', 'uniqueidentifier') AS DeletedByUserID, 
     t.p.value('@ModifiedByUserID', 'uniqueidentifier') AS ModifiedByUserID, 
     t.p.value('@ModifiedDate', 'datetime') AS ModifiedDate, 
     t.p.value('@IsPublic', 'bit') AS IsPublic, 
     t.p.value('@DisplayOrder', 'int') AS DisplayOrder, 
     t.p.value('@RuleID', 'uniqueidentifier') AS RuleID, 
     t.p.value('@RuleDescription', 'nvarchar(max)') AS RuleDescription, 
     t.p.value('@DataItemID', 'uniqueidentifier') AS DataItemID, 
     t.p.value('@DataItemName', 'nvarchar(100)') AS DataItemName, 
     t.p.value('@DataItemOtherOptions', 'nvarchar(max)') AS DataItemOtherOptions, 
     t.p.value('@OutputDocRepeaterOptionID', 'int') AS OutputDocRepeaterOptionID, 
     t.p.value('@FormOptions', 'nvarchar(max)') AS FormOptions, 
     t.p.value('@FormID', 'uniqueidentifier') AS FormID, 
     t.p.value('@DefaultValue', 'nvarchar(max)') AS DefaultValue, 
     t.p.value('@GenericFieldDefinitionInspectionTypeID', 'uniqueidentifier') AS GenericFieldDefinitionInspectionTypeID, 
     t.p.value('@GenericFieldDefinitionRoleID', 'uniqueidentifier') AS GenericFieldDefinitionRoleID, 
     t.p.value('@PhotoGrid_ImagesToShow', 'nvarchar(50)') AS PhotoGrid_ImagesToShow, 
     t.p.value('@PhotoGrid_NumberColumns', 'int') AS PhotoGrid_NumberColumns, 
     t.p.value('@PhotoGrid_NumberRows', 'int') AS PhotoGrid_NumberRows, 
     t.p.value('@Photo_PublicOnly', 'bit') AS Photo_PublicOnly, 
     t.p.value('@Photo_LabelPosition', 'nvarchar(50)') AS Photo_LabelPosition, 
     t.p.value('@Photo_Number', 'int') AS Photo_Number, 
     t.p.value('@LinkedToNodeID', 'uniqueidentifier') AS LinkedToNodeID, 
     t.p.value('@UseNoPhotoPlaceholder', 'bit') AS UseNoPhotoPlaceholder, 
     t.p.value('@NoPhotoPlaceholderURL', 'nvarchar(max)') AS NoPhotoPlaceholderURL, 
     t.p.value('@Photo_HighlightHazards', 'bit') AS Photo_HighlightHazards, 
     t.p.value('@Photo_Level', 'int') AS Photo_Level, 
     t.p.value('@CommCov_Level', 'int') AS CommCov_Level 
    FROM [Promotion].Promotions AS p 
    CROSS APPLY p.OutputsXml.nodes('/Data/Outputs.OutputNodes/Outputs.OutputNodes') t(p) 
    WHERE p.PromotionID = 'xxxxx' 
    ), [email protected]) 
SELECT * 
FROM SourceOutputNodes o 
WHERE OutputTemplateBookmarkID IS NULL 
    OR EXISTS (SELECT [OutputDocTemplateBookmarkID] FROM [Outputs].[OutputDocTemplateBookmarks] WHERE o.OutputTemplateBookmarkID = OutputDocTemplateBookmarkID) 
+0

Funktioniert gut, gleiche Ergebniszeilenanzahl. Die Leistung war gleich, aber das beinhaltet das Rippen von XML. Vielen Dank. – nerdperson

+0

Ja ... Ich dachte, dass XML-Bit mit dem Kreuz anwenden ist wahrscheinlich die eigentliche Performance Schwein in all dies. Ich bin froh zu hören, dass das Neuschreiben funktioniert hat! – JNevill