當(dāng)需要對(duì)一個(gè)非常大的表INSERT的時(shí)候,會(huì)消耗非常多的資源,因?yàn)閡pdate表的時(shí)候,oracle需要生成 redo log和undo log;此時(shí)最好的解決辦法是用insert, 并且將表設(shè)置為nologging;當(dāng)把表設(shè)為nologging后,并且使用的insert時(shí),速度是最快的,這個(gè)時(shí)候oracle只會(huì)生成最低限度的必須的redo log,而沒(méi)有一點(diǎn)undo信息。如果有可能將index也刪除,重建
1.alter table table_name nologging;
2. insert into table_name select * from xxxx;
對(duì)此有了更好的解決方法:
1、alter table nologging;
注釋:Nologging的設(shè)置跟數(shù)據(jù)庫(kù)的運(yùn)行模式有關(guān)
a、查詢當(dāng)前數(shù)據(jù)庫(kù)的歸檔狀態(tài):
select name,log_mode from v$database;
默認(rèn)為 NOARCHIVELOG 非歸檔
b、nologging在歸檔模式下有效,非歸檔模式nologging不起什么作用
c、為了提高插入的速度,我們可以對(duì)表關(guān)閉寫(xiě)log功能。 SQL 如下:
插入/修改,完數(shù)據(jù)后,再修改表寫(xiě)日志:
sql> alter table table_name LOGGING;
d、沒(méi)有寫(xiě)log, 速度會(huì)塊很多,但是也增加了風(fēng)險(xiǎn),如果出現(xiàn)問(wèn)題就不能恢復(fù)。
2、drop掉索引約束之類的;
注釋:
3、
注釋:
a. 、append 屬于direct insert,歸檔模式下append+table nologging會(huì)大量減少日志,
非歸檔模式append會(huì)大量減少日志,append方式插入只會(huì)產(chǎn)生很少的undo
b、綜合一下吧:一是減少對(duì)空間的搜索;二是有可能減少redolog的產(chǎn)生。所以append方式會(huì)快很多,一般用于大數(shù)據(jù)量的處理
c、建議不要經(jīng)常使用append,這樣表空間會(huì)一直在高水位上,除非你這個(gè)表只插不刪
d、oracle append有什么作用?
請(qǐng)教一下,oracle中append是做什么用的。
insert into table1 select * from table2
在使用了append選項(xiàng)以后,insert數(shù)據(jù)會(huì)直接加到表的最后面,而不會(huì)在表的空閑塊中插入數(shù)據(jù)。
使用append會(huì)增加數(shù)據(jù)插入的速度。
的作用是在表的高水位上分配空間,不再使用表的extent中的空余空間
append 屬于direct insert,歸檔模式下append+table nologging會(huì)大量減少日志,
非歸檔模式append會(huì)大量減少日志,append方式插入只會(huì)產(chǎn)生很少的undo
不去尋找 freelist 中的free block , 直接在table HWM 上面加入數(shù)據(jù)。
4、總結(jié)
測(cè)試證明:
1. 不管哪種模式下append要與nologging方式聯(lián)用才能達(dá)到很好的效果。
2. 非歸檔與歸檔方式,只用NOLOGGING是不起效果的。
3. 非歸檔下append已達(dá)到不錯(cuò)的效果,但不及與nologging的聯(lián)用方式。
4. 歸檔下單append起不到效果。
NOLOGGING插完后最好做個(gè)備份。
另外,如果庫(kù)處在FORCELOGGING模式下,此時(shí)的nologging方式是無(wú)效的。
總結(jié)得出以下3點(diǎn)結(jié)論:
a、非歸檔模式下,只需append就能大量減少redo的產(chǎn)生;歸檔模式下,只有append+nologging才能大量減少redo。
b、insert /*+ append */ 時(shí)會(huì)對(duì)表加鎖(排它鎖),會(huì)阻塞表上的除了select以外所有DML語(yǔ)句;傳統(tǒng)的DML在TM enqueue上使用模式3(row exclusive),其允許其他DML在相同的模式上獲得TM enqueue。但是直接路徑加載在TM enqueue使用模式6(exclusive),這使其他DML在直接路徑加載期間將被阻塞。
c、insert /*+ append */ 直接路徑加載,速度比常規(guī)加載方式快。因?yàn)槭菑腍WM的位置開(kāi)始插入,也許會(huì)造成空間浪費(fèi)。
數(shù)據(jù)庫(kù)版本:
SQL> select * from v$version;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
PL/SQL Release 11.2.0.1.0 - Production
CORE 11.2.0.1.0 Production
TNS for Linux: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
網(wǎng)上說(shuō)測(cè)試時(shí)不要使用auto trace來(lái)查看redo size,這個(gè)值是有偏差的.建議建立一個(gè)視圖:
SQL> create or replace view redo_size
2 as
3 select value
4 from v$mystat, v$statname
5 where v$mystat.statistic# = v$statname.statistic#
6 and v$statname.name = 'redo size';
視圖已創(chuàng)建。
一、非歸檔模式
SQL> archive log list
數(shù)據(jù)庫(kù)日志模式 非存檔模式
自動(dòng)存檔 禁用
存檔終點(diǎn) USE_DB_RECOVERY_FILE_DEST
最早的聯(lián)機(jī)日志序列 95
當(dāng)前日志序列 97
1、nologging表
SQL> create table test1 nologging as select * from dba_objects where 1=0;
表已創(chuàng)建。
SQL> select * from redo_size;
VALUE
----------
25714940
SQL> insert into test1 select * from dba_objects;
已創(chuàng)建72753行。
SQL> select * from redo_size;
VALUE
----------
34216916
SQL> insert /*+ APPEND */ into test1 select * from dba_objects;
已創(chuàng)建72753行。
SQL> select * from redo_size;
VALUE
----------
34231736
SQL> select (34231736-34216916) redo_append , (34216916-25714940) redo_normal from dual;
REDO_APPEND REDO_NORMAL
----------- -----------
14820 8501976
2、logging表:
SQL> create table test2 as select * from dba_objects where 1=0;
表已創(chuàng)建。
SQL> select * from redo_size;
VALUE
----------
34273348
SQL> insert into test2 select * from dba_objects;
已創(chuàng)建72754行。
SQL> select * from redo_size;
VALUE
----------
42775336
SQL> insert /*+ APPEND */ into test2 select * from dba_objects;
已創(chuàng)建72754行。
SQL> select * from redo_size;
VALUE
----------
42790156
SQL> select (42790156-42775336) redo_append , (42775336-34273348) redo_normal from dual;
REDO_APPEND REDO_NORMAL
----------- -----------
14820 8501988
二、歸檔模式下:
SQL> shutdown immediate
數(shù)據(jù)庫(kù)已經(jīng)關(guān)閉。
已經(jīng)卸載數(shù)據(jù)庫(kù)。
ORACLE例程已經(jīng)關(guān)閉。
SQL> startup mount
ORACLE例程已經(jīng)啟動(dòng)。
Total System Global Area 477073408 bytes
Fixed Size 1337324 bytes
Variable Size 293603348 bytes
Database Buffers 176160768 bytes
Redo Buffers 5971968 bytes
數(shù)據(jù)庫(kù)裝載完畢。
SQL> alter database archivelog;
數(shù)據(jù)庫(kù)已更改。
SQL> alter database open;
數(shù)據(jù)庫(kù)已更改。
SQL> archive log list
數(shù)據(jù)庫(kù)日志模式 存檔模式
自動(dòng)存檔 啟用
存檔終點(diǎn) USE_DB_RECOVERY_FILE_DEST
最早的聯(lián)機(jī)日志序列 95
下一個(gè)存檔日志序列 97
當(dāng)前日志序列 97
1、nologging表
SQL> select * from redo_size;
VALUE
----------
17936
SQL> insert into test1 select * from dba_objects;
已創(chuàng)建72754行。
SQL> select * from redo_size;
VALUE
----------
8490972
SQL> insert /*+ APPEND */ into test1 select * from dba_objects;
已創(chuàng)建72754行。
SQL> select * from redo_size;
VALUE
----------
8506164
SQL> select (8506164-8490972) redo_append , (8490972-17936) redo_normal from dual;
REDO_APPEND REDO_NORMAL
----------- -----------
15192 8473036
2、logging表
SQL> select * from redo_size;
VALUE
----------
8506780
SQL> insert into test2 select * from dba_objects;
已創(chuàng)建72754行。
SQL> select * from redo_size;
VALUE
----------
16979516
SQL> insert /*+ APPEND */ into test2 select * from dba_objects;
已創(chuàng)建72754行。
SQL> select * from redo_size;
VALUE
----------
25518172
SQL> select (25518172-16979516) redo_append , (16979516-8506780) redo_normal from dual;
REDO_APPEND REDO_NORMAL
----------- -----------
8538656 8472736
在歸檔模式下,對(duì)于常規(guī)表的insert append產(chǎn)生和insert同樣的redo
此時(shí)的insert append實(shí)際上并不會(huì)有性能提高.
但是此時(shí)的append是生效了的。
三、insert /*+ append */會(huì)阻塞除select以外的DML語(yǔ)句,direct-path insert操作是單獨(dú)一個(gè)事務(wù)。
SQL> select count(*) from test2;
COUNT(*)
----------
291016
SQL> insert into test2 select * from dba_objects;
已創(chuàng)建72754行。
SQL> select count(*) from test2;
COUNT(*)
----------
363770
SQL> insert /*+ APPEND */ into test2 select * from dba_objects;
已創(chuàng)建72754行
同一個(gè)session下:
SQL> select count(*) from test2;
select count(*) from test2
*
第 1 行出現(xiàn)錯(cuò)誤:
ORA-12838: 無(wú)法在并行模式下修改之后讀/修改對(duì)象
SQL> commit;
提交完成。
SQL> select count(*) from test2;
COUNT(*)
----------
436524
SQL> insert /*+ APPEND */ into test2 select * from dba_objects;
已創(chuàng)建72754行。
SQL> shutdown immediate
ORA-01097: 無(wú)法在事務(wù)處理過(guò)程中關(guān)閉 - 請(qǐng)首先提交或回退
SQL> select * from v$mystat where rownum<2;
SID STATISTIC# VALUE
---------- ---------- ----------
224 0 1
SQL> select KADDR,TYPE,LMODE from v$lock where sid=224;
KADDR TY LMODE
---------------- -- ----------
0000000071BAE180 TM 6
0000000070CB11B8 TX 6
另外開(kāi)啟一個(gè)會(huì)話,就會(huì)發(fā)現(xiàn)只能select,其他DML全部阻塞。