0

I have a series of rows in MySQL with a 'location' column, which represents the location of an object on a two dimensional xy grid. I want to search the table for rows with a location which is within a given distance of a certain tiles.

For example, if I ran a search within 10 tiles of [34,56], that would return any rows with a 'location' value between [24-44 and 46-66].

My solution to this problem was to create an array (using for loops) with all of the possible tiles that would fall within that search term, and then query MySQL thusly:

"SELECT * FROM table WHERE localcoordinate IN ('$rangearray')"

This solution works fine, but is very resource intensive. I'd like to be able to run many searches at a distance of hundreds or thousands of tiles. Can anyone suggest a better approach that might run faster?

4

1 回答 1

0

通过实施以下策略更改,我将资源消耗提高了 100 倍。

1) 我将 xy 坐标分成表中的两个字段。

2) 我在 MySQL 中使用“BETWEEN”功能进行本地搜索。

最终查询看起来像这样。您可以从查询中推断数据结构。

SELECT * FROM table WHERE localcoordinateX BETWEEN $x-lo AND $x-hi AND localcoordinateY BETWEEN $y-lo AND $y-hi.

我应该第一次想到这个,但我没有。不过,只是发布到堆栈交换的行为让我再次清晰地思考!

于 2013-11-14T01:21:59.847 回答