0

This may or may not be a simple question.

I have a query which selects all locations within a mile radius around a provided latitude and longitude. That part works perfectly, but I have additional information inside of another table that I would like to match to it. I've tried LEFT JOIN, but it's timing out.

SELECT *, 
( 3959 * acos( cos( radians(40.7143528) ) * cos( radians( lat ) ) 
* cos( radians( lon ) - radians(-74.0059731) ) + sin( radians(40.7143528) ) 
* sin( radians( lat ) ) ) ) AS distance 
FROM locations
LEFT JOIN informations ON
locations.name = informations.name
HAVING distance < 1

Here is what I would like this query to do:

  1. Provide all matching locations within a mile radius (works)
  2. Obtain the name of those stores (works)
  3. Match those names against the names of each store in the "informations" table
  4. Join all of the information in the matching rows together, where "locations.name" and "informations.name" match

The above query seems like it wants to work. I don't get any errors and it shows as valid in any MySQL formatter I use. However, I think I'm making an error somewhere which causes my tiny server to max processor usage.

To a more experienced set of eyes, would you see a reason why this would occur? Other than my server being near useless, of which I'm aware.

4

1 回答 1

0

使您的初始查询成为主查询的子查询,然后将生成的关系与新表左连接(带有附加信息)。那应该让你的表现恢复

结果将如下所示:

SELECT * 
FROM( 
  SELECT *, 
  ( 3959 * acos( cos( radians(40.7143528) ) * cos( radians( lat ) ) 
  * cos( radians( lon ) - radians(-74.0059731) ) + sin( radians(40.7143528) ) 
  * sin( radians( lat ) ) ) ) AS distance 
  FROM locations
) locations 
LEFT JOIN informations 
  ON location.name =informations.name 

HAVING 子句当然是查询#1 的一部分。

我通常使用 SQL SERVER,因此语法可能不太适合 MYSQL。

于 2013-04-26T23:05:54.180 回答