gpt4 book ai didi

sql - 需要一些帮助来优化复杂的 SQL 查询

转载 作者:行者123 更新时间:2023-11-29 09:16:38 31 4
gpt4 key购买 nike

我想知道是否有人可以提供一些关于我如何优化这个 MySQL 查询的意见。我认为我对索引做了正确的事情,所以我不认为我可以更快地获得这个查询(目前运行时间超过 3 秒),但我真的很喜欢有更多经验的人来证明我错了。

这是查询:

SELECT `rooms`.*,
((IFNULL(SUM(av.host_daily_price), 0) +
rooms.host_daily_price * (4 - COUNT(DISTINCT av.id))) / 4) / 1 as 'price',
rooms.*,
(ACOS(least(1, COS(0.7115121627883911) * COS(1.291278129536698) *
COS(RADIANS(rooms.lat)) * COS(RADIANS(rooms.lng)) +
COS(0.7115121627883911) * SIN(-1.291278129536698) *
COS(RADIANS(rooms.lat)) * SIN(RADIANS(rooms.lng)) +
SIN(0.7115121627883911) * SIN(RADIANS(rooms.lat)))) * 3963.19) AS distance
FROM `rooms`
LEFT JOIN availabilities AS av
ON (av.room_id = rooms.id AND
av.date BETWEEN '2010-12-29' AND '2011-01-01')
WHERE (rooms.deleted_at IS NULL) AND
(`rooms`.`hidden` = 0) AND
(rooms.id <> 7713) AND
(rooms.city_id = 1 AND
rooms.max_guests >= 4 AND
rooms.minimum_stay <= 3 AND
rooms.room_type IN ('room','apartment','house')) AND
(av.inquiry_id IS NULL)
GROUP BY rooms.id
HAVING SUM(IFNULL(status, 0)) = 0 AND
(COUNT(*) = 4 OR `rooms`.default_available = 1)
ORDER BY distance ASC
LIMIT 12;

解释的输出:

id  select_type table   type    possible_keys   key key_len ref rows    Extra

1 SIMPLE rooms ref PRIMARY,index_rooms_on_city_id,index_rooms_on_room_type,index_rooms_on_city_id_and_updated_at index_rooms_on_city_id 5 const 2412 Using where; Using temporary; Using filesort

1 SIMPLE av ref index_availabilities_on_room_id,index_availabilities_on_room_id_and_date,index_availabilities_on_room_id_and_date_and_status index_availabilities_on_room_id 5 roomorama.rooms.id 79 Using where

如果有其他有用的信息,请告诉我!

最佳答案

您应该认真考虑存储距离而不是计算它,特别是如果您按该值排序。

关于sql - 需要一些帮助来优化复杂的 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3939519/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com