What is an efficient query to count table rows in SQL Server 2008?

What is an efficient query to count table rows in SQL Server 2008

+3
source share
3 answers

For an exact answer, a reasonable way to get the exact number of rows in a table in a normalized database is:

select count(*) from table;

In a table without any indexes, the database will perform a full table scan. If you specify a non-empty column, the database may use a (potentially much smaller) index to resolve your query.

, , . , ( API ..) * .

, , , , , (, )

* : SQL-, , , Oracle.

+5
SELECT COUNT(*) FROM TableName

?

+1
SELECT COUNT(*) FROM table_name

. .

WITH NOLOCK, , .

SELECT sum(rows)
FROM sys.partitions 
where object_id=object_id('foo') and index_id < 2

However, this also will not give you a consistent response to the transaction.

+1
source

Source: https://habr.com/ru/post/1778599/


All Articles