来自许多表的聚合数据

时间:2012-07-23 18:09:28

标签: sql postgresql multiple-tables

鉴于我在DB中有3个表,它们包含来自同一来源的不同数据片段。 所有表都具有非常相似的结构:

id | parent_id | timestamp | contents 

每个表都有parent_id(多个记录关系的父对象)和时间戳索引。

我需要按时间排序访问这些数据。目前我使用下一个查询:

prepare query3(bigint) as 
select id, timestamp, contents, filter from 
 (select t1.id, t1.timestamp, t1.contents, 'filter1' as filter from table1 t1 
where t1.parent_id = $1
  union select t2.id, t2.timestamp, t2.contents, 'filter2' as filter from table2 t2 
where t2.parent_id = $1
  union select t3.id, t3.timestamp, t3.contents, 'filter3' as filter from table3 t3 
where t3.parent_id = $1 
) table_alias order by timestamp;

由于每个表中有相当多的数据,因此每次执行此查询时需要2到3分钟。根据解释:650000行和Sort Method: external merge Disk: 186592kB

有没有办法在不更改架构的情况下优化检索执行时间,但是构建更有效的查询或创建特定索引?

更新添加完整解释分析结果在这里。在这种情况下,查询中有4个表,但我相信在这种情况下3和4之间没有太大区别。

"Sort  (cost=83569.28..83959.92 rows=156258 width=80) (actual time=2288.871..2442.318 rows=639225 loops=1)"
"  Sort Key: t1.timestamp"
"  Sort Method: external merge  Disk: 186592kB"
"  ->  Unique  (cost=52685.43..54638.65 rows=156258 width=154) (actual time=1572.274..1885.966 rows=639225 loops=1)"
"    ->  Sort  (cost=52685.43..53076.07 rows=156258 width=154) (actual time=1572.273..1737.041 rows=639225 loops=1)"
"    Sort Key: t1.id, t1.timestamp, t1.contents, ('table1'::text)"
"    Sort Method: external merge  Disk: 186624kB"
"      ->  Append  (cost=0.00..14635.39 rows=156258 width=154) (actual time=0.070..447.375 rows=639225 loops=1)"
"        ->  Index Scan using table1_parent_id on table1 t1  (cost=0.00..285.08 rows=5668 width=109) (actual time=0.068..5.993 rows=9385 loops=1)"
"        Index Cond: (parent_id = $1)"
"        ->  Index Scan using table2_parent_id on table2 t2  (cost=0.00..11249.13 rows=132927 width=168) (actual time=0.063..306.567 rows=589056 loops=1)"
"        Index Cond: (parent_id = $1)"
"        ->  Index Scan using table3_parent_id on table3 t3  (cost=0.00..957.18 rows=4693 width=40) (actual time=25.234..82.381 rows=20176 loops=1)"
"        Index Cond: (parent_id = $1)"
"        ->  Index Scan using table4_parent_id_idx on table4 t4  (cost=0.00..581.42 rows=12970 width=76) (actual time=0.029..5.894 rows=20608 loops=1)"
"        Index Cond: (parent_id = $1)"
"Total runtime: 2489.569 ms"

1 个答案:

答案 0 :(得分:1)

你的大部分时间都是因为消除了联盟的重复。请改用union all:

select id, timestamp, contents, filter
from  ((select t1.id, t1.timestamp, t1.contents, 'filter1' as filter
        from table1 t1 
        where t1.parent_id = $1
       )
       union all
       (select t2.id, t2.timestamp, t2.contents, 'filter2' as filter
        from table2 t2 
        where t2.parent_id = $1
       )
       union all
       (select t3.id, t3.timestamp, t3.contents, 'filter3' as filter
        from table3 t3 
        where t3.parent_id = $1 
       )
      ) table_alias
order by timestamp;

为了使其更有效,您应该在三个表中的每个表上都有一个parent_id索引。随着这些变化,它应该非常紧凑。