Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9018205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:28:00+00:00 2026-06-16T04:28:00+00:00

Sometimes I need to check if at least one record is present, usually I

  • 0

Sometimes I need to check if at least one record is present, usually I use a:

IF EXISTS (SELECT TOP 1 1 FROM [SomeTable] WHERE [Fields] = [Values]) BEGIN
-- action
END

Is there a fast way to check if more than one record is present? I could do something like:

IF EXISTS (SELECT 1 FROM [SomeTable] 
                        WHERE [Fields] = [Values] 
                                HAVING Count(*) > 1) 
BEGIN
    -- action
END

But I’m not sure if it is the fastest way of doing this as it will test all the records in the set. Is there a faster way?

The ‘where’ part can be quite complex and could consist of multiple ANDs and ORs.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:28:01+00:00Added an answer on June 16, 2026 at 4:28 am

    SQL Server does not generally short circuit aggregate queries. Sometimes it can transform a HAVING COUNT(*) > 0 query to use the same plan as EXISTS (discussed in the comments here) but that’s as far as it goes.

    A HAVING COUNT(*) > 1 query will always count all rows even though in theory it could stop counting after row no 2.

    With that in mind I would use

    IF EXISTS(
      SELECT * FROM (
                     SELECT TOP 2 *
                     FROM [SomeTable] 
                     WHERE [Fields] = [Values] 
    ) T
    HAVING COUNT(*)=2) 
    

    The TOP 2 iterator will stop requesting rows after the second one is returned and thus allow the inner query to shortcircuit early rather than returning them all and counting them.

    Example plans for both versions are below

    Plans

    Regarding the question in the comments about

    “How can you tell which one is best? Is it the query cost?”

    In the particular case shown in the plans above cost would be a reasonable indication as the estimated and actual row counts are quite accurate and the two plans are very similar except for the addition of the TOP iterator. So the additional cost shown in the plan is entirely a representation of the fact that additional number of rows need to be scanned (and possibly read in from disc) and counted.

    It is quite clear cut in this case that this just represents additional work. In other plans it may not be. The addition of the TOP 2 may change the query tree underneath it significantly (e.g. disfavouring plans with blocking iterators)

    In that case the cost shown in execution plans may not a reliable metric. Even in actual execution plans the cost shown is based on estimates so is only as good as those are and even if the estimated row counts are good the costs shown are still just based on certain modelling assumptions.

    SQL Kiwi puts it well in this recent answer on the DBA site

    optimizer cost estimates are mainly only useful for internal server
    purposes. They are not intended to be used to assess potential
    performance, even at a ‘high level’. The model is an abstraction that
    happens to work reasonably well for the internal purposes it was
    designed for. The chances that estimated costs bear any sensible
    resemblance to real execution costs on your hardware and configuration
    is very small indeed.

    Choose other metrics to compare performance, based on whatever real
    issues are important to you.

    logical reads (shown when SET STATISTICS IO ON;) are one such metric that can be looked at but again focusing on this exclusively can be misleading. Testing query duration is probably the only reliable way but even that is not an exact science as performance can vary dependent upon concurrent activity on the server (waits for memory grants, DOP available, number of relevant pages in the cache).

    In the end it just comes down to getting a query plan that looks to be an efficient use of the resources on your server.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I Use MVC and sometimes need to pass the ViewBag.Message from Controller to view,
I sometimes need to run gacutil.exe or installutil.exe, etc. from the command line. Is
Sometimes I need to find some strings inside DB usually it is just host-name
I create functions in Javascript dynamically . Sometimes I need to check if a
We are running an erlang program (ejabberd). Sometimes, we need to do health checks
I sometimes need to look for information for a special portion of code. When
I sometimes need to debug JS in other browsers, and it would be really
In my app I sometimes need to rebuild and repopulate database file. SQLite databse
In the form designer, I sometimes need to see the type of a property,
Sometimes I need to modify a third party library for my specific needs. It

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.