When writing a query for paging on a web page what is the least expensive method to get a total row count? Is there a way to do this without running a query twice - one for the total and the next for the limit?
Using MySQL
Example: (I want to know if there is a less expensive way)
Get Count
SELECT COUNT(*) FROM table
Get Paging
SELECT mycolumns FROM table LIMIT 100
How can I get the total count without running 2 queries.
You can run the first query with the SQL_CALC_FOUND_ROWS
option, and then run this:
SELECT FOUND_ROWS();
http://dev.mysql.com/doc/refman/5.1/en/information-functions.html#function_found-rows
This will give you an additional column called Count
in each row that contains the total number of rows:
SELECT mycolumns, (select count(*) from table) as Count
FROM table
LIMIT 100
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With