Here is your original request
SELECT l.location_id, l.location_name, t.type_id, t.type_name, i.location_address, i.location_phone FROM location AS l LEFT JOIN location_information AS i ON (l.location_id = i.location_id) LEFT JOIN location_types AS t ON (l.location_type_id = t.type_id) ORDER BY l.location_id DESC LIMIT 10
You are paginating for the last time. If you reorganize this request, you can paginate earlier.
SELECT l.location_id, l.location_name, t.type_id, t.type_name, i.location_address, i.location_phone FROM (SELECT location_id,location_type_id FROM location ORDER BY location_id LIMIT 10) AS k LEFT JOIN location AS l ON (k.location_id = l.location_id) LEFT JOIN location_information AS i ON (k.location_id = i.location_id) LEFT JOIN location_types AS t ON (l.location_type_id = t.type_id) ;
Note. I created a subquery called k . 10 keys receive and order FIRST !!!
JOINs can then continue from there, hoping using only 10 location_ids.
What helps the subquery k is an index that contains location_id and location_type_id
ALTER TABLE location ADD INDEX id_type_ndx (location_id,location_type_id);
Here is what you might like about this approach.
How do you request the following 10 identifiers (ids 11 - 20)? Like this:
SELECT l.location_id, l.location_name, t.type_id, t.type_name, i.location_address, i.location_phone FROM (SELECT location_id,location_type_id FROM location ORDER BY location_id LIMIT 10,10) AS k LEFT JOIN location AS l ON (k.location_id = l.location_id) LEFT JOIN location_information AS i ON (k.location_id = i.location_id) LEFT JOIN location_types AS t ON (l.location_type_id = t.type_id) ;
All you have to do is change the LIMIT in subquery k with each new page.
LIMIT 20,10LIMIT 30,10- etc.
I can improve refactoring by deleting the location table and getting the subquery k, follow these fields:
SELECT k.location_id, k.location_name, t.type_id, t.type_name, i.location_address, i.location_phone FROM (SELECT location_id,location_type_id,location_name FROM location ORDER BY location_id LIMIT 10,10) AS k LEFT JOIN location_information AS i ON (k.location_id = i.location_id) LEFT JOIN location_types AS t ON (k.location_type_id = t.type_id) ;
Creating this additional index for this version is not required.
Give it a try !!!