number of rows SQL Server

前端 未结 5 926
挽巷
挽巷 2020-12-31 14:38

How to count or know the number of rows a table has without scaning all the table, maybe using ROW_NUMBER?

5条回答
  •  甜味超标
    2020-12-31 15:06

    There is no ROW_NUMBER in SQL Server, just Oracle. Use:

    SELECT COUNT(primary_key) FROM table
    

    Where primary key the primary key column of your table.

    Since its a primary key, its is already indexed, so SQL can count it without scanning the whole table (it uses a clustered index to be precise, which is much faster than a full table scan)

    You could also use sys.indexes schema, but its not accurate, and you would need database admin priviledges to access, and your application database user is not supposed to have grants in that schema

提交回复
热议问题