【发布时间】:2011-12-09 16:46:31
【问题描述】:
我在 postgres 数据库中有一个包含许多列的表,其中我有:
n_store_object_id integer,
n_latitude decimal,
n_longitude decimal
该表目前大约有 250,000 行。
我需要查找距给定位置固定距离内具有非空 store_object_id 的记录。对于距离计算,我有以下功能:
CREATE OR REPLACE FUNCTION fn_geo_distance(numeric, numeric, numeric, numeric)
RETURNS numeric AS
$BODY$
declare
lat1d ALIAS for $1;
lon1d ALIAS for $2;
lat2d ALIAS for $3;
lon2d ALIAS for $4;
lat1 DECIMAL := lat1d / 57.29577951;
lon1 DECIMAL := lon1d / 57.29577951;
lat2 DECIMAL := lat2d / 57.29577951;
lon2 DECIMAL := lon2d / 57.29577951;
begin
return 3963.0 * acos(sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon2 - lon1));
end;$BODY$
LANGUAGE plpgsql IMMUTABLE;
现在,我需要的查询很简单:
select *
from objects
where n_store_object_id is not null
and fn_geo_distance(51.5, 0, n_latitude, n_longitude) <= 20
这需要很长时间 - 当我“解释”这个查询时,我可以看到全表扫描。很公平。所以我在这三列上创建了一个索引:
create index idx_object_location on objects(n_store_object_id, n_latitude, n_longitude)
我重新运行上面的查询 - 仍然需要很长时间。 “解释”它表明新创建的索引没有被使用。我错过了什么吗?为什么不使用它,如何强制引擎使用它?哦,首先,这个索引会有帮助吗?
谢谢!
【问题讨论】: