什么更有效:几个插入与单个插入与联合

时间:2015-05-06 15:30:42

标签: performance postgresql insert

我在Postgresql中有一个大表(~6M行,41列),如下所示:

id | answer1 | answer2 | answer3 | ... | answer40
1  | xxx     | yyy     | null    | ... | null
2  | xxx     | null    | null    | ... | null
3  | xxx     | null    | zzz     | ... | aaa

请注意,每行中都有许多空列,我只想要那些带有数据

的行

我想将其标准化以获得此结果:

id | answers
1  | xxx
1  | yyy
2  | xxx
3  | xxx
3  | zzz
...
3  | aaa

问题是,什么是更有效/更快,几个插入或单个插入和许多联合?:

选项1

create new_table as 
select id, answer1 from my_table where answer1 is not null
union 
select id, answer2 from my_table where answer2 is not null
union 
select id, answer3 from my_table where answer3 is not null
union ... 

选项2

create new_table as select id, answer1 from my_table where answer1 is not null;
insert into new_table select id, answer2 from my_table where answer2 is not null;
insert into new_table select id, answer3 from my_table where answer3 is not null;
...

选项3:有更好的方法吗?

1 个答案:

答案 0 :(得分:1)

选项2应该更快。

begin-commit块中的所有语句换行,以节省单个提交的时间。

要获得更快的选择,请确保要过滤的列(例如where answer1 is not null)具有索引