Exists and IF Exists

  • Hi

    Which is better in the below query

    --- This eg is from HELP File

    USE AdventureWorks ;

    GO

    SELECT a.FirstName, a.LastName

    FROM Person.Contact AS a

    WHERE EXISTS

    (SELECT *

    FROM HumanResources.Employee AS b

    WHERE a.ContactId = b.ContactID

    AND a.LastName = 'Johnson');

    GO

    USE AdventureWorks ;

    GO

    SELECT a.FirstName, a.LastName

    FROM Person.Contact AS a

    WHERE EXISTS

    (SELECT 1

    FROM HumanResources.Employee AS b

    WHERE a.ContactId = b.ContactID

    AND a.LastName = 'Johnson');

    GO

    in the same way

    IF EXISTS (SELECT * FROM Tab_Name)

    Begin

    .....

    End

    OR

    IF EXISTS (SELECT 1 FROM Tab_Name)

    Begin

    .....

    End

    Which is better

    selecting all columns or single Column which is better.

    Thanks

    Parthi

    Thanks
    Parthi

  • They're exactly the same. The columns are ignored in an EXISTS.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • I prefer:

    IF EXISTS(SELECT TOP 1 1 FROM ...

  • GilaMonster (1/10/2011)


    They're exactly the same. The columns are ignored in an EXISTS.

    So if there is millions of records are there we can prefer either option ,is it is good???

    Now i am having only 1000+records for testing i need to Implement this on million records to check whether EXISTS condition works perfectly

    Thanks

    Parthi

    Thanks
    Parthi

  • As I said, they're exactly the same. During the parsing of the query any reference to columns is stripped out of the exists.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • nekonecny (1/10/2011)


    I prefer:

    IF EXISTS(SELECT TOP 1 1 FROM ...

    Why?

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Those are exactly same. It does not matter, whether its * or 1 in the case of Exists.

  • I need only one row (top 1), if i use "EXISTS". Is Optimizer so clever to eliminate other potential rows?:-)

  • nekonecny (1/10/2011)


    I need only one row (top 1), if i use "EXISTS". Is Optimizer so clever to eliminate other potential rows?:-)

    Yes, it is.. An EXISTS check completes as soon as a single row is found, it doesn't need to look any further and it knows it. The logic is "short-circuited". In either case as soon as a single row is found matching the criteria it returns TRUE. It seems TOP might actually cost you performance..

    CEWII

  • Elliott Whitlow (1/10/2011)


    nekonecny (1/10/2011)


    I need only one row (top 1), if i use "EXISTS". Is Optimizer so clever to eliminate other potential rows?:-)

    Yes, it is.. An EXISTS check completes as soon as a single row is found, it doesn't need to look any further and it knows it. The logic is "short-circuited". In either case as soon as a single row is found matching the criteria it returns TRUE. It seems TOP might actually cost you performance..

    CEWII

    Thanks! I am smarter now:-)

  • Elliott Whitlow (1/10/2011)


    nekonecny (1/10/2011)


    I need only one row (top 1), if i use "EXISTS". Is Optimizer so clever to eliminate other potential rows?:-)

    Yes, it is.. An EXISTS check completes as soon as a single row is found, it doesn't need to look any further and it knows it. The logic is "short-circuited". In either case as soon as a single row is found matching the criteria it returns TRUE. It seems TOP might actually cost you performance..

    I have a blog post planned on this (use of Top 1 with Exists)

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply