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

4 千万的数据量, postgre order by 优化。

  •  
  •   xmge · 2020-08-25 17:33:20 +08:00 · 1019 次点击
    这是一个创建于 1311 天前的主题,其中的信息可能已经有所发展或是发生改变。

    各位大佬,有个这样的数据库优化问题:

    select xxxx,xxx from tables where a > xx order by update limit 100;
    
    a 和 update 都有索引
    

    数据量 4 千万,执行 explain analyze 分析 sql 结果如下,主要的花费在于 Recheck Cond,

    这个问题如何解决呢???

    Sort  (cost=2058.65..2059.93 rows=512 width=89) (actual time=513.218..513.274 rows=333 loops=1)
      Sort Key: updated
      Sort Method: quicksort  Memory: 74kB
      ->  Bitmap Heap Scan on Tables_XXX  (cost=20.53..2035.61 rows=512 width=89) (actual time=15.101..512.814 rows=333 loops=1)
            Recheck Cond: ((user_id)::text = 'XXXXXXX'::text)
            Heap Blocks: exact=318
            ->  Bitmap Index Scan on XXXX_INDEX  (cost=0.00..20.41 rows=512 width=0) (actual time=11.006..11.006 rows=333 loops=1)
                  Index Cond: ((user_id)::text = 'XXXXXXX'::text)
    Planning time: 0.089 ms
    Execution time: 513.320 ms
    
    
    3 条回复    2020-08-27 13:49:20 +08:00
    MoYi123
        1
    MoYi123  
       2020-08-25 21:36:54 +08:00
    min_pk,max_pk = select min(primary_key), max(primary_key) from table where a > xx;
    select xxxx from tables where min_pk < pk and pk < max_pk and a > xx order by update limit 100;
    可以试一试这样有没有用。
    参考了这篇博客
    https://github.com/digoal/blog/blob/master/202007/20200710_01.md
    xmge
        2
    xmge  
    OP
       2020-08-25 21:59:06 +08:00
    @MoYi123 好的,我看一下,感谢啦
    yuan434356430
        3
    yuan434356430  
       2020-08-27 13:49:20 +08:00
    把 postgre 的并行打开试试会不会变快
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5321 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 09:03 · PVG 17:03 · LAX 02:03 · JFK 05:03
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.