MERGE语句中EXCEPT的替代方案?

时间:2020-06-11 11:41:02

标签: sql sql-server

我正在编写一个查询,以将STG表中的数据合并到Data表中,并且我想从STG表中排除数据表中确切存在的所有数据。我写了下面的代码来满足我的需要,但是EXCEPT语句导致对数据表的大量读取。有什么我可以尝试改善性能的选择吗?

    MERGE INTO Table AS Target
USING
( /*EXCEPT STATEMENT REMOVES ANY ROWS WHICH HAVE BEEN MODIFIED, BUT NONE OF THE COLUMNS WHICH CHANGED WERE BROUGHT OVER*/
   SELECT    TimesheetEntryID
            ,UserID
            ,StartDateTime
            ,EndDateTime
            ,TimesheetTaskTypeID
            ,TaskID



    FROM Table_STG
EXCEPT
    SELECT   TimesheetEntryID
            ,UserID
            ,StartDateTime
            ,EndDateTime
            ,TimesheetTaskTypeID
            ,TaskID


    FROM Table

) AS Source

ON Target.TimesheetEntryID = Source.TimesheetEntryID

    WHEN MATCHED



    THEN

        UPDATE SET  Target.TimesheetEntryID = Source.TimesheetEntryID,
                    Target.UserID = Source.UserID,
                    Target.StartDateTime = Source.StartDateTime,
                    Target.EndDateTime = Source.EndDateTime,
                    Target.TimesheetTaskTypeID = Source.TimesheetTaskTypeID,
                    Target.TaskID = Source.TaskID



    WHEN NOT MATCHED BY TARGET THEN

    INSERT (
                 TimesheetEntryID
                ,UserID
                ,StartDateTime
                ,EndDateTime
                ,TimesheetTaskTypeID
                ,TaskID


            )

    VALUES  (   
                 Source.TimesheetEntryID
                ,Source.UserID
                ,Source.StartDateTime
                ,Source.EndDateTime
                ,Source.TimesheetTaskTypeID
                ,Source.TaskID

            );

1 个答案:

答案 0 :(得分:1)

您可以使用not innot exists

不在

SELECT    TimesheetEntryID
        ,UserID
        ,StartDateTime
        ,EndDateTime
        ,TimesheetTaskTypeID
        ,TaskID

FROM Table_STG where (TimesheetEntryID ,UserID ,StartDateTime ,EndDateTime 
,TimesheetTaskTypeID ,TaskID) not in
(SELECT   TimesheetEntryID ,UserID ,StartDateTime ,EndDateTime ,TimesheetTaskTypeID 
,TaskID from table);

不存在

SELECT    TimesheetEntryID
        ,UserID
        ,StartDateTime
        ,EndDateTime
        ,TimesheetTaskTypeID
        ,TaskID

FROM Table_STG STG where (TimesheetEntryID ,UserID ,StartDateTime ,EndDateTime 
,TimesheetTaskTypeID ,TaskID) not exitst
(SELECT 1 from table where TimesheetEntryID = STG.TimesheetEntryID  ,UserID = 
STG.UserID ,StartDateTime = STG.StartDateTime
,EndDateTime = STG.EndDateTime ,TimesheetTaskTypeID = STG.TimesheetTaskTypeID ,TaskID 
= STG.TaskID)