如何加快查询速度?

时间:2018-09-12 15:19:37

标签: mysql sql

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

有什么想法吗?

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 个答案:

答案 0 :(得分:1)

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

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

魔术发生了。