简体   繁体   English

SQL表Rowcount与SQL Server中的Select Count不同

[英]SQL Table Rowcount different from Select Count in SQL Server

I am using Microsoft SQL Server. 我正在使用Microsoft SQL Server。

I have a Table which had been updated by 80 rows. 我有一个表已更新80行。

If I right click and look at the table properties the rowcount say 10000 but a select Count(id) from TableName indicates 10080. 如果我右键单击并查看表属性,rowcount表示10000但是TableName中的select Count(id)表示10080。

I checked the statistics and they also have a rowcount of 10080. 我检查了统计数据,他们的行数也是10080。

Why is there a difference between the Rocount in Properties and the Select Count? 为什么Rocount in Properties和Select Count之间存在差异?

Thanks, S 谢谢,S

This information most probably comes from the sysindexes table (see the documentation) and the information in sysindexes isn't guaranteed to be up-to-date. 此信息很可能来自sysindexes表(请参阅文档),并且sysindexes中的信息不保证是最新的。 This is a known fact in SQL Server. 这是SQL Server中的一个已知事实。

Try running DBCC UPDATEUSAGE and check the values again. 尝试运行DBCC UPDATEUSAGE并再次检查值。
Ref: http://msdn.microsoft.com/en-us/library/ms188414.aspx 参考: http//msdn.microsoft.com/en-us/library/ms188414.aspx

DBCC UPDATEUSAGE corrects the rows, used pages, reserved pages, leaf pages and data page counts for each partition in a table or index. DBCC UPDATEUSAGE更正表或索引中每个分区的行,已用页,保留页,叶页和数据页计数。 If there are no inaccuracies in the system tables, DBCC UPDATEUSAGE returns no data. 如果系统表中没有不准确,DBCC UPDATEUSAGE不返回任何数据。 If inaccuracies are found and corrected and WITH NO_INFOMSGS is not used, DBCC UPDATEUSAGE returns the rows and columns being updated in the system tables. 如果找到并更正了不准确性并且未使用NO_INFOMSGS,则DBCC UPDATEUSAGE将返回系统表中正在更新的行和列。

Example: 例:

DBCC UPDATEUSAGE (0)

Update the statistics. 更新统计信息。 That's the only way RDBMS knows current status of your tables and indexes. 这是RDBMS了解表和索引当前状态的唯一方法。 This also helps RDBMS to choose correct execution path for optimal performance. 这也有助于RDBMS选择正确的执行路径以获得最佳性能。

SQL Server 2005 SQL Server 2005

UPDATE STATISTICS dbOwner.yourTableName;

Oracle 神谕

UPDATE STATISTICS yourSchema.yourTableName;

属性信息缓存在SSMS中。

there are a variety of ways to check the size of a table. 有多种方法可以检查表的大小。

http://blogs.msdn.com/b/martijnh/archive/2010/07/15/sql-server-how-to-quickly-retrieve-accurate-row-count-for-table.aspx mentions 4 of various accuracy and speed. http://blogs.msdn.com/b/martijnh/archive/2010/07/15/sql-server-how-to-quickly-retrieve-accurate-row-count-for-table.aspx提到4种不同的准确度和速度。

The ever reliable full table scan is a bit slow .. 可靠的全表扫描有点慢..

SELECT COUNT(*) FROM Transactions

and the quick alternative depends on statistics 而快速的选择取决于统计数据

SELECT CONVERT(bigint, rows)
FROM sysindexes
WHERE id = OBJECT_ID('Transactions')
AND indid < 2

It also mentions that the ssms gui uses the query 它还提到ssms gui使用查询

SELECT CAST(p.rows AS float)
FROM sys.tables AS tbl
INNER JOIN sys.indexes AS idx ON idx.object_id = tbl.object_id and idx.index_id < 2
INNER JOIN sys.partitions AS p ON p.object_id=CAST(tbl.object_id AS int)
AND p.index_id=idx.index_id
WHERE ((tbl.name=N'Transactions'
AND SCHEMA_NAME(tbl.schema_id)='dbo'))

and that a fast, and relatively accurate way to size a table is 并且一种快速且相对准确的表格大小的方法是

SELECT SUM (row_count)
FROM sys.dm_db_partition_stats
WHERE object_id=OBJECT_ID('Transactions')   
AND (index_id=0 or index_id=1);

Unfortunately this last query requires extra permissions beyond basic select. 不幸的是,最后一个查询需要超出基本选择的额

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM