What are 3 ways to get a count of the number of records in a table in sql server?
Loading
What are 3 ways to get a count of the number of records in a table in sql server?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Rajanikant HawaldarPosted Oct 9, 2022, 7:51 AM
1) Count(*) and Count(1) are same but Count(1) is faster so use Count(1)
2) We can use SQL Server catalog views with the following dynamic management views:
sys.tables – populates the list of tables.
sys.indexes – populates the list of indexes of the table.
sys.partitions – populates the rows of each partition.
This approach is faster than the COUNT function. To get the count of rows, run the following script:
3) The sp_spaceused procedure along with the rows count provides the following details:
Name – the Table Name
Rows – the Count of the rows in a table.
Reserved – the total reserved space for a table.
Data – the total space used by the table.
Index_size – the total space used by the index.
Unused – the total reserved space for a table that is not used.
Sachin SinghPosted Oct 9, 2022, 7:38 AM
Please check the execution plan for each then select the good one.