请假各位大哥 pg 中一个相对比较简单的查询为什么效率这么低 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
zhwguest
V2EX    数据库

请假各位大哥 pg 中一个相对比较简单的查询为什么效率这么低

  •  
  •   zhwguest 325 天前 2058 次点击
    这是一个创建于 325 天前的主题,其中的信息可能已经有所发展或是发生改变。

    其实就是一个主表,里面有两个指向明细表的索引。

    create table pairs1(id serial primary key, ai int not null, bi int not nul); create table pairs2(id serial primary key, ai int not null, bi int not null); create table items1(id serial primary key, name text not null); create table items2(id serial primary key, name text not null); create view pairs as select * from pairs1 union all select * from pairs2; create view items as select * from items1 union all select * from items2; create view viewpairs as select t0.id, t1.id as aid, t1.name as aname, t2.id as bid, t2.name as bname from pairs t0 left join items t1 on t0.ai=t1.id left join items t2 on t0.bi=t2.id; 
    $ explain select * from viewpairs; QUERY PLAN ------------------------------------------------------------------------------------------------------- Merge Right Join (cost=5319.09..15347.88 rows=658063 width=76) Merge Cond: (items1_1.id = pairs1.bi) -> Merge Append (cost=0.32..151.82 rows=2540 width=36) Sort Key: items1_1.id -> Index Scan using items1_pkey on items1 items1_1 (cost=0.15..63.20 rows=1270 width=36) -> Index Scan using items2_pkey on items2 items2_1 (cost=0.15..63.20 rows=1270 width=36) -> Sort (cost=5318.77..5448.31 rows=51816 width=44) Sort Key: pairs1.bi -> Merge Right Join (cost=326.20..1261.29 rows=51816 width=44) Merge Cond: (items1.id = pairs1.ai) -> Merge Append (cost=0.32..151.82 rows=2540 width=36) Sort Key: items1.id -> Index Scan using items1_pkey on items1 (cost=0.15..63.20 rows=1270 width=36) -> Index Scan using items2_pkey on items2 (cost=0.15..63.20 rows=1270 width=36) -> Sort (cost=325.88..336.08 rows=4080 width=12) Sort Key: pairs1.ai -> Append (cost=0.00..81.20 rows=4080 width=12) -> Seq Scan on pairs1 (cost=0.00..30.40 rows=2040 width=12) -> Seq Scan on pairs2 (cost=0.00..30.40 rows=2040 width=12) (19 rows) 

    问了 ai ,提示在 pairs1 和 pairs2 中为 ai 和 bi 创建索引,但我总觉得有点不靠谱。

    7 条回复    2025-02-08 18:20:00 +08:00
    xiaoxinshiwo
        1
    xiaoxinshiwo  
       325 天前 via Android
    -> Sort (cost=5318.77..5448.31 rows=51816 width=44)
    Sort Key: pairs1.bi
    lysShub
        2
    lysShub  
       325 天前
    你这也不简单吧?这么多联表,视图套视图。。。

    看 cost 有 sort, 把联表条件的列加上索引试试
    F281M6Dh8DXpD1g2
        3
    F281M6Dh8DXpD1g2  
       325 天前   1
    join 了之后再 union all
    sagaxu
        4
    sagaxu  
       325 天前
    这查询慢才符合预期,一个 where 条件都没有,如果你是 DB 引擎,你怎么优化?
    zbinlin
        5
    zbinlin  
       325 天前
    你这都没 where
    kkwa56188
        6
    kkwa56188  
       325 天前
    union all 了以后, views 里的 id 就没有索引了.
    sthwrong
        7
    sthwrong  
       324 天前
    查所有数据还用黑魔法吗?运气好可能比你 4 个表依次读出来程序组装还快点。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     946 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 23ms UTC 19:28 PVG 03:28 LAX 11:28 JFK 14:28
    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