-3
I have a query in mysql that is very slow , more than 25 seconds.
The base is quite large, around 1,000 records of real estate (table osrs_properties) and 16,000 photo records (table osrs_photos).
The consultation without the photo takes half a second, but with photo takes 25, as I can improve the query or the table to increase the speed of the query?
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 consultation LEFT JOIN (SELECT * FROM osrs_photos ORDER BY ordering ASC) f ON f.pro_id = p.id
serves for me to catch the photo with the smaller Ordering, she that is making the consultation slow
Try to change the
SELECT * FROM osrs_photos
for a query that returns only the columns you use in your query. See if you have any gains.– Victor Laio
1000 records is practically insignificant. Your problem is surely the image files in the bd. The ideal is not to have them in the comics, but outside, controlling only the address by the bank. Probably 1 image, worth 1000 records of your other tables! rs
– rbz
@Rbz It would not be an option for it to store in the bank but in a compressed form?
– Victor Laio
@Rbz is then, is that the query works like this, it will bring all immovable and 1 photo, and this photo has to be the smallest (the Ordering column has to be the smallest) I made a test using Ordering =1, hence it goes super fast, the problem is that it has records that do not have Ordering 1, the smallest is 2 for example
– Leandro Marzullo
@The problem is not space, but processing. Compressing will probably get worse, as the database will have 1 more step to open the image.
– rbz
@Victorlaio how to compress asism? (work with mysql)
– Leandro Marzullo
@The decompression process would be done by the backend so as not to overload the bank, and I have seen several processes that can decrease the size of an image drastically. In the companies I worked I never recommended storage on the server due to security.
– Victor Laio
@Leandro, it would compress the image or use processes in the backend to decrease the size of the image before saving it in the database to avoid consuming so much resource. Like what Whatsapp does with images.
– Victor Laio
@Victorlaio as I understand it, you said to dimuir the image size, this? if it is, it is not necessary, what is taking actually, is to find the reference of that image in the bank
– Leandro Marzullo
@Victorlaio why you do not do the search without the image and after that return goes in the bank again (async) to upload the image? I think it is a very simple solution and should solve the problem
– rLinhares
@Leandromarzullo You tried to use a
SELECT TOP
, and search for the first record found, making aORDER BY
by another field, since you have fieldNULL
inORDERING
? This Join is probably causing the slownessLEFT JOIN (SELECT * FROM osrs_photos ORDER BY ordering ASC) f ON f.pro_id = p.id
– rbz
@Rbz taking advantage of his comment that 1000 records is insignificant, from more or less records qts, I consider as large (considering a table of 5 columns)
– Leandro Marzullo
@Leandromarzullo It depends a lot on your server, your database, indexes, etc. But "basically" a table with only 5 columns (varchar, int, thinking of the most used), can put millions and millions! rs
– rbz
show, thanks for the teachings...rs
– Leandro Marzullo