我有一个问题,比如我有三个表tbldispensaries
,tblStates
,tblCountries
。我将国家和州的ID存储在药房表中,所有与国家和州相关的详细信息分别位于tblCountries
和tblStates
。
我正在编写一个查询,以便在半径500范围内找到附近的药房,以便在Google地图上显示标记,查询很好,但是当我使用左边连接查询时,它需要花费太多时间响应。我尝试过使用索引,但这对它没有任何影响。所以请建议我克服这个问题。这是我正在使用的查询: -
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
答案 0 :(得分:0)
这可能不是一个答案,但是评论是两个大问题。此外,我没有数据,所以我无法测试,但现在是。
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
会更快。