{"id":4,"date":"2010-05-21T11:59:37","date_gmt":"2010-05-21T18:59:37","guid":{"rendered":"http:\/\/www.sqldbpros.com\/?p=4"},"modified":"2010-10-11T16:34:08","modified_gmt":"2010-10-11T23:34:08","slug":"sql-server-row-count-the-fastest-way","status":"publish","type":"post","link":"http:\/\/sqldbpros.com\/wordpress\/2010\/05\/sql-server-row-count-the-fastest-way\/","title":{"rendered":"SQL Server Row Count &#8211; The Fastest Way"},"content":{"rendered":"<p>What's the fastest way to get the number of rows in a table in SQL Server 2005 or SQL Server 2008?\u00a0 Performing a SELECT COUNT(*) on the tables will work but could have a serious performance impact if the tables are of any meaningful size.\u00a0 Alternatively, a few lines of code hitting one of the system tables can get you the answer with minimal performance impact.\u00a0 SQL Server consultants use the following script:<!--more--><\/p>\n<blockquote><p>SELECT<br \/>\nOBJECT_SCHEMA_NAME(object_id)<br \/>\n,OBJECT_NAME(object_id)<br \/>\n,SUM(Rows) AS NumOfRows --sum the rows if there are multiple partitions<br \/>\nFROM<br \/>\nsys.partitions<br \/>\nWHERE<br \/>\nindex_id &lt; 2 --ignore the partitions from non-clustered indexes if any<br \/>\nGROUP BY<br \/>\nOBJECT_ID<br \/>\nORDER BY<br \/>\nNumOfRows DESC<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>What's the fastest way to get the number of rows in a table on SQL Server 2005 or SQL Server 2008?  Use the code the pros use.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[4,6,5],"class_list":["post-4","post","type-post","status-publish","format-standard","hentry","category-scripts","tag-performance","tag-script","tag-tips"],"_links":{"self":[{"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/posts\/4","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/comments?post=4"}],"version-history":[{"count":12,"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/posts\/4\/revisions"}],"predecessor-version":[{"id":6,"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/posts\/4\/revisions\/6"}],"wp:attachment":[{"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/media?parent=4"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/categories?post=4"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sqldbpros.com\/wordpress\/wp-json\/wp\/v2\/tags?post=4"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}