CREATE,INSERT INTO& UPDATE vs CREATE&选择

时间:2016-03-02 20:44:10

标签: postgresql

我正在PostgreSQL(Netezza)中编写一个大的sql脚本,我想定义从另一个[ext_table]创建和更新[table]的最佳策略。如您所见,某些字段(bi,i = 1 ... n)是根据使用CASE条件的源字段(ai,i = 1 ... n)计算的。哪个更有效率,为什么?

1)创建,插入然后更新:

CREATE TABLE <table> (a1,a2,a3,...b1,b2,b3...);
INSERT INTO [table] (a1,a2,a3,...) SELECT a1,a2,a3,... FROM [ext_table];
UPDATE [table] SET 
b1=CASE WHEN a1=1 THEN 'Y' ELSE 'N' END,
b2=CASE WHEN a2='A' THEN 1 ELSE 0 END,
b3=CASE WHEN a2='Y' THEN 1 ELSE 0 END;

2)选择时创建并填充:

CREATE TABLE [table] AS (
SELECT a1,a2,a3,...,
(CASE WHEN a1=1 THEN 'Y' ELSE 'N' END) as b1,
(CASE WHEN a2='A' THEN 1 ELSE 0 END) as b2,
(CASE WHEN a2='Y' THEN 1 ELSE 0 END) as b3,
....
FROM [ext_table]
);

0 个答案:

没有答案