216

As the title suggests... I'm trying to figure out the fastest way with the least overhead to determine if a record exists in a table or not.

Sample query:

SELECT COUNT(*) FROM products WHERE products.id = ?;

    vs

SELECT COUNT(products.id) FROM products WHERE products.id = ?;

    vs

SELECT products.id FROM products WHERE products.id = ?;

Say the ? is swapped with 'TB100'... both the first and second queries will return the exact same result (say... 1 for this conversation). The last query will return 'TB100' as expected, or nothing if the id is not present in the table.

The purpose is to figure out if the id is in the table or not. If not, the program will next insert the record, if it is, the program will skip it or perform an UPDATE query based on other program logic outside the scope of this question.

Which is faster and has less overhead? (This will be repeated tens of thousands of times per program run, and will be run many times a day).

(Running this query against M$ SQL Server from Java via the M$ provided JDBC driver)

7
  • 1
    This might be database dependent. For example, counting on Postgres is rather slow. Commented Aug 7, 2013 at 21:54
  • Sorry, this is Java talking to M$ SQL via jdbc driver. I'll update my OP.
    – SnakeDoc
    Commented Aug 7, 2013 at 21:54
  • 2
    There is exists also. Commented Aug 7, 2013 at 21:58
  • @Nikola Markovinović: how would you use it in this case?
    – zerkms
    Commented Aug 7, 2013 at 21:59
  • 6
    @zerkms Depends on context. If in stored procedure it would be if exists(select null from products where id = @id); if in a query called directly by a client select case when exists (...) then 1 else 0 end. Commented Aug 7, 2013 at 22:05

17 Answers 17

275

EXISTS (or NOT EXISTS) is specially designed for checking if something exists and therefore should be (and is) the best option. It will halt on the first row that matches so it does not require a TOP clause and it does not actually select any data so there is no overhead in size of columns. You can safely use SELECT * here - no different than SELECT 1, SELECT NULL or SELECT AnyColumn... (you can even use an invalid expression like SELECT 1/0 and it will not break).

IF EXISTS (SELECT * FROM Products WHERE id = ?)
BEGIN
--do what you need if exists
END
ELSE
BEGIN
--do what needs to be done if not
END
11
  • 9
    @SnakeDoc No. Exists works with select in such a fashion that it exits as soon as one row is found. Furthermore exists merely notes the existence of record, not actual values in the record, saving the need to load the row from disk (assuming search criteria is indexed, of course). As for overhead of if - you will have to spend this minuscule time anyway. Commented Aug 8, 2013 at 9:14
  • 1
    @NikolaMarkovinović interesting point. I'm not sure if an Index exists on this field, and my newbish SQL doesn't know how to find out. I am working with this DB from Java via JDBC and the database is remotely located in a colo somewhere. I've only been provided a "database summary" which just details which fields exist in each table, their type, and any FK or PK's. Does this change anything?
    – SnakeDoc
    Commented Aug 8, 2013 at 14:51
  • 4
    @SnakeDoc To find out about table structure, including foreign keys and indexes, run sp_help table_name. Indexes are essential when it comes to retrieving a few rows out of many, wherther using select top or exists; if they are not present sql engine will have to perform table scan. This is the least desirable table search option. If you are not authorized to create indexes you will have to communicate to technical staff on the other side to find out whether they adjust them automatically or they expect you to suggest indexes. Commented Aug 8, 2013 at 23:34
  • 1
    Is it possible to directly return the result of EXISTS? Like select exists() ;
    – Konstantin
    Commented Feb 28, 2018 at 17:09
  • 6
    @Konstantin You can do something like SELECT CASE WHEN EXISTS(..) THEN 1 ELSE 0 END; Commented Feb 28, 2018 at 17:28
214

SELECT TOP 1 products.id FROM products WHERE products.id = ?; will outperform all of your suggestions as it will terminate execution after it finds the first record.

9
  • 6
    Doesn't optimizer take it into account itself when searches through PK (or any other unique key)?
    – zerkms
    Commented Aug 7, 2013 at 21:56
  • 5
    He nver stated that is was the PK, but if so then yes the optimizer would take that into account.
    – Declan_K
    Commented Aug 7, 2013 at 21:58
  • 5
    @Declan_K: seems like my magic sphere failed in this case and a column entitled as id isn't PK. So +1 to your advice.
    – zerkms
    Commented Aug 7, 2013 at 22:00
  • 6
    If it is not the PK, I would also suggest making sure there is an index on that column. Otherwise, the query will have to do a table scan instead of a faster table seek. Commented Aug 7, 2013 at 22:23
  • 6
    I think we should consider @nenad-zivkovic answer over this one. Commented Jul 7, 2015 at 23:06
31

Nothing can beat -

SELECT TOP 1 1 FROM products WHERE id = 'some value';

You don't need to count to know if there is a data in table. And don't use alias when not necessary.

4
  • 5
    In spite of its name id is not primary key. So, even though you are not counting you still need to find all matching records, possibly thousands of them. About aliasing - code is constant work in progress. You never know when you'll have to go back. Aliasing helps preventing stupid runtime errors; for example, unique column name that didn't need an alias is not unique any more because somebody created a column of same name in another, joined table. Commented Aug 7, 2013 at 22:21
  • Yes, you are absolutely right. Aliasing helps a lot but i don't think it makes any difference when not using joins. So, I said don't use it if not necessary. :) And you can find a long discussion here on checking existence. :)
    – AgentSQL
    Commented Aug 7, 2013 at 22:34
  • 3
    I don't know why I acccepted the term aliasing. Correct term is qualifying. Here is longer explanation by Alex Kuznetzov. About single table queries - it is single table now. But later, when bug is discovered and you are trying to hold the flood, client is nervous, you join another table just to face error message - easily correctable message, but not at this sweaty moment, a small stroke strikes - and you correct the error remembering never to leave a column ... Commented Aug 7, 2013 at 22:47
  • 1
    Can't ignore that now. Thanks!! :)
    – AgentSQL
    Commented Aug 7, 2013 at 23:10
18
SELECT CASE WHEN EXISTS (SELECT TOP 1 *
                         FROM dbo.[YourTable] 
                         WHERE [YourColumn] = [YourValue]) 
            THEN CAST (1 AS BIT) 
            ELSE CAST (0 AS BIT) END

This approach returns a boolean for you.

2
  • 1
    Can probably omit the Top statement and the * statement to make it a bit more faster, as Exist will exit once it finds a record, so something like this: SELECT CASE WHEN EXISTS (SELECT 1 FROM dbo.[YourTable] WHERE [YourColumn] = [YourValue]) THEN CAST (1 AS BIT) ELSE CAST (0 AS BIT) END Commented Jul 4, 2017 at 1:58
  • This suggestion fails to mention why this would be faster over the built-in exists / not exists statements within SQL Server. Without any benchmarking i'd be hard-pressed to believe that a case statement would yeild a faster result than an immediate true/false response.
    – Bonez024
    Commented Apr 9, 2019 at 16:10
13

Don't think anyone has mentioned it yet, but if you are sure the data won't change underneath you, you may want to also apply the NoLock hint to ensure it is not blocked when reading.

SELECT CASE WHEN EXISTS (SELECT 1 
                     FROM dbo.[YourTable] WITH (NOLOCK)
                     WHERE [YourColumn] = [YourValue]) 
        THEN CAST (1 AS BIT) 
        ELSE CAST (0 AS BIT) END
11

You can also use

 If EXISTS (SELECT 1 FROM dbo.T1 WHERE T1.Name='Scot')
    BEGIN
         --<Do something>
    END 

ELSE    
     BEGIN
       --<Do something>
     END
6

Below is the simplest and fastest way to determine if a record exists in database or not Good thing is it works in all Relational DB's

SELECT distinct 1 products.id FROM products WHERE products.id = ?;
2
SELECT COUNT(*) FROM products WHERE products.id = ?;

This is the cross relational database solution that works in all databases.

3
  • 9
    However you force the db to loop over all records, very slow on big tables
    – amd
    Commented Aug 16, 2017 at 7:43
  • @amd care to explain why ?
    – UmNyobe
    Commented Aug 27, 2018 at 14:37
  • @amd your comment make total sense. This query is more a FIND ALL than FIND ANY.
    – UmNyobe
    Commented Aug 27, 2018 at 15:56
2

For those stumbling upon this from MySQL or Oracle background - MySQL supports the LIMIT clause to select a limited number of records, while Oracle uses ROWNUM.

1

For MySql you can use LIMIT like below (Example shows in PHP)

  $sql = "SELECT column_name FROM table_name WHERE column_name = 'your_value' LIMIT 1";
  $result = $conn->query($sql);
  if ($result -> num_rows > 0) {
      echo "Value exists" ;
  } else {
      echo "Value not found";
  }
0
create or replace procedure ex(j in number) as
i number;
begin
select id into i from student where id=j;
if i is not null then
dbms_output.put_line('exists');
end if;
exception
   when no_data_found then
        dbms_output.put_line(i||' does not exists');

end;
1
  • 2
    Possibly your code works great, but it would be better if you add some additional information so that is better understandable.
    – idmean
    Commented May 25, 2014 at 14:39
0

I've used this in the past and it doesn't require a full table scan to see if something exists. It's super fast...

UPDATE TableName SET column=value WHERE column=value
IF @@ROWCOUNT=0
BEGIN
     --Do work
END             
0

SQL SERVER 2012+

SELECT IIF((SELECT TOP 1 1 FROM dbo.[YourTable] WHERE [YourColumn] = [YourValue]) IS NULL, 0, 1)
0

May you wanna try my way:

IF (SELECT TOP 1 1 FROM [TableName]) = 1
BEGIN
     --Do work
END 
0
Select count(*) as RecordsExists from 
( SELECT TOP 1 * 
  FROM 
  Your-Table-Name 
  WHERE Column-Name-A='Y' or Column-Name-A ='N'
) 
as x

This Statement will get you the Records Count Based on Inner Statement whether the Records exists or not. It is divided into 2 parts

  1. The Inner Nested Statement will send you the Records whether it exists or not
  2. The Outer Statement will give you the count based on the Records provided by the inner Statement.
  • If the inner statement gives you "No Records" then the Outer Statement will give you "0"
  • If the inner statement gives you "1 Record" then the Outer Statement will give you "1"
0
SELECT IIF(EXISTS(SELECT 1 FROM products WITH (NOLOCK) WHERE products.id = ?), 1, 0);

This is the fastest way because:

  • It uses EXISTS rather than COUNT or SELECT TOP 1. This is the database native way of determining whether a row exists.
  • Uses NOLOCK to avoid contention.

Additionally, this is more ergonomic than a CASE expression.

1
  • Does not work for MySQL Commented Jul 1 at 2:46
0

Returning a simple True/False (1 | 0) in MySQL/MariaDB:

SELECT 
    EXISTS(
        SELECT TRUE FROM <table>
        WHERE <condition>
    )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.