We usually need to query the total number of rows in a table. Many people prefer to use select count (*) from table. if the table data is very large, such queries are time-consuming and resource-consuming.
In fact, there are two ways to quickly query the table data of SQL Server.
1. sp_spaceused: One of the columns is rows. If the input table object is used, the number of rows in the table will be obtained, which is very fast.
Rows |
Char (11) |
The number of existing rows in the table. If the specified object is a service broker queue, this column indicates the number of messages in the queue. |
2. obtain it from SYS. sysindexes. There is also a columnRowcnt, Counts the total number of inserted, deleted, or updated rows since the last time statistics were updated for the table
Use the following statement:
--- Replace the tablename when you use this script
Select ID, object_name (ID) as tablename, indid, rows, rowcnt
From SYS. sysindexes where id = object_id ('tablename') and indid in (0, 1)
The statistical result may not be too accurate because the system statistics have a time difference.