x

counting the number of rows in a table

What is the fastest way of counting the number of rows in a table? All tables have PrimaryKeys on them.

more ▼

asked Oct 21, 2009 at 03:37 PM in Default

Raj More gravatar image

Raj More
1.7k 80 82 84

(comments are locked)
10|1200 characters needed characters left

5 answers: sort voted first

If you can deal with a rough count on SQL 2000 - you can query sysindexes - something like

SELECT rowcnt FROM sysindexes WHERE id = OBJECT_ID('MyTableName') and indid in (0, 1)

Under SQL 2005+ the equivalent would be

SELECT [record_count] FROM [sys].[dm_db_index_physical_stats](DB_ID(), OBJECT_ID('MyTableName'), NULL, NULL, 'SAMPLED')
WHERE [index_id] IN (0, 1)

The row count returned from 2005+ will be accurate, unless your table has no clustered index (i.e. is a heap).

These methods are orders of magnitude faster than counting all the rows using COUNT().

more ▼

answered Oct 21, 2009 at 04:01 PM

Matt Whitfield gravatar image

Matt Whitfield ♦♦
29.5k 61 65 87

(comments are locked)
10|1200 characters needed characters left

You could also use:

SELECT row_count 
FROM sys.dm_db_partition_stats
WHERE OBJECT_ID = OBJECT_ID('TableName')
    	AND index_id =1

I specified index_id = 1, since you mentioned that all table have a PK, which I inferred to be the clustered index. You could also specify index_id < 2, which would account for a HEAP table

more ▼

answered Oct 21, 2009 at 04:20 PM

Jay Bonk gravatar image

Jay Bonk
1.4k 2

You should use SUM(row_count) to return total rows in the table in case the table has more partitions.
Jul 21, 2010 at 08:22 AM Pavel Pawlowski
(comments are locked)
10|1200 characters needed characters left
EXEC sp_spaceused 'MyTableName'

one of the returned columns is "rows"

Alternatively, of course, you can run:

SELECT COUNT(*) FROM MyTableName

If you need to store the count in a variable, then use this:

SET @MyVar = (SELECT COUNT(*) FROM MyTableName) 
more ▼

answered Oct 21, 2009 at 03:52 PM

Tom Staab gravatar image

Tom Staab
5.8k 6 8 10

(comments are locked)
10|1200 characters needed characters left

I use the dm_db_index_physical_stats as well. it runs nightly as part of maintenance

more ▼

answered Oct 21, 2009 at 04:19 PM

alen teplitsky gravatar image

alen teplitsky
21 2 2 2

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x1951
x991

asked: Oct 21, 2009 at 03:37 PM

Seen: 3010 times

Last Updated: Oct 21, 2009 at 04:12 PM