我有一个位置表,我的应用程序用户需要能够在其中搜索。Baically"距离我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),这可能会使搜索速度更快。
这可能吗?如何创建该索引?
谢谢
Daniel
您可能需要btree_gist扩展来创建包含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));