I have a query in mysql that is very slow, more than 25 seconds.
The base is quite large, something around 1,000 records of real estate (table osrs_properties) and 16,000 records of photos (table osrs_photos).
The query without the photo takes half a second, but with a photo it takes 25, how can I improve the query or table to increase the query speed?
Below the query:
SELECT p.ref,p.pro_name, p.id, p.pro_alias, p.precovenda, f.image as fotoimg, MIN(f.ordering) AS foto, p.city, p.bed_room, p.rooms, p.parking, p.square_feet
FROM osrs_properties p
LEFT JOIN (SELECT * FROM osrs_photos ORDER BY ordering ASC) f ON f.pro_id = p.id
LEFT JOIN (SELECT category_id, pid FROM osrs_property_categories) c ON c.pid = p.id
WHERE p.id > 0
GROUP BY p.pro_name, p.id
ORDER BY p.pro_name, foto DESC
LIMIT 21
The query LEFT JOIN (SELECT * FROM osrs_photos ORDER BY ordering ASC) f ON f.pro_id = p.id
is for me to take the photo with the lowest ordering, it is leaving the query slow