gpt4 book ai didi

MySQL 左连接返回时间太长

转载 作者:行者123 更新时间:2023-11-29 03:26:22 24 4
gpt4 key购买 nike

我有一个问题,比如我有三个表 tbldispensariestblStatestblCountries。我将国家和州 ID 存储在药房表中,与国家和州相关的所有详细信息分别在 tblCountriestblStates 中。

我正在编写一个查询,以找出半径 500 以内的附近药房,以便在 Google map 上显示标记,查询很好,但是当我在查询中使用左连接时,它花费了太多时间回应。我试过使用索引,但这对它也没有影响。所以请建议我克服这个问题。以下是我正在使用的查询:-

SELECT
d.id AS disp,
d.id AS the_id,
d. NAME,
d.address AS addr,
d.city AS city_name,
c.country_name AS cntry_name,
s.state_name AS st_name,
d.zip AS zipcode,
d.latitude,
d.longitude,
(
6371 * ACOS(
COS(RADIANS(33.6119)) * COS(RADIANS(latitude)) * COS(
RADIANS(longitude) - RADIANS(- 111.8906)
) + SIN(RADIANS(33.6119)) * SIN(RADIANS(latitude))
)
) AS distance
FROM
`tblDispensaries` d
LEFT JOIN tblCountries c ON (c.country_id = d.country)
LEFT JOIN tblStates s ON (s.state_id = d.state)
WHERE
d.id IS NOT NULL
AND d. STATUS = 1
HAVING distance < 500
LIMIT 0,60

最佳答案

这可能不是一个答案,但它是两个重要的评论。另外,我没有数据,所以我无法测试,但就在这里。

SELECT 
Dispensaries.*,
c.country_name AS cntry_name,
s.state_name AS st_name
FROM (
SELECT
d.id AS disp,
d.id AS the_id,
d. NAME,
d.address AS addr,
d.city AS city_name,
d.zip AS zipcode,
d.latitude,
d.longitude,
(
6371 * ACOS(
COS(RADIANS(33.6119)) * COS(RADIANS(latitude)) * COS(
RADIANS(longitude) - RADIANS(- 111.8906)
) + SIN(RADIANS(33.6119)) * SIN(RADIANS(latitude))
)
) AS distance
FROM
`tblDispensaries` d
WHERE
d.id IS NOT NULL
AND d. STATUS = 1
HAVING distance < 500
LIMIT 0,60
) as Dispensaries
LEFT JOIN tblCountries c ON (c.country_id = Dispensaries.country)
LEFT JOIN tblStates s ON (s.state_id = Dispensaries.state)

我想获取结果并使用 JOIN 会更快。

关于MySQL 左连接返回时间太长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35649477/

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