空间索引与两个坐标索引
我有一个包含两列的表:纬度和经度。我想要获取“矩形”(好吧,纬度/经度坐标度量中的矩形)边界框内的所有对象:最小-最大纬度和最小-最大经度。基本上可以归结为以下伪 SQL:
SELECT * FROM MyTable WHERE lat < :maxlat AND lat > :minlat
AND lon < :maxlon AND lon > :minlon
为表建立索引的最佳解决方案是什么?两列索引?两列上有两个索引?空间索引?
我想知道在这种情况下是否真的需要空间索引,因为您需要特殊的列、特定的库,所有这些都以牺牲数据库的可移植性和简单性为代价。
注意:我想保持这个问题与数据库无关,但为了完整起见,我提到我正在使用 PostGreSQL 8,没有(目前)PostGIS。
I have a table with two columns: latitude and longitude. I want to get all objects inside a "rectangular" (well, rectangular in a lat/lon coordinate metric) bounding box: min-max latitude and min-max longitude. Basically that boils down to the following pseudo-SQL:
SELECT * FROM MyTable WHERE lat < :maxlat AND lat > :minlat
AND lon < :maxlon AND lon > :minlon
What's the best solution for indexing my table? A two-column index? Two indexes on the two columns? A spatial index?
I would like to know if a spatial index is really needed in that case, as you need a special column, specific libraries, all that at the expense of database portability and simplicity.
Note: I'd like to keep this question database-agnostic, but for the sake of completeness I mention the fact that I'm working with PostGreSQL 8, w/o (for now) PostGIS.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 PostgreSQL 版本是什么:8.0、8.1 等?如果您有“高版本”,您可以尝试包含 lat 和 lon 列作为点类型的唯一列。像这样:
并创建测试所需的索引:
现在您可以测试哪种类型的查询更快:
或者:
我在 PostgreSQL 9(有 20000 条记录)上进行了测试,第二个选项更快。
What's your version of PostgreSQL: 8.0, 8.1, etc? If you have a "high version", you could try include lat and lon column as a unique column of point type. Like this:
And create the indexes needed to test:
Now you can test what type of query is faster:
Or:
I did the test on PostgreSQL 9 (with 20000 records) and the second option is more faster.