The system view sys.partitions
has a column "rows" that is the total number of rows in a given partition. For a table that is not partitioned (or only has one partition depending on how you look at it) this column gives the number of rows in the table.
I'm curious how accurate this column is and if I can use it instead of a SELECT COUNT(1) FROM TableName
. I've done some experiments where create a table and add a few thousand rows, delete a few hundred, add a few thousand more etc and the count has always been dead on. However I have one table with about 700 mil rows and several indexes. The row in sys.partitions
for the clustered index is again dead on, however the other indexes show some slight variations (+-20k).
Does anyone know how this row is calculated and if it is as accurate as it appears?
Best Answer
Books Online states that the rows field "indicates the approximate number of rows in this partition." I would therefore expect it to be close, but not 100% accurate, 100% of the time.
Michael Zilberstein reports an example of
sys.partitions
being wildly incorrect in For want of a nail. Not saying it is a common occurrence, but it is possible.sys.dm_db_index_physical_stats
contains arecord_count
field that appears to be more accurate, although be aware running the DMV may result in a REDO blocking issue if you run it on an instance hosting an AlwaysOn Readable Secondary Replica.The explanation for the
record_count
field shows the following info:See also Martin Smith's answer to a similar question on Stack Overflow.