Last Updated: February 25, 2016
·
392
· thestubborndev

Quickly count rows in a large SQL table.

Instead of using this:

SELECT COUNT(*) FROM MyTable

use this (it's quicker):

SELECT
   Total_Rows= SUM(st.row_count)
FROM
   sys.dm_db_partition_stats st
WHERE
    object_name(object_id) = 'MyTable' AND (index_id < 2)