I am searching for records in a table as follows:
SELECT Id, Name FROM my_table WHERE Name LIKE '%prashant%' LIMIT 0, 10;
Now, I am adding LIMIT to maintain my paging. But when user searches for word 'prashant' then total records I have is 124 for 'prashant'. But as the limit applied to the query so it only fetches 10 records in my PHP script and when I am count the mysql variable in PHP code it returns total records found is 10.
So basically I want to count and Limit using a single query, by making some modification in the above query, I want the total count (124) of records. I don't want to run a separate count(*) query for just counting the total result found by the query.
Thanks.
-
SELECT SQL_CALC_FOUND_ROWS Id, Name FROM my_table WHERE Name LIKE '%prashant%' LIMIT 0, 10; # Find total rows SELECT FOUND_ROWS();tpdi : MySQL never fails to amaze in the ad-hocery department.Prashant : "MySQL never fails to amaze in the ad-hocery department." - I am not getting this, please explain...tpdi : It's a not very valuable extension to support paging on web pages, and it's pretty obvious reading the MySQL docs that it wasn't really well thought out before it was added. And it has poor performance. In short, it's a hack. -
MySQL supports doing this using SQL_CALC_FOUND_ROWS, as mentioned by Ionut. However, it turns out that in many cases it's actually faster to do it the old fashioned way using two statements, where one of them is a regular count(). This does however require that the counting can be done using an index scan, which won't be the case for this very query, but I thought I'd mention it anyway.
0 comments:
Post a Comment