Fastest way to determine if record exists
Quick check with EXISTS
and forget about data retrieval:
Efficient: it stops on track after the first match like a discerning detective and doesn't bother fetching data.
The magic of EXISTS
If you suspect a record's existence, gently knock on the database door with an EXISTS
:
When "SELECT 1" suffices
When using EXISTS
, the SELECT literal doesn't matter. Think it as: "Is mail in the box?" instead of "How much mail in there?"
Beware of "SELECT *"
Be cautious with the "SELECT * FROM..." beast. It's a vortex that consumes memory!
Unwanted attention
Don't attract unwanted rifle scope from optimizer - Select only what you need. You can use "NOLOCK
hint" for a quick look without interrupting transactions, but beware of "ghost reads".
Use NOLOCK
like a ninja
When you're bold enough for a quick glance, slide in with NOLOCK:
Make your operations nearly invisible but beware - others might not see you too.
Checking before doing
Be a "read before you leap" person. Use "IF EXISTS" before any data modification:
This way you won't trigger unwanted write locks.
Was this article helpful?