熟悉 postgresql 大侠的请再次留步

2019-01-23 18:44:50 +08:00
 yaxianzhi
新上手阿里云 postgresql,发现有个关键查询很慢,比在 ecs 上自建 pg 还要慢 2 到 3 倍;打印初查询计划,请大家给诊断诊断,现行谢过~



2472 次点击
所在节点    PostgreSQL
5 条回复
yaxianzhi
2019-01-23 18:48:34 +08:00
终于可以了,期间打扰大家了
hilbertz
2019-01-23 18:50:10 +08:00
表结构和查询都不给出来,看 query plan 看不出名堂的
yaxianzhi
2019-01-23 19:27:20 +08:00
查询语句:
SELECT g.grid_no FROM city_grid g JOIN poi p ON g.city = '北京' and p.city = '北京' and g.grid_level = 1 and p.xm_type = '住宅' AND st_dwithin(g.center, p.location, 400) GROUP BY g.grid_no HAVING count(p.id) >= 3;

表结构如下

table city_grid 数据量 1600 万

address varchar(255),
box geography,
center geography,
city varchar(255),
grid_level integer,
grid_no integer

box gist 索引
center gist 索引
grid_no btree 索引

table poi 60 万

id serial not null constraint poi_pkey primary key,
address text,
city varchar(255),
district varchar(255),
location geography,
name varchar(255),
poi_id varchar(255),
type varchar(255),
type_code varchar(255),
xm_type varchar(255)

location gist 索引
hilbertz
2019-01-23 19:45:49 +08:00
你如果要对 city 进行 join 的话,最好要索引 city,grid_level 和 xm_type 也一样,因为 pg 会在 join 前用这些索引过滤掉大部分行
luozic
2019-01-24 08:59:16 +08:00
先過濾 再 join。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/529929

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX