Dimitri Maziuk said:
VisionSet sez:
Dep. on rs size etc. it may be faster to "select count(*) ..."
first as a separate query.
There is a risk if you do "Select count(*)..." as a separate query before
you get to the "real" query. If there is a lot of insert/update/delete
activity against the table, it is possible that the record count will change
between the time you do the count(*) and the time you actually ask for the
rows in the table. For example, there might 3 million records in your table
when you do the count(*) but there might be hundreds or thousands of
transactions against the table every minute (or second); in the milliseconds
between the time you do count(*) and the time you do your "real" query, a
few rows may be deleted, several rows may be added, and a number of rows may
have values changed that would make the row fail the WHERE conditions where
they had previously met them or vice versa. This could be very significant
for your application if you used the result of count(*) to set up an array
for handling the main result set and the array was too small because more
rows had been added since the count(*).
This problem can often be solved by locking the table to prevent any updates
between the time the count(*) is done and the main result set is obtained.
However, not all database engines necessarily support locking of tables.
Also, locking the table may have negative consequences of their own. If you
lock the table against other users while you count the rows and then process
them, you may make it impossible for other users to do critical reading or
updates of the data that can't wait until you are finished with your
processing.
You need to understand the risks of doing the count(*) separately from the
"real" query and then decide if you can live with them. Often, the
consequences are entirely tolerable but sometimes they are not.