• HanShi (7/17/2013)


    Could you rewrite the delete statement to:

    DELETE TOP (20) FROM [dbo].[ServerParametersInstance]

    WHERE Expiration < @now

    This will prevent the query to simultaneously read and delete from the same table.

    SQL Server already does this for you. Look at the execution plan, specifically the eager spool.

    Having said that, this is an odd little stored procedure. In this article, MS says "If you have to use TOP to delete rows in a meaningful chronological order, you must use TOP together with ORDER BY in a subselect statement." I think MS forgot to put the ORDER BY into the subselect - in which case, your version will achieve the same result with only one read instead of two.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden