我的桌子看起来像这样:
CREATE TABLE public.places
(
id bigint DEFAULT nextval('places_id_seq'::regclass) PRIMARY KEY NOT NULL,
lon numeric(18,13) DEFAULT NULL::numeric,
lat numeric(18,13) DEFAULT NULL::numeric,
location geography(Point,4326)
);
CREATE INDEX places_ll ON public.places (lon, lat);
CREATE INDEX places_location_ix ON public.places (location);
INSERT INTO public.places (id, lon, lat, location) VALUES (1, 14.4783371228873, 46.0299536240291, '0101000020E610000003CD3585D50347400287769AE8F42C40');
现在我不想用查询来查找附近的位置:
SELECT ST_Distance(Geography(ST_MakePoint(14.47859, 46.02998166)), location) as dist, lon, lat, location FROM places
WHERE ST_DWithin(Geography(ST_MakePoint(14.47859, 46.02998166)), Geography(location), 50) ORDER BY dist LIMIT 1;
我得不到任何结果。然后我尝试查询:
SELECT ST_Distance(Geography(ST_MakePoint(14.47859, 46.02998166)), ST_MakePoint(lon,lat)) as dist, lon, lat, location FROM places
WHERE ST_DWithin(Geography(ST_MakePoint(14.47859, 46.02998166)), Geography(ST_MakePoint(lon,lat)), 50) ORDER BY dist LIMIT 1;
我得到了一个结果:
14.4783371228873 46.0299536240291 0101000020E610000003CD3585D50347400287769AE8F42C40
问题是,第二个查询返回结果,但比第一个查询慢得多,第一个查询不返回结果,但非常快(索引搜索)。当然,我喜欢两者的混合体。毫不奇怪,我想要的是尽可能快的结果。
https://stackoverflow.com/questions/51987887
复制相似问题