此查询非常慢,我正在寻找一种优化方法。
有什么想法吗?
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
答案 0 :(得分:1)
从1.300.000行中的3.7s到0.1s,执行以下步骤:
products.locale
,products.rating
,products.reviews
和products.price
上添加索引locations.place_id
和locations.city_place_id
上添加了索引魔术发生了。