我需要按距离从 InnoDb 表中获取记录(不能完全正确)并按距离排序。该表有 1000 万条记录。
到目前为止,我最好的时间是 8 秒(没有按距离排序的 3 秒),这使得它无法使用。我该如何改进?
我有一个定义为 SRID 4326 的点列。我使用的是 MySQL 8.0.12。
SELECT mp.hash_id,
ROUND(ST_Distance(ST_SRID(POINT(8.53955, 47.37706), 4326), mp.geo_pt), 2) AS distance
FROM member_profile mp
WHERE
MBRCONTAINS(ST_GeomFromText(
CONCAT('POLYGON((', ST_X(POINT (8.53955, 47.37706)) - 0.43415340086831, ' ',
ST_Y(POINT (8.53955, 47.37706)) - 0.43415340086831, ',',
ST_X(POINT (8.53955, 47.37706)) + 0.43415340086831, ' ',
ST_Y(POINT (8.53955, 47.37706)) - 0.43415340086831, ',',
ST_X(POINT (8.53955, 47.37706)) + 0.43415340086831, ' ',
ST_Y(POINT (8.53955, 47.37706)) + 0.43415340086831, ',',
ST_X(POINT (8.53955, 47.37706)) - 0.43415340086831, ' ',
ST_Y(POINT (8.53955, 47.37706)) …
Run Code Online (Sandbox Code Playgroud) 在放弃 MySQL 之后,我尝试了 Elasticsearch,现在不想看看我是否可以使用 PostgreSQL/PostGIS,因为它可以让我只使用 PostgreSQL。
我需要按距离(不能完全相同)从表中获取记录并按距离排序。该表有 1000 万条记录。
当我在 PostgreSQL 上查询比在 MySQL 上的查询速度慢时,我想我一定做错了什么。
我可以做什么更好?
桌子:
id | hash_id | town | geo_pt2
geo_pt2 is geography
Run Code Online (Sandbox Code Playgroud)
指数:
CREATE INDEX geo_pt2_gix ON public.member_profile USING gist (geo_pt2)
Run Code Online (Sandbox Code Playgroud)
询问:
SELECT hash_id, town
, ST_Distance(t.x, geo_pt2) AS dist
FROM member_profile, (SELECT ST_GeographyFromText('POINT(47.4667 8.3167)')) AS t(x)
WHERE ST_DWithin(t.x, geo_pt2, 250000)
ORDER BY dist
limit 100 offset 1000;
Run Code Online (Sandbox Code Playgroud)
解释:
Limit (cost=9.08..9.08 rows=1 width=53)
-> Sort (cost=9.07..9.08 rows=1 width=53)
Sort Key: (_st_distance('0101000020E610000088855AD3BCBB474052499D8026A22040'::geography, member_profile.geo_pt2, '0'::double precision, true))
-> …
Run Code Online (Sandbox Code Playgroud) postgresql performance spatial postgis postgresql-10 postgresql-performance