I'm not sure it this is a post for PHP or the database forum, but since my question deals with php functions, i put it here.
In the past when i would page results i do the following:
1) get the count of my full resultset
2) calculate limit and offset
3) query db again with limit and offset
4) display results
This works fine, and how it's done.
But, lets say the query to generate the resultset is very costly (multiple joins, etc.) that takes a couple of seconds to finish. While digging through the PHP functions for postgres, i came across the fact that you can pass an int to pg_fetch_row to specifiy the offset in the result resource. The same offset can be accomplished by using pg_result_seek.
So, my question is, which would be better if you have an expensive query?
1) exec 2 queries, 1 to find results count, and 1 to get the resultset based on the first query
or
2) exec 1 query, use pg_num_rows() for page/offset calcuation, and pass an offset value to the pg_fetch_row function to get the rows you want.
Thanks for your feedback.