1. 程式人生 > >SQL Server 快速獲取表的行數

SQL Server 快速獲取表的行數

統計表的行數,這個大家都知道使用select count(*) from table ,但是隨著表的增長,這個統計的效率越來越低了。

試下下面的語句吧,能快速獲取多少行,還能提供一些其他表資訊,如索引型別,分割槽數等

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

-- 快速獲取表資訊.
SELECT Object_schema_name(p.object_id) AS [Schema],
       Object_name(p.object_id)        AS [Table],
       i.name                          AS [Index],
       p.partition_number,
       p.rows                          AS [Row Count],
       i.type_desc                     AS [Index Type]
FROM   sys.partitions p
       INNER JOIN sys.indexes i
               ON p.object_id = i.object_id
                  AND p.index_id = i.index_id
WHERE  Object_schema_name(p.object_id) <> 'sys'
     --  AND Object_name(p.object_id) = 'table_1' --獲取某個表
ORDER  BY [Schema],
          [Table],
          [Index]