2013-08-05 34 views
2

我有一个应用程序用户需要搜索的位置表。“保存”位于x英里范围内的位置“如何创建包含经度/纬度(使用GIST)的PostgreSQL索引,同时也是常规字段

我使用这样的查询:

select * from job_locations 
    where earth_box(ll_to_earth(51.5, -0.085), 5000) @> ll_to_earth(latitude, longitude) 
    and earth_distance(ll_to_earth(51.5, -0.085), ll_to_earth(latitude, longitude)) < 5000; 

和索引这样的:

CREATE INDEX job_locations_lat_lng on job_locations USING gist(ll_to_earth(latitude, longitude)); 

然而,这是一个多租户系统,在这里所有的表有一个” tenant_id“字段,和我们总是过滤器也是如此。所以理想情况下,我可以在索引中同时拥有tenant_id和ll_to_earth(lat,lng),这应该可以使搜索速度更快。

这可能吗?我如何创建该索引?

谢谢!
丹尼尔

+0

快速的问题,是'要点(ll_to_earth()'空间索引或者Postgres的一个通用指标? –

回答

5

你可能会需要btree_gis吨扩展来创建索引中包含的tenant_id场,这似乎因为已经存在至少Postgres的8 .4。

CREATE EXTENSION btree_gist; 
CREATE INDEX job_locations_lat_lng ON job_locations USING gist(tenant_id, ll_to_earth(latitude, longitude)); 
+0

优秀,并称扩展解决了这一问题。谢谢!! –

+0

快速的问题,是'要点(ll_to_earth ()'postgres的空间索引或通用索引? –

+0

“ll_to_earth()”是“earthdistance”模块的组成部分, “gist”是PostgreSQL核心的索引类型,请参阅http://www.postgresql.org/docs/current/static/textsearch-indexes.html – bma

相关问题