Postgres版本:PostgreSQL 9.6.4 on x86_64-alpine-linux-musl, compiled by gcc (Alpine 6.3.0) 6.3.0, 64-bit
我们在1个表上有INNER JOIN
和其他18个问题,我想知道在哪里修改它(我们必须隐藏我们的实际表名,但我有在EXPLAIN中也替换了这个。)
id
)versionId
引用version(id)
和anotherColumn
,这是uuid
)unique(versionId, anotherColumn)
,因此正在使用多列索引的左侧部分(请参阅下面的EXPLAIN ANALYZE
)EXPLAIN (ANALYZE,BUFFERS)
SELECT Version.*,
table6.versionid AS table6_versionId,
<OTHER COLUMNS>
table7.versionid AS table7_versionId,
<OTHER COLUMNS>
FROM asm.version Version
INNER JOIN table6 table6 ON table6.versionId = Version.id
INNER JOIN table7 table7 ON table7.versionId = Version.id
<REPEATED FOR ALL 18 TABLES>
WHERE Version.id = 30
EXPLAIN ANALYZE
) EXPLAIN
中,我们可以看到在某些情况下返回的实际行被高估了(除非我的数学很糟糕)。当连接4个表,每个表有2行时,我们应该返回8行,但计划器返回16(例如(table10(2)+table5(2))+(table7(2)+table9(2)) = 16
。这是否是指向更深层问题的指针? 'Nested Loop (cost=75.20..250907.21 rows=18874368 width=2545) (actual time=1.337..2069.413 rows=262144 loops=1)'
' Buffers: shared hit=38'
' -> Nested Loop (cost=58.55..14939.23 rows=1179648 width=1841) (actual time=1.160..126.443 rows=16384 loops=1)'
' Buffers: shared hit=30'
' -> Nested Loop (cost=29.36..106.51 rows=1536 width=921) (actual time=0.347..2.167 rows=128 loops=1)'
' Buffers: shared hit=16'
' -> Nested Loop (cost=16.83..51.92 rows=48 width=585) (actual time=0.244..0.583 rows=16 loops=1)'
' Buffers: shared hit=10'
' -> Nested Loop (cost=8.50..30.44 rows=8 width=281) (actual time=0.155..0.244 rows=4 loops=1)'
' Buffers: shared hit=6'
' -> Bitmap Heap Scan on table5 (cost=4.18..12.64 rows=4 width=80) (actual time=0.046..0.058 rows=2 loops=1)'
' Recheck Cond: (versionid = 30)'
' Heap Blocks: exact=1'
' Buffers: shared hit=2'
' -> Bitmap Index Scan on table5_pkey (cost=0.00..4.18 rows=4 width=0) (actual time=0.028..0.028 rows=2 loops=1)'
' Index Cond: (versionid = 30)'
' Buffers: shared hit=1'
' -> Materialize (cost=4.32..17.70 rows=2 width=201) (actual time=0.052..0.075 rows=2 loops=2)'
' Buffers: shared hit=4'
' -> Nested Loop (cost=4.32..17.69 rows=2 width=201) (actual time=0.088..0.115 rows=2 loops=1)'
' Buffers: shared hit=4'
' -> Index Scan using version_pkey on version (cost=0.15..8.17 rows=1 width=41) (actual time=0.038..0.043 rows=1 loops=1)'
' Index Cond: (id = 30)'
' Buffers: shared hit=2'
' -> Bitmap Heap Scan on table10 (cost=4.16..9.50 rows=2 width=160) (actual time=0.042..0.053 rows=2 loops=1)'
' Recheck Cond: (versionid = 30)'
' Heap Blocks: exact=1'
' Buffers: shared hit=2'
' -> Bitmap Index Scan on table10_pkey (cost=0.00..4.16 rows=2 width=0) (actual time=0.011..0.011 rows=2 loops=1)'
' Index Cond: (versionid = 30)'
' Buffers: shared hit=1'
' -> Materialize (cost=8.34..20.90 rows=6 width=304) (actual time=0.023..0.056 rows=4 loops=4)'
' Buffers: shared hit=4'
' -> Nested Loop (cost=8.34..20.87 rows=6 width=304) (actual time=0.071..0.145 rows=4 loops=1)'
' Buffers: shared hit=4'
' -> Bitmap Heap Scan on table7 (cost=4.17..11.28 rows=3 width=128) (actual time=0.031..0.042 rows=2 loops=1)'
' Recheck Cond: (versionid = 30)'
' Heap Blocks: exact=1'
' Buffers: shared hit=2'
' -> Bitmap Index Scan on table7_pkey (cost=0.00..4.17 rows=3 width=0) (actual time=0.015..0.015 rows=2 loops=1)'
' Index Cond: (versionid = 30)'
' Buffers: shared hit=1'
' -> Materialize (cost=4.16..9.51 rows=2 width=176) (actual time=0.018..0.032 rows=2 loops=2)'
' Buffers: shared hit=2'
' -> Bitmap Heap Scan on table9 (cost=4.16..9.50 rows=2 width=176) (actual time=0.023..0.035 rows=2 loops=1)'
' Recheck Cond: (versionid = 30)'
' Heap Blocks: exact=1'
' Buffers: shared hit=2'
' -> Bitmap Index Scan on table9_pkey (cost=0.00..4.16 rows=2 width=0) (actual time=0.008..0.008 rows=2 loops=1)'
' Index Cond: (versionid = 30)'
' Buffers: shared hit=1'
' -> Materialize (cost=12.53..35.46 rows=32 width=336) (actual time=0.008..0.048 rows=8 loops=16)'
' Buffers: shared hit=6'
' -> Nested Loop (cost=12.53..35.30 rows=32 width=336) (actual time=0.090..0.261 rows=8 loops=1)'
' Buffers: shared hit=6'
' -> Nested Loop (cost=8.34..22.25 rows=8 width=256) (actual time=0.058..0.128 rows=4 loops=1)'
' Buffers: shared hit=4'
' -> Bitmap Heap Scan on table4 (cost=4.18..12.64 rows=4 width=80) (actual time=0.021..0.033 rows=2 loops=1)'
' Recheck Cond: (versionid = 30)'
' Heap Blocks: exact=1'
' Buffers: shared hit=2'
' -> Bitmap Index Scan on table4_pkey (cost=0.00..4.18 rows=4 width=0) (actual time=0.011..0.011 rows=2 loops=1)'
' Index Cond: (versionid = 30)'
' Buffers: shared hit=1'
' -> Materialize (cost=4.16..9.51 rows=2 width=176) (actual time=0.015..0.029 rows=2 loops=2)'
' Buffers: shared hit=2'
' -> Bitmap Heap Scan on table6 (cost=4.16..9.50 rows=2 width=176) (actual time=0.023..0.034 rows=2 loops=1)'
' Recheck Cond: (versionid = 30)'
' Heap Blocks: exact=1'
' Buffers: shared hit=2'
' -> Bitmap Index Scan on table6_pkey (cost=0.00..4.16 rows=2 width=0) (actual time=0.008..0.008 rows=2 loops=1)'
' Index Cond: (versionid = 30)'
' Buffers: shared hit=1'
' -> Materialize (cost=4.18..12.66 rows=4 width=80) (actual time=0.008..0.018 rows=2 loops=4)'
' Buffers: shared hit=2'
' -> Bitmap Heap Scan on table11 (cost=4.18..12.64 rows=4 width=80) (actual time=0.019..0.029 rows=2 loops=1)'
' Recheck Cond: (versionid = 30)'
' Heap Blocks: exact=1'
' Buffers: shared hit=2'
' -> Bitmap Index Scan on table11_pkey (cost=0.00..4.18 rows=4 width=0) (actual time=0.008..0.008 rows=2 loops=1)'
' Index Cond: (versionid = 30)'
' Buffers: shared hit=1'
' -> Materialize (cost=29.19..89.04 rows=768 width=920) (actual time=0.009..0.314 rows=128 loops=128)'
' Buffers: shared hit=14'
' -> Nested Loop (cost=29.19..85.20 rows=768 width=920) (actual time=0.793..2.118 rows=128 loops=1)'
' Buffers: shared hit=14'
' -> Nested Loop (cost=12.53..35.30 rows=32 width=336) (actual time=0.603..0.742 rows=8 loops=1)'
' Buffers: shared hit=6'
' -> Nested Loop (cost=8.34..22.25 rows=8 width=256) (actual time=0.554..0.607 rows=4 loops=1)'
' Buffers: shared hit=4'
' -> Bitmap Heap Scan on table1 (cost=4.18..12.64 rows=4 width=80) (actual time=0.503..0.510 rows=2 loops=1)'
' Recheck Cond: (versionid = 30)'
' Heap Blocks: exact=1'
' Buffers: shared hit=2'
' -> Bitmap Index Scan on table1_pkey (cost=0.00..4.18 rows=4 width=0) (actual time=0.483..0.483 rows=2 loops=1)'
' Index Cond: (versionid = 30)'
' Buffers: shared hit=1'
' -> Materialize (cost=4.16..9.51 rows=2 width=176) (actual time=0.022..0.032 rows=2 loops=2)'
' Buffers: shared hit=2'
' -> Bitmap Heap Scan on table12 (cost=4.16..9.50 rows=2 width=176) (actual time=0.030..0.036 rows=2 loops=1)'
' Recheck Cond: (versionid = 30)'
' Heap Blocks: exact=1'
' Buffers: shared hit=2'
' -> Bitmap Index Scan on table12_pkey (cost=0.00..4.16 rows=2 width=0) (actual time=0.016..0.016 rows=2 loops=1)'
' Index Cond: (versionid = 30)'
' Buffers: shared hit=1'
' -> Materialize (cost=4.18..12.66 rows=4 width=80) (actual time=0.012..0.019 rows=2 loops=4)'
' Buffers: shared hit=2'
' -> Bitmap Heap Scan on table2 (cost=4.18..12.64 rows=4 width=80) (actual time=0.034..0.041 rows=2 loops=1)'
' Recheck Cond: (versionid = 30)'
' Heap Blocks: exact=1'
' Buffers: shared hit=2'
' -> Bitmap Index Scan on table2_pkey (cost=0.00..4.18 rows=4 width=0) (actual time=0.013..0.013 rows=2 loops=1)'
' Index Cond: (versionid = 30)'
' Buffers: shared hit=1'
' -> Materialize (cost=16.66..40.36 rows=24 width=584) (actual time=0.025..0.096 rows=16 loops=8)'
' Buffers: shared hit=8'
' -> Nested Loop (cost=16.66..40.24 rows=24 width=584) (actual time=0.175..0.415 rows=16 loops=1)'
' Buffers: shared hit=8'
' -> Nested Loop (cost=8.34..20.87 rows=6 width=240) (actual time=0.069..0.116 rows=4 loops=1)'
' Buffers: shared hit=4'
' -> Bitmap Heap Scan on table3 (cost=4.17..11.28 rows=3 width=96) (actual time=0.027..0.033 rows=2 loops=1)'
' Recheck Cond: (versionid = 30)'
' Heap Blocks: exact=1'
' Buffers: shared hit=2'
' -> Bitmap Index Scan on table3_pkey (cost=0.00..4.17 rows=3 width=0) (actual time=0.012..0.012 rows=2 loops=1)'
' Index Cond: (versionid = 30)'
' Buffers: shared hit=1'
' -> Materialize (cost=4.16..9.51 rows=2 width=144) (actual time=0.019..0.027 rows=2 loops=2)'
' Buffers: shared hit=2'
' -> Bitmap Heap Scan on table14 (cost=4.16..9.50 rows=2 width=144) (actual time=0.027..0.031 rows=2 loops=1)'
' Recheck Cond: (versionid = 30)'
' Heap Blocks: exact=1'
' Buffers: shared hit=2'
' -> Bitmap Index Scan on table14_pkey (cost=0.00..4.16 rows=2 width=0) (actual time=0.013..0.013 rows=2 loops=1)'
' Index Cond: (versionid = 30)'
' Buffers: shared hit=1'
' -> Materialize (cost=8.33..19.08 rows=4 width=344) (actual time=0.026..0.049 rows=4 loops=4)'
' Buffers: shared hit=4'
' -> Nested Loop (cost=8.33..19.06 rows=4 width=344) (actual time=0.091..0.140 rows=4 loops=1)'
' Buffers: shared hit=4'
' -> Bitmap Heap Scan on table13 (cost=4.16..9.50 rows=2 width=160) (actual time=0.051..0.055 rows=2 loops=1)'
' Recheck Cond: (versionid = 30)'
' Heap Blocks: exact=1'
' Buffers: shared hit=2'
' -> Bitmap Index Scan on table13_pkey (cost=0.00..4.16 rows=2 width=0) (actual time=0.030..0.030 rows=2 loops=1)'
' Index Cond: (versionid = 30)'
' Buffers: shared hit=1'
' -> Materialize (cost=4.16..9.51 rows=2 width=184) (actual time=0.018..0.026 rows=2 loops=2)'
' Buffers: shared hit=2'
' -> Bitmap Heap Scan on table8 (cost=4.16..9.50 rows=2 width=184) (actual time=0.026..0.030 rows=2 loops=1)'
' Recheck Cond: (versionid = 30)'
' Heap Blocks: exact=1'
' Buffers: shared hit=2'
' -> Bitmap Index Scan on table8_pkey (cost=0.00..4.16 rows=2 width=0) (actual time=0.008..0.008 rows=2 loops=1)'
' Index Cond: (versionid = 30)'
' Buffers: shared hit=1'
' -> Materialize (cost=16.65..38.42 rows=16 width=704) (actual time=0.002..0.037 rows=16 loops=16384)'
' Buffers: shared hit=8'
' -> Nested Loop (cost=16.65..38.34 rows=16 width=704) (actual time=0.161..0.389 rows=16 loops=1)'
' Buffers: shared hit=8'
' -> Nested Loop (cost=8.33..19.06 rows=4 width=288) (actual time=0.078..0.128 rows=4 loops=1)'
' Buffers: shared hit=4'
' -> Bitmap Heap Scan on table15 (cost=4.16..9.50 rows=2 width=144) (actual time=0.022..0.027 rows=2 loops=1)'
' Recheck Cond: (versionid = 30)'
' Heap Blocks: exact=1'
' Buffers: shared hit=2'
' -> Bitmap Index Scan on table15_pkey (cost=0.00..4.16 rows=2 width=0) (actual time=0.011..0.011 rows=2 loops=1)'
' Index Cond: (versionid = 30)'
' Buffers: shared hit=1'
' -> Materialize (cost=4.16..9.51 rows=2 width=144) (actual time=0.026..0.036 rows=2 loops=2)'
' Buffers: shared hit=2'
' -> Bitmap Heap Scan on table16 (cost=4.16..9.50 rows=2 width=144) (actual time=0.043..0.048 rows=2 loops=1)'
' Recheck Cond: (versionid = 30)'
' Heap Blocks: exact=1'
' Buffers: shared hit=2'
' -> Bitmap Index Scan on table16_pkey (cost=0.00..4.16 rows=2 width=0) (actual time=0.016..0.016 rows=2 loops=1)'
' Index Cond: (versionid = 30)'
' Buffers: shared hit=1'
' -> Materialize (cost=8.33..19.08 rows=4 width=416) (actual time=0.021..0.043 rows=4 loops=4)'
' Buffers: shared hit=4'
' -> Nested Loop (cost=8.33..19.06 rows=4 width=416) (actual time=0.068..0.116 rows=4 loops=1)'
' Buffers: shared hit=4'
' -> Bitmap Heap Scan on table17 (cost=4.16..9.50 rows=2 width=176) (actual time=0.027..0.031 rows=2 loops=1)'
' Recheck Cond: (versionid = 30)'
' Heap Blocks: exact=1'
' Buffers: shared hit=2'
' -> Bitmap Index Scan on table17_pkey (cost=0.00..4.16 rows=2 width=0) (actual time=0.014..0.014 rows=2 loops=1)'
' Index Cond: (versionid = 30)'
' Buffers: shared hit=1'
' -> Materialize (cost=4.16..9.51 rows=2 width=240) (actual time=0.018..0.027 rows=2 loops=2)'
' Buffers: shared hit=2'
' -> Bitmap Heap Scan on table18 (cost=4.16..9.50 rows=2 width=240) (actual time=0.027..0.032 rows=2 loops=1)'
' Recheck Cond: (versionid = 30)'
' Heap Blocks: exact=1'
' Buffers: shared hit=2'
' -> Bitmap Index Scan on table18_pkey (cost=0.00..4.16 rows=2 width=0) (actual time=0.016..0.016 rows=2 loops=1)'
' Index Cond: (versionid = 30)'
' Buffers: shared hit=1'
'Planning time: 6.766 ms'
'Execution time: 2653.397 ms'