这个查询非常慢,我正在寻找一种优化的方法。

有任何想法吗?

select *,
      (rating / (select max(rating) from products)) rk_rating, (reviews /
      (select max(reviews) from products)) rk_reviews,
      (if(label = "", 0, 1)) rk_label,
      (1 - price / (select max(price) from products)) rk_price
from `products`
where (`locale` = 'it' or `locale` = '*')
  and `id` in (select `product_id`
               from `location_product`
               where `location_product`.`location_id` in (select `id`
                                                          from `locations`
                                                          where `place_id` = 'ChIJdd4hrwug2EcRmSrV3Vo6llI' or `city_place_id` = 'ChIJdd4hrwug2EcRmSrV3Vo6llI'))
order by (rk_rating * '2' + rk_reviews * '5' + rk_label * '0.5' + rk_price * '50') desc limit 21 offset

最佳答案

从1.300.000行中的3.7s到0.1s,执行以下步骤:


products.localeproducts.ratingproducts.reviewsproducts.price上添加了索引
locations.place_idlocations.city_place_id上添加了索引


魔术发生了。

关于mysql - 如何加快查询速度?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52298587/

10-13 23:28