How to quickly obtain the number of rows in the SQL Server table

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.