Most Efficient (Fast) T-SQL DELETE For Many Rows?

前端 未结 8 1260
后悔当初
后悔当初 2020-12-16 06:31

Our server application receives information about rows to add to the database at a rate of 1000-2000 rows per second, all day long. There are two mutually-exclusive columns

8条回答
  •  旧巷少年郎
    2020-12-16 07:25

    Seems that your table is not indexed on (tag) and (longTag)

    Build two indexes: one on (tag), one on (longTag)

    If you are planning to delete a really big number of records, then declare two table variables, fill them with values and delete like this:

    DECLARE @tag TABLE (id INT);
    DECLARE @longTag TABLE (id VARCHAR(50));
    
    INSERT
    INTO  @tag
    VALUES (`tag1`)
    
    INSERT
    INTO  @tag
    VALUES (`tag2`)
    
    /* ... */
    
    INSERT INTO @longTag
    VALUES ('LongTag1')
    
    /* ... */
    
    
    DELETE
    FROM    MyRecords r
    WHERE   r.tag IN (SELECT * FROM @tag)
            OR r.longTag IN (SELECT * FROM @longTag)
    

    You may also try to perform a two-pass DELETE:

    DELETE
    FROM    MyRecords r
    WHERE   r.tag IN (SELECT * FROM @tag)
    
    DELETE
    FROM    MyRecords r
    WHERE   r.longTag IN (SELECT * FROM @longTag)
    

    and see what statements runs longer, to see if there's an issue with the indexes.

提交回复
热议问题