I've notice that in Oracle, the query
SELECT COUNT(*) FROM sometable;
is very slow for large tables. It seems like the database it actually going through every row and incrementing a counter one at a time. I would think that there would be a counter somewhere in the table how many rows that table has.
So if I want to check the number of rows in a table in Oracle, what is the fastest way to do that?
If you want just a rough estimate, you can extrapolate from a sample:
SELECT COUNT(*) * 100 FROM sometable SAMPLE (1);
For greater speed (but lower accuracy) you can reduce the sample size:
SELECT COUNT(*) * 1000 FROM sometable SAMPLE (0.1);
For even greater speed (but even worse accuracy) you can use block-wise sampling:
SELECT COUNT(*) * 100 FROM sometable SAMPLE BLOCK (1);