求助,关于 sql 查询时间取并集的情况 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
vanpeisi7
V2EX    PostgreSQL

求助,关于 sql 查询时间取并集的情况

  •  1
     
  •   vanpeisi7 2024 年 1 月 8 日 2206 次点击
    这是一个创建于 766 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如下,这种该如何优化,添加 open_time 和 close_time 的索引,但是还是比较慢,查询时候大约 500ms.

    select count(1) from switchcover where open_time <= '2024-01-07 23:59:59' and close_time>= '2024-01-07 00:00:00' ;

    11 条回复    2024-01-09 14:17:43 +08:00
    vanpeisi7
        1
    vanpeisi7  
    OP
       2024 年 1 月 8 日
    单纯的加上 limit 和 offset 分页查询是很快的,几十 ms ,就是 count 太耗时了。

    select
    *
    from
    switch.bz_switchcover
    where
    close_time>= '2023-05-01 00:00:00'
    and open_time <= '2323-05-01 23:59:59'
    order by open_time desc
    limit 15 offset 0
    ;
    whoami9426
        2
    whoami9426  
       2024 年 1 月 8 日
    贴下 explain
    vanpeisi7
        3
    vanpeisi7  
    OP
       2024 年 1 月 8 日
    @whoami9426

    ```
    Finalize Aggregate (cost=362425.35..362425.36 rows=1 width=8)
    -> Gather (cost=362425.14..362425.35 rows=2 width=8)
    Workers Planned: 2
    -> Partial Aggregate (cost=361425.14..361425.15 rows=1 width=8)
    -> Parallel Index Only Scan using bz_switchcover_open_close_time_c_idx on bz_switchcover (cost=0.43..358436.32 rows=1195526 width=0)
    Index Cond: ((open_time <= '2023-05-11 23:59:59'::timestamp without time zone) AND (close_time >= '2023-05-11 00:00:00'::timestamp without time zone))
    JIT:
    Functions: 5
    Options: Inlining false, Optimization false, Expressions true, Deforming true

    ```
    vanpeisi7
        4
    vanpeisi7  
    OP
       2024 年 1 月 8 日
    详细的 explain:

    Finalize Aggregate (cost=425545.45..425545.46 rows=1 width=8) (actual time=522.040..573.976 rows=1 loops=1)
    -> Gather (cost=425545.23..425545.44 rows=2 width=8) (actual time=521.286..573.949 rows=3 loops=1)
    Workers Planned: 2
    Workers Launched: 2
    -> Partial Aggregate (cost=424545.23..424545.24 rows=1 width=8) (actual time=494.321..494.322 rows=1 loops=3)
    -> Parallel Index Only Scan using bz_switchcover_open_close_time_c_idx on bz_switchcover (cost=0.43..421581.16 rows=1185629 width=0) (actual time=20.590..487.712 rows=9267 loops=3)
    Index Cond: ((open_time <= '2023-07-16 23:59:59'::timestamp without time zone) AND (close_time >= '2023-07-16 00:00:00'::timestamp without time zone))
    Heap Fetches: 18
    Planning Time: 0.184 ms
    JIT:
    Functions: 11
    Options: Inlining false, Optimization false, Expressions true, Deforming true
    Timing: Generation 1.365 ms, Inlining 0.000 ms, Optimization 1.067 ms, Emission 17.333 ms, Total 19.766 ms
    Execution Time: 574.577 ms
    Huelse
        5
    Huelse  
       2024 年 1 月 8 日
    你应该 create index where close_time>= '2024-01-07 00:00:00'::timestamp;
    whoami9426
        6
    whoami9426  
       2024 年 1 月 8 日
    看看这篇 [理解 PostgreSQL 的 count 函数的行为]( https://zhuanlan.zhihu.com/p/63379010)
    如果表中数据量本身就很大, count 耗时长是难免的
    opengps
        7
    opengps  
       2024 年 1 月 8 日
    如果没有时间差字段,那么最好是有这俩时间列的联合索引。
    如果有时间差字段,那么开始时间和时间差字段的组合要比直接操作两个 time 类型容易很多,这里也得有联合索引辅助
    MoYi123
        8
    MoYi123  
       2024 年 1 月 8 日
    这个 sql 什么意思, 感觉逻辑很奇怪, 一般来说 open_time 应该小于 close_time 吧


    总之想个办法, 把其中一个时间改成 between 一个较小的时间段, 性能应该就会好不少.

    现在是 2 个大 set 取交集, 肯定很慢.
    siweipancc
        9
    siweipancc  
       2024 年 1 月 9 日 via iPhone
    给个建议,count(id)
    siweipancc
        10
    siweipancc  
       2024 年 1 月 9 日 via iPhone
    Belmode
        11
    Belmode  
       2024 年 1 月 9 日
    select count(1) from (select id, close_time from switchcover where open_time <= '2024-01-07 23:59:59') t where t.close_time>= '2024-01-07 00:00:00' ;
    要不这样试试?
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     873 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 28ms UTC 19:35 PVG 03:35 LAX 11:35 JFK 14:35
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86