views:

22

answers:

1

i want to implement my own Zend_Paginator_Adapter so i implemented Zend_Paginator_Adapter_Interface (docs). i am now at implementing count(). am i right to say that if my query is

SELECT * FROM Posts LIMIT ... // where limit is for pagination purposes

i need to get the total number of rows in the table (to return in count())?

SELECT COUNT(*) FROM Posts

it seem unavoidable to have 2 queries? i am not too concerned in my current project about performance, but the need for 2 queries, just made me think, so i wanted to just ask here

+3  A: 

am i right to say...to get the total number of rows in the table (to return in count())?
SELECT COUNT(*) FROM Posts

This is certainly one way to get the result you are looking for. Running a second query is not that bad assuming your table/indexes are optimized.

There is an alternative. You can include the statement "SQL_CALC_FOUND_ROWS" in your original query, then your second query would consist of only:

"SELECT FOUND_ROWS();"

See the following link for more info on this functionality: http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows

Of course, this assumes you are using MySQL.

i am not too concerned in my current project about performance, but the need for 2 queries, just made me think...

It is certainly a valid concern especially as the number of records starts to increase. What makes Zend_Paginator awesome is the fact that you can inject a Zend_Cache_Core instance. This will ensure that you will only be running these queries a minimal number of times based on how long your cache lifetime is.

See the following page for more info on the caching features: http://framework.zend.com/manual/en/zend.paginator.advanced.html#zend.paginator.advanced.caching

wilmoore
when i tried `select SQL_CALC_FOUND_ROWS * from posts LIMIT 0, 3;` it returned 3 rows. and total number of rows in the table is 6. but `SELECT FOUND_ROWS()` always returns 1
jiewmeng
update: i found the answer [here](http://www.sitepoint.com/forums/showthread.php?t=340519). it will not work in software like phpmyadmin/workbench as they add stuff to queries
jiewmeng
The problem with doing this through something like phpmyadmin or workbench is that each query you run is a totally new session...If you string the queries together with ";" you will run both in the same session and see the correct result.
wilmoore