SQL
sql
select *, st_distance_sphere( geometry(poi), geometry('POINT(116.41739 39.913164)') ) distance from test order by distance asc limit 10
EXPLAIN结果:
Limit (cost=14826.00..14826.02 rows=10 width=51)
-> Sort (cost=14826.00..14950.85 rows=49939 width=51)
Sort Key: (_st_distance(geography(geometry(poi)), '0101000020E61000005BEB8B84B61A5D4039F1D58EE2F44340'::geography, 0::double precision, false))
-> Seq Scan on test (cost=0.00..13746.83 rows=49939 width=51)
已经给poi建立了gist索引了……为啥却没用呢?
才12w数据,检索却要0.6s
表结构
id serial8 primary key not null,
name varchar(80) not null default '',
poi st_geometryfromtext('POINT(112.123, 23.111)',4326)
索引
create index poi on test using gist(poi)
自问自答:
正确的SQL应该是: