SQL Server: The proper and fastest way to check if rows matching a condition exist
You wouldn't believe how many times i've seen this code
IF (SELECT COUNT(*) FROM Table1 WHERE ... ) > 0
It's understandable though. Logically it's the easiest way to write "if there are any rows matching my condition do this".
But it's also wrong. Plainly and simplly wrong!
Why?
Because when you do a count(*) there is no way to get around an index range scan or a full table scan. For a large resultset this will be a huge resource hog.
So how do i do this you might ask yourself?
Very simple: Use EXISTS!
IF EXISTS(SELECT * FROM Table1 WHERE ...)
Exists stops the execution as soon as it reads the first row, compared to Count which goes through the whole resultset matching our condition.
This may seem as such a trivial tip but EXISTS is so underused even by experienced database developers.
And don't forget the NOT EXISTS to check if rows matching the condition don't exist.
SQL Server Product team recognised this problem so in SQL Server 2005 those 2 statements produce same execution plans whenever possible.
However relying on some internal "maybe when it suits me" operation is not good practice in my book.
Legacy Comments
Dewayne Christensen
2007-09-13 |
re: SQL Server: The proper and fastest way to check if rows matching a condition exist >Exists stops the execution as soon as it reads the first row... Not quite. It stops as soon as it finds the first row _that matches_. So you could potentially still be reading the whole table, if there are no matching rows. But it will never be worse than select count(). |
Mladen
2007-09-13 |
re: SQL Server: The proper and fastest way to check if rows matching a condition exist @Dewayne: i was reffering to the resultset, but i see how this could be misinterpreted now. thanx for pointing it out. |
Mick
2007-09-13 |
re: SQL Server: The proper and fastest way to check if rows matching a condition exist This may not be the case anymore, but didn't it used to be that using SELECT 1 FROM instead of SELECT * FROM produced faster results? That way it didn't have to get any data back. |
ML
2007-09-14 |
re: SQL Server: The proper and fastest way to check if rows matching a condition exist Things have changed after SQL 2005. http://milambda.blogspot.com/2006/10/exists-or-not-exists-that-is-question.html And the stuff about "select 1" vs. "select *" vs. "select <whatever>" is just a myth... :) |
Mladen
2007-09-14 |
re: SQL Server: The proper and fastest way to check if rows matching a condition exist @Mick: i think that select 1 vs select * was true in sql server 6.5 but got fixed after 7.0 version. @ML: thanx for the link! |
Steve G.
2007-09-28 |
re: SQL Server: The proper and fastest way to check if rows matching a condition exist Problem is, I've has "NOT EXISTS" return a wrong answer on several occasions, so you can understand that I'd be leary of using it. FYI, this was on SQL Server 2000, running scripts in Query Analyzer. Steve G. |
Mladen
2007-09-28 |
re: SQL Server: The proper and fastest way to check if rows matching a condition exist return wrong answer? can you show an example, because i've never seen that. |
Sameer Alibhai
2007-10-19 |
re: SQL Server: The proper and fastest way to check if rows matching a condition exist You know I wrote an article on the same topic. :) http://www.sharpdeveloper.net/content/archive/2007/08/12/if-exists-instead-of-count-equals-increased-performance.aspx Cheers! |
Alan H
2008-01-30 |
re: SQL Server: The proper and fastest way to check if rows matching a condition exist Does anyone know if EXISTS check within a Stored Proc on a Temporary Table can force a run time SP: Recompile event? Currently encoutering performance issues related to run-time procedure compilation and common factor seems to be EXISTS statement on accessing temporary table. FYI Temporary table is created and populated within the same Stored Proc prior to call to existence check. Any Ideas? |
Kumaresh
2008-08-05 |
re: SQL Server: The proper and fastest way to check if rows matching a condition exist I got a good hint. I found this post very useful. Thanks guys |
Baffled
2009-12-10 |
re: SQL Server: The proper and fastest way to check if rows matching a condition exist So why do I frequently find (in SQL 2000, 2005, or 2008) that SELECT <column list> FROM <table1> INNER JOIN <table2> WHERE <condition> IF @@ROWCOUNT > 0 <Do Something> is orders of magnitude faster than IF EXISTS (SELECT * FROM <table1> INNER JOIN <table2> WHERE <condition>) <Do Something> Both return the same execution plan, but CPU, reads, and writes may be 600 - 3000X greater for the IF EXISTS. |