53

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?

6
  • 2
    Get a query execution plan, then we can talk... (SELECT COUNT directly queries the table rather than using statistics, as statistics may be out of date)
    – Justin
    Commented Sep 18, 2012 at 15:05
  • 2
    Silly question, but do you have an index?
    – Kermit
    Commented Sep 18, 2012 at 15:10
  • @FreshPrinceOfSO it is still slow if you count on Id (bigint, primary key, identity specification=true).
    – ANeves
    Commented Nov 7, 2013 at 16:50
  • @FreshPrinceOfSO would it make any diff? yes, I have a clustered index actually.
    – Adi
    Commented Nov 12, 2013 at 21:44
  • If count is frequently used then we can store it in another table. I was also facing the same problem and I have used IF EXISTS to fix this. Commented Feb 11, 2014 at 15:50

5 Answers 5

88

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.

21
  • Also can be used when no index is present.
    – Kermit
    Commented Sep 18, 2012 at 15:27
  • 2
    @Adi A query against sys.partitions took a long time? I find that very hard to believe. Commented Sep 18, 2012 at 21:02
  • 1
    @Adi no, this is for independent, whole tables. If you have a filtered index that matches your WHERE clause, you could check that index_id specifically. For a JOIN I think the only way to bypass a count would e to have an indexed view... Commented Sep 28, 2012 at 9:49
  • 4
    The query in this answer does appear to work on Azure Sql at this point in time. I'm testing in a P2 db on a table with 200M+ rows and about 5K new rows inserted in bulk each minute. The query in this answer returns near instantly, a naive SELECT COUNT(*) takes about a minute (it uses an index scan). Both return the same number. Commented Jul 8, 2017 at 18:37
  • 1
    @SarojShrestha Updated the answer to address that case. Commented Feb 20, 2019 at 14:58
14

(How large is "large amount of data"? - should have commented this first, but maybe the exec below helps you out already)

If I run a query on a static (means no one else is annoying with read/write/updates in quite a while so contention is not an issue) table with 200 million rows and COUNT(*) in 15 seconds on my dev machine (oracle). Considering the pure amount of data, this is still quite fast (at least to me)

As you said NOLOCK is not an option, you could consider

exec sp_spaceused 'myTable'

as well.

But this pins down nearly to the same as NOLOCK (ignoring contention + delete/update afaik)

8

I've been working with SSMS for well over a decade and only in the past year found out that it can give you this information quickly and easily, thanks to this answer.

  1. Select the "Tables" folder from the database tree (Object Explorer)
  2. Press F7 or select View > Object Explorer Details to open Object Explorer Details view
  3. In this view you can right-click on the column header to select the columns you want to see including table space used, index space used and row count: enter image description here

Note that the support for this in Azure SQL databases seems a bit spotty at best - my guess is that the queries from SSMS are timing out, so it only returns a handful of tables each refresh, however the highlighted one always seems to be returned.

2

Count will do either a table scan or an index scan. So for a high number of rows it will be slow. If you do this operation frequently, the best way is to keep the count record in another table.

If however you do not want to do that, you can create a dummy index (that will not be used by your query's) and query it's number of items, something like:

select 
    row_count
from sys.dm_db_partition_stats as p
inner join sys.indexes as i 
  on p.index_id = i.index_id
  and p.object_id = i.object_id
where   i.name = 'your index'

I am suggesting creating a new index, because this one (if it will not be used) will not get locked during other operations.

As Aaron Bertrand said, maintaining the query might be more costly then using an already existing one. So the choice is yours.

2
  • But even if this index is not used for other read operations, it still has to be maintained for other DML. I think this dummy index is costlier than you think. Commented Sep 18, 2012 at 15:29
  • It might be as you say. It has to be tested. The sql can be used without creating actually a new index, but on an existing index. I used something similar on filtered indexes. I never needed to actually count the table from heads to finish. Commented Sep 18, 2012 at 15:33
-5

If you just need a rough count of number of rows, ie. to make sure a table loaded properly or to make sure the data was not deleted, do the following:

MySQL> connect information_schema;
MySQL> select table_name,table_rows from tables;
1
  • The OP question is not related to MySQL. Commented Mar 25, 2018 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.