Home Forums SQL Server 2005 T-SQL (SS2K5) EXISTS Subqueries: SELECT 1 vs. SELECT * (Database Weekly 11.02.2008) RE: EXISTS Subqueries: SELECT 1 vs. SELECT * (Database Weekly 11.02.2008)

  • There's a massive difference between the statements

    Select 1 where exists(select * from dbo.WordIndex where Term='inc')

    and

    select * from dbo.WordIndex where Term='inc'

    Exists is only checking for the existence of a row. It can use whatever indexes are appropriate based on the where clause and return as soon as a row is found. It doesn't need to retrieve any actual values from the table in question

    The select * , however has to retrieve all the columns that match the where clause. It also, if it was using a NC index, has to lookup the rest of the columns in the table from the clustered index.

    All Conor was talking about was the difference between

    IF EXISTS (SELECT 1 FROM SomeTable WHERE SomeCondition)

    and

    IF EXISTS (SELECT * FROM SomeTable WHERE SomeCondition)

    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