3

我有一个位置表,我的应用程序的用户需要能够在其中搜索。基本上是“我 x 英里内的位置”

我正在使用这样的查询:

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),这可能会使搜索速度更快。

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

谢谢!
丹尼尔

4

1 回答 1

6

您可能需要btree_gis t 扩展来创建索引以包含 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));
于 2013-08-05T14:16:46.057 回答