Search code examples
sql-servercounttop-n

Count(*) againt Top 1 in SQL Server


We work with tables with a lot of records. Anyone knows if is better to make a Count(*) or a Top 1 to know if a specific record exists or a group of records match some conditions. Also this type of sentence lock the table or I need to specify no lock the table.


Solution

  • If you don't care about the possibility of dirty reads you can try this

       SELECT TOP(1)
           FROM YourTable WITH (NOLOCK)
        WHERE ..... (your conditions here)