V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
yaxianzhi
V2EX  ›  PostgreSQL

熟悉 postgresql 大侠的请再次留步

  •  
  •   yaxianzhi · 2019-01-23 18:44:50 +08:00 · 2436 次点击
    这是一个创建于 1891 天前的主题,其中的信息可能已经有所发展或是发生改变。
    新上手阿里云 postgresql,发现有个关键查询很慢,比在 ecs 上自建 pg 还要慢 2 到 3 倍;打印初查询计划,请大家给诊断诊断,现行谢过~



    5 条回复    2019-01-24 08:59:16 +08:00
    yaxianzhi
        1
    yaxianzhi  
    OP
       2019-01-23 18:48:34 +08:00
    终于可以了,期间打扰大家了
    hilbertz
        2
    hilbertz  
       2019-01-23 18:50:10 +08:00
    表结构和查询都不给出来,看 query plan 看不出名堂的
    yaxianzhi
        3
    yaxianzhi  
    OP
       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
        4
    hilbertz  
       2019-01-23 19:45:49 +08:00
    你如果要对 city 进行 join 的话,最好要索引 city,grid_level 和 xm_type 也一样,因为 pg 会在 join 前用这些索引过滤掉大部分行
    luozic
        5
    luozic  
       2019-01-24 08:59:16 +08:00
    先過濾 再 join。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4538 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 10:00 · PVG 18:00 · LAX 03:00 · JFK 06:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.