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 3303456
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:57:32+00:00 2026-05-17T20:57:32+00:00

EDIT :- With PK on the Id column following is produced both for EXISTS

  • 0

EDIT :-

With PK on the Id column following is produced both for EXISTS and COUNT(*) > 0

Table 'TableWithHugeData'. Scan count 0, logical reads 3, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

IF OBJECT_ID('TableWithHugeData') IS NOT NULL
BEGIN
DROP TABLE TableWithHugeData
END

CREATE TABLE TableWithHugeData
(
ID UNIQUEIDENTIFIER,
Name varchar(max)
)


DECLARE @Counter INT
SELECT @Counter = 0
WHILE (@Counter < 50000)
BEGIN
INSERT INTO TableWithHugeData
VALUES (NewId(),'Ashish ' + Convert(varchar(5000),@Counter))
SELECT @Counter = @Counter +1
END

EXISTS

DBCC FreeProcCache
DBCC DROPCLEANBUFFERS
SET  STATISTICS IO ON
DECLARE @Id UNIQUEIDENTIFIER
SELECT @Id = '28BD1F4C-7D89-4731-9D2C-21ECB20500F8'
IF EXISTS (SELECT * FROM TableWithHugeData WHERE Id = @Id)
BEGIN
SELECT CONVERT(BIT, 1)
END
ELSE
BEGIN
SELECT CONVERT(BIT, 0)
END

IO statistics for EXISTS :-

Table ‘TableWithHugeData’. Scan count 1, logical reads 6, physical reads 1, read-ahead reads 270, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

COUNT(*) >0

DBCC FreeProcCache
DBCC DROPCLEANBUFFERS
DECLARE @Id UNIQUEIDENTIFIER
SELECT @Id = '28BD1F4C-7D89-4731-9D2C-21ECB20500F8'
IF (SELECT COUNT(*) FROM TableWithHugeData WHERE Id = @Id)>0
BEGIN
SELECT CONVERT(BIT, 1)
END
ELSE
BEGIN
SELECT CONVERT(BIT, 0)
END

*IO Statistics For Count() >0 ** :-

Table ‘TableWithHugeData’. Scan count 1, logical reads 6, physical reads 1, read-ahead reads 270, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

As you see, the IO statistics for EXISTS and COUNT(*)>0 are same. I thought EXISTS would be faster and would perform less reads. Am I missing something here? Would defining a Primary key would make any difference?

  • 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-05-17T20:57:33+00:00Added an answer on May 17, 2026 at 8:57 pm

    As you see, the IO statistics for EXISTS and COUNT(*)>0 are same. I thought EXISTS would be faster and would perform less reads. Am I missing something here?

    SQL Server reads data block-wise.

    If the record you are looking for happens to be in the last block (or not found at all) then the EXISTS would still have to scan all blocks.

    Would defining a Primary key would make any difference?

    Yes.

    With the PRIMARY KEY, EXISTS and COUNT(*) would always be the same (single index seek).

    Without one, EXISTS would stop on first match while COUNT(*) would continue to the end of the table.

    Update:

    Sorry, I was wrong earlier.

    It seems that SQL Server optimizes COUNT(*) > 0 to EXISTS, so yes, the statistics would be the same.

    DBCC FreeProcCache
    CHECKPOINT
    DBCC DROPCLEANBUFFERS
    SET  STATISTICS IO ON
    DECLARE @id UNIQUEIDENTIFIER
    SET @id = '7C65EBB1-1242-4084-8BB4-3CC92CA2BE51'
    IF      (
            SELECT  COUNT(*)
            FROM    tablewithhugedata
            WHERE   id = @id
            ) > 0
    BEGIN
            SELECT CONVERT(BIT, 1)
    END
    ELSE
    BEGIN
            SELECT CONVERT(BIT, 0)
    END
    
    DBCC execution completed. If DBCC printed error messages, contact your system administrator.
    DBCC execution completed. If DBCC printed error messages, contact your system administrator.
    Table 'TableWithHugeData'. Scan count 1, logical reads 1, physical reads 0, read-ahead reads 270, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
    
    (1 row(s) affected)
    

    And a little bit different query:

    DBCC FreeProcCache
    CHECKPOINT
    DBCC DROPCLEANBUFFERS
    SET  STATISTICS IO ON
    DECLARE @id UNIQUEIDENTIFIER
    SET @id = '7C65EBB1-1242-4084-8BB4-3CC92CA2BE51'
    IF EXISTS
            (
            SELECT  1
            FROM    tablewithhugedata
            WHERE   id = @id
            )
    BEGIN
            SELECT CONVERT(BIT, 1)
    END
    ELSE
    BEGIN
            SELECT CONVERT(BIT, 0)
    END
    
    DBCC FreeProcCache
    CHECKPOINT
    DBCC DROPCLEANBUFFERS
    SET  STATISTICS IO ON
    DECLARE @id UNIQUEIDENTIFIER
    SET @id = '7C65EBB1-1242-4084-8BB4-3CC92CA2BE51'
    IF      (
            SELECT  COUNT(*)
            FROM    tablewithhugedata
            WHERE   id = @id
            ) BETWEEN 1 AND 2
    BEGIN
            SELECT CONVERT(BIT, 1)
    END
    ELSE
    BEGIN
            SELECT CONVERT(BIT, 0)
    END
    
    DBCC execution completed. If DBCC printed error messages, contact your system administrator.
    DBCC execution completed. If DBCC printed error messages, contact your system administrator.
    Table 'TableWithHugeData'. Scan count 2, logical reads 266, physical reads 6, read-ahead reads 270, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
    
    (1 row(s) affected)
    

    The second query needs to know exact value of COUNT(*) so it reads all blocks (266 logical reads vs. 1 for EXISTS / COUNT(*) > 0).

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

Sidebar

Related Questions

Edit : Solved, there was a trigger with a loop on the table (read
Consider following MySQL table: CREATE TABLE `log` ( `what` enum('add', 'edit', 'remove') CHARACTER SET
In my scenario I have the following 2 tables: Table 1: tdc_doc_field_def Column 1:
Experts, I have JQGrid with custom template column like Edit. the following screen display
Is there any way to edit column names in a DataGridView?
EDIT: I have added Slug column to address performance issues on specific record selection.
How can I add an Edit column in this GridView dynamically? I have been
I'm trying to select data from a table defined similar to the following :
I want to find items in common from the following_list column in a table
I would like to edit a cell by the row and column indexes so

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.