日期:2014-05-19  浏览次数:21908 次

这种方式是不是统计一个表的行数的最快的方法
假如有一个表叫orders,有10000000行数据,用下面的方法是不是最快的?
select rowcnt from sys.sysindexes where id=object_id('orders') and indid=1
因为在 sys.sysindexes 系统表中,有一个列rowcnt,是专门用来存放表的行数,不知这样做是不是对的?

------解决方案--------------------
SQL code
SELECT [rows]
FROM sysindexes
WHERE (id = OBJECT_ID('GENERALDATA')) AND (indid < 2)//速度查询行数

------解决方案--------------------
SQL code
--获取库中所有表及该表的记录数

declare @sql varchar(8000)
set @sql='select * from ('
select @sql=@sql+' select name = ''' + name + ''' , count(*) as num from ['+name+'] union all ' from sysobjects where xtype='u'
set @sql = left(@sql,len(@sql) - 10) + ')a'
exec(@sql)

/*
--sql server 2000 自带库 pubs 的结果
name        num         
----------- ----------- 
titleauthor 25
stores      6
sales       21
roysched    86
discounts   3
jobs        14
pub_info    8
employee    43
authors     23
publishers  8
titles      18
*/


--计算一个库里各个表的记录总数:
select b.name,a.rowcnt from sysindexes a,sysobjects b 
where a.id=b.id and a.indid<2 and b.xtype='u'

--统计数据库里每个表的详细情况
EXEC sp_MSforeachtable @command1="sp_spaceused '?'"

--获得每个表的记录数和容量:
EXEC sp_MSforeachtable @command1="print '?'",
     @command2="sp_spaceused '?'",
     @command3= "SELECT count(*) FROM ? "

------解决方案--------------------
SQL code--获取库中所有表及该表的记录数

declare @sql varchar(8000)
set @sql='select * from ('
select @sql=@sql+' select name = ''' + name + ''' , count(*) as num from ['+name+'] union all ' from sysobjects where xtype='u'
set @sql = left(@sql,len(@sql) - 10) + ')a'
exec(@sql)

/*
--sql server 2000 自带库 pubs 的结果
name num
----------- ----------- 
titleauthor 25
stores 6
sales 21
roysched 86
discounts 3
jobs 14
pub_info 8
employee 43
authors 23
publishers 8
titles 18
*/


--计算一个库里各个表的记录总数:
select b.name,a.rowcnt from sysindexes a,sysobjects b 
where a.id=b.id and a.indid<2 and b.xtype='u'

--统计数据库里每个表的详细情况
EXEC sp_MSforeachtable @command1="sp_spaceused '?'"

--获得每个表的记录数和容量:
EXEC sp_MSforeachtable @command1="print '?'",
@command2="sp_spaceused '?'",
@command3= "SELECT count(*) FROM ? "
 
 
 
对我有用[0] 丢个板砖[0] 引用 举报 管理 TOP

------解决方案--------------------
“假如有一个表叫orders,有10000000行数据,用下面的方法是不是最快的?
select rowcnt from sys.sysindexes where id=object_id('orders') and indid=1
因为在 sys.sysindexes 系统表中,有一个列rowcnt,是专门用来存放表的行数,不知这样做是不是对的?


通过这个统计结果可能不是太准确,因为系统统计信息有个时间差,

如果需要非常准确的话,建议还是直接在该表中count一下,


可以上面提到到这个微软未公开的函数试试;
EXEC sp_MSforeachtable 
@command1="print '?'",
@command2="sp_spaceused '?'",
@command3= "SELECT count(*) FROM ? "
------解决方案--------------------
最好不要用sysindexes 。下面的msdn的提示
重要提示: 
将此 SQL Server 2000 系统表作为一个视图包含进来是为了保持向后兼容性。建议您改用当前的 SQL Server 系统视图。若要查找一个或多个等效系统视图,请参阅将 SQL Server 2000 系统表映射到 SQL Server 2005 系统视图。后续版本的 Microsoft SQL Server 将删除该功能。请避免在新的开发工作中使用该功能,并着手修改当前还在使用该功能的应用程序。

可以用
select rows from sys.partitions where object_id = object_id('Order') and index_id = 1
------解决方案--------------------
SQL code

set statistics time on
select COUNT(*) from test
--7220472
--SQL Server 分析和编译时间: 
--   CPU 时间 = 0 毫秒,占用时间 = 1 毫秒。

--(1 行受影响)

--SQL Server 执行时间:
-- CPU 时间 = 985 毫秒,占用时间 = 2080 毫秒。

select rowcnt from sys.sysindexes where id=object_id('test') and indid=0

--SQL Server 分析和编译时间: 
--   CP