快速统计数据库每个表的记录数

 快速得到数据库每个表的记录数


一般情况下,我们用count(*)来统计记录数。
但当数据量特别大时,这种方法很慢。
下面提供如何借用sysindexes和sysobjects表来得到某个数据库每个表记录数的方法,不过,这种方法好像有些延时(具体没测试)。

 

SQL Server 2000版本的SQL 语句:

SELECT o.NAME,
 i.rowcnt
FROM sysindexes AS i
 INNER JOIN sysobjects AS o ON i.id = o.id
WHERE i.indid < 2
 AND OBJECTPROPERTY(o.id, 'IsMSShipped') = 0
ORDER BY o.NAME

 

SQL Server2005/8版本的SQL 语句:

 

SELECT o.name,
 ddps.row_count
FROM sys.indexes AS i
 INNER JOIN sys.objects AS o ON i.OBJECT_ID = o.OBJECT_ID
 INNER JOIN sys.dm_db_partition_stats AS ddps ON i.OBJECT_ID = ddps.OBJECT_ID
 AND i.index_id = ddps.index_id
WHERE i.index_id < 2
 AND o.is_ms_shipped = 0
ORDER BY o.NAME

你可能感兴趣的:(MSSQL,bigdata)