I’m guessing the answer is no but in Postgres is there a way to find the number of results returned that is faster than running the entire query? In other words if you have a query that takes a really long time can it be done faster if you only want to know the number of rows returned?
Share
Not sure if it will give you the scale of improvement you’re after, but a
select count()will always be faster than aselect some fieldsquery because the result set doesn’t have to be returned to the client.The server still needs to go through the entire selection process though, so that part of the operation will take exactly the same time. In short, the amount of time you win with a
count()decreases when the query is more complex or when the result set is quite short.I hope this makes sense…
On the other hand, if you’re worried about the execution time of your query, your first stop should always be EXPLAIN (also available in a highly visual form in pgAdminIII, as explained here)