Home Forums SQL Server 2008 SQL Server 2008 Performance Tuning Logical reads aggregating when particular "if exists" stmt called multiple times inside proc RE: Logical reads aggregating when particular "if exists" stmt called multiple times inside proc

  • You have multiple issues here in my opinion.

    1) You have a table variable in your query. Table variables have no statistics, so the optimizer assumes there is exactly 1 row of data. This often leads to horrible execution plans. Use a temporary table instead.

    2) You're using NOLOCK. Do you understand what that means?

    That said, try to optimize the query inside the EXISTS. If you need assistance with that, please post the actual execution plan and we'll try to help.

    Another thing that I noticed is that sometimes the optimizer creates different plans for SELECT 1 WHERE EXISTS (...query...) compared to IF EXISTS (... query...). Sometimes decoupling the IF and the EXISTS assisgning to a variable produces better plans.

    -- Gianluca Sartori