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

  • Home
  • SEARCH
  • 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 8111441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:07:09+00:00 2026-06-06T02:07:09+00:00

I have a SQL table BookChapters with over 20 millions rows. It has a

  • 0

I have a SQL table BookChapters with over 20 millions rows. It has a clustered primary key (bookChapterID) and doesn’t have any other keys or indexes. It takes miliseconds to run the following query

if (select count(*) from BookChapters) = 0
...

However, it takes over 10 minutes when I change it like so

if (select count(*) from BookChapters) = 1
...

or

if (select count(*) from BookChapters) > 1
...

Why is that?
How can I get select count(*) to execute faster?

  • 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-06T02:07:14+00:00Added an answer on June 6, 2026 at 2:07 am

    Mikael Eriksson has a good explanation bellow why the first query is fast:

    SQL server optimize it into:
    if exists(select * from BookChapters). So it goes looking for the presence of one row instead of counting all the rows in the table.

    For the other two queries, SQL Server would use the following rule. To perform a query like SELECT COUNT(*), SQL Server will use the narrowest
    non-clustered index to count the rows. If the table does not have any
    non-clustered index, it will have to scan the table.

    Also, if your table has a clustered index you can get your count even faster using the following query (borrowed from this site Get Row Counts Fast!)

    --SQL Server 2005/2008/2016/2017/2019/2022
    SELECT OBJECT_NAME(i.id) [Table_Name], i.rowcnt [Row_Count]
    FROM sys.sysindexes i WITH (NOLOCK)
    WHERE i.indid in (0,1)
    ORDER BY i.rowcnt desc
    
    --SQL Server 2000
    SELECT OBJECT_NAME(i.id) [Table_Name], i.rows [Row_Count]
    FROM sysindexes i (NOLOCK)
    WHERE i.indid in (0,1)
    ORDER BY i.rows desc
    

    It uses sysindexes system table. More info you can find here SQL Server.

    Here is another link Why is my SELECT COUNT(*) running so slow? with another solution. It shows technique that Microsoft uses to quickly display the number of rows when you right click on the table and select properties.

    select sum (spart.rows)
    from sys.partitions spart
    where spart.object_id = object_id(’YourTable’)
    and spart.index_id < 2
    

    You should find that this returns very quickly no matter how many tables you have.

    If you are using SQL 2000, you can use the sysindexes table to get the number.

    select max(ROWS)
    from sysindexes
    where id = object_id(’YourTable’)
    

    This number may be slightly off depending on how often SQL updates the sysindexes table, but it’s usually correct (or at least close enough).

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

Sidebar

Related Questions

I have a SQL table that has a primary key ( Row_id ) that
I have an SQL table with basically the following structure: PK (int, primary key),
I have 2 sql table [dbo].[Contract] [ContractID] pk,.....other column [dbo].[Installment] //this table has composite
I have SQL table that has a varchar(8) column that occasionally has binary data
I have a sql table which has 7 column and the first six column
I have a sql table images which has columns id , imagePath , caption``count
I have a sql table which has a date column (let say deployDate). Now
I have a sql table which has five columns Count1 float Count2 float Total
I have a SQL table which has a number of fields ID | Value
I have a sql table, it has many coloumns. For Instance; The Table :

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.