Faster alternative in Oracle to SELECT COUNT(*) FROM sometable

前端 未结 11 536
被撕碎了的回忆
被撕碎了的回忆 2020-11-29 21:57

I\'ve notice that in Oracle, the query

SELECT COUNT(*) FROM sometable;

is very slow for large tables. It seems like the database it actual

11条回答
  •  悲&欢浪女
    2020-11-29 22:44

    Think about it: the database really has to go to every row to do that. In a multi-user environment my COUNT(*) could be different from your COUNT(*). It would be impractical to have a different counter for each and every session so you have literally to count the rows. Most of the time anyway you would have a WHERE clause or a JOIN in your query so your hypothetical counter would be of litte practical value.

    There are ways to speed up things however: if you have an INDEX on a NOT NULL column Oracle will count the rows of the index instead of the table. In a proper relational model all tables have a primary key so the COUNT(*) will use the index of the primary key.

    Bitmap index have entries for NULL rows so a COUNT(*) will use a bitmap index if there is one available.

提交回复
热议问题