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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:58:09+00:00 2026-06-11T12:58:09+00:00

Counting tables with large amount of data may be very slow, sometimes it takes

  • 0

Counting tables with large amount of data may be very slow, sometimes it takes minutes; it also may generate deadlock on a busy server. I want to display real values, NOLOCK is not an option.

The servers I use is SQL Server 2005 or 2008 Standard or Enterprise – if it matters.
I can imagine that SQL Server maintains the counts for every table and if there is no WHERE clause I could get that number pretty quickly, right?

For example:

SELECT COUNT(*) FROM myTable

should immediately return with the correct value. Do I need to rely on statistics to be updated?

  • 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-11T12:58:10+00:00Added an answer on June 11, 2026 at 12:58 pm

    Very close approximate (ignoring any in-flight transactions) would be:

    SELECT SUM(p.rows) FROM sys.partitions AS p
      INNER JOIN sys.tables AS t
      ON p.[object_id] = t.[object_id]
      INNER JOIN sys.schemas AS s
      ON s.[schema_id] = t.[schema_id]
      WHERE t.name = N'myTable'
      AND s.name = N'dbo'
      AND p.index_id IN (0,1);
    

    This will return much, much quicker than COUNT(*), and if your table is changing quickly enough, it’s not really any less accurate – if your table has changed between when you started your COUNT (and locks were taken) and when it was returned (when locks were released and all the waiting write transactions were now allowed to write to the table), is it that much more valuable? I don’t think so.

    If you have some subset of the table you want to count (say, WHERE some_column IS NULL), you could create a filtered index on that column, and structure the where clause one way or the other, depending on whether it was the exception or the rule (so create the filtered index on the smaller set). So one of these two indexes:

    CREATE INDEX IAmTheException ON dbo.table(some_column)
      WHERE some_column IS NULL;
    
    CREATE INDEX IAmTheRule ON dbo.table(some_column)
      WHERE some_column IS NOT NULL;
    

    Then you could get the count in a similar way using:

    SELECT SUM(p.rows) FROM sys.partitions AS p
      INNER JOIN sys.tables AS t
      ON p.[object_id] = t.[object_id]
      INNER JOIN sys.schemas AS s
      ON s.[schema_id] = t.[schema_id]
      INNER JOIN sys.indexes AS i
      ON p.index_id = i.index_id
      WHERE t.name = N'myTable'
      AND s.name = N'dbo'
      AND i.name = N'IAmTheException' -- or N'IAmTheRule'
      AND p.index_id IN (0,1);
    

    And if you want to know the opposite, you just subtract from the first query above.

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

Sidebar

Related Questions

I have two database tables containing similar data - one is the amount of
I am counting words in a text field and after a certain amount of
I'm using SQLBULKCOPY to copy some data-tables into a database table, however, because the
In our database tables we keep a number of counting columns to help reduce
I have two tables in MYSQL database which contains same Columns by counting and
I'm having a problem counting data w/ inner join. I want to count how
I have two tables of data categories and category relations. This is a setup
In our database tables we keep a number of counting columns to help reduce
I'm counting the right answers field of a table and saving that calculated value
I am writing a counting sort function and when I run it, a window

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.