我需要在表中添加3列具有默认值的列,其中Oracle 11g中有17亿条记录。在最短的时间内完成这项工作的最佳选择是什么?
答案 0 :(得分:0)
添加列值时无法提升性能。如果您只选择行的子集,那么您可以尝试进行优化。
如果列真正具有DEFAULT
值,则当您INSERT
并且(a)未在列列表中命名列或(b)时,Oracle将实际放置该值)将其值指定为DEFAULT
:
CREATE TABLE MyTable (
Col1 NUMBER(5) DEFAULT 10 NOT NULL,
Col2 NUMBER(5) DEFAULT 20 NOT NULL,
Col3 NUMBER(5) DEFAULT 30 NOT NULL
);
INSERT INTO MyTable VALUES (DEFAULT, DEFAULT, DEFAULT);
INSERT INTO MyTable (Col2, Col3) VALUES (21, 31);
INSERT INTO MyTable (Col2, Col3) VALUES (22, DEFAULT);
INSERT INTO MyTable (Col1) VALUES (11);
SELECT * FROM MyTable;
COL1 COL2 COL3
----- ----- -----
10 20 30 <-- VALUES (DEFAULT, DEFAULT, DEFAULT)
10 21 31 <-- (Col2, Col3) VALUES (21, 31); Col1 defaults because not named
10 22 30 <-- (Col2, Col3) VALUES (22, DEFAULT); Col1 defaults because not named
11 20 30 <-- (Col1) VALUES (11); Col2 and Col3 default because not named
所以没有必要检查NULL
,如果这是你担心的:
SELECT Col1, Col2, Col3, Col1+Col2+Col3 AS TotCols
FROM MyTable;
COL1 COL2 COL3 TOTCOLS
---------- ---------- ---------- ----------
10 20 30 60
10 21 31 62
10 22 30 62
11 20 30 61
如果您的问题是如何优化行的子集,请添加更多信息。