我有一个问题,好像我有三个表。我将国家和州的id存储在药房表中,与国家和州相关的所有详细信息分别位于tbldispensaries
和tblStates
中。
我正在写一个查询,以找出附近的药房半径500,以便在谷歌地图上显示标记,该查询是好的,但当我使用左连接查询,它需要太多的时间来响应。我已经尝试使用索引,但这也没有影响。所以请建议我克服这个问题。下面是我正在使用的查询:-
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)
我想获取结果和使用a
JOIN
会更快。