我有一个位置表,我的应用程序的用户需要能够在其中搜索。基本上是“我 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),这可能会使搜索速度更快。
这可能吗?如何创建该索引?
谢谢!
丹尼尔