zoukankan      html  css  js  c++  java
  • Oracle: Benefits and consequences of the NOLOGGING option

    Posted on by Carlos Acosta

    I still find confusion out there about the LOGGING and NOLOGGING clauses when performing DML and DDL operations, the reality is that the NOLOGGING clause will work only on particular conditions, but all regular inserts,updates and deletes will still log the operations.

    The benefits of the NOLOGGING option are:

    • Will save disk space when the archive option is enabled.
    • Will largely reduce I/O on the redologs.
    • Will reduce the time it takes to complete the operation.

    Please note that NOLOGGING operations will only reduce -not eliminate- the logging.

    Lets see an example -

    01 -- First: we create an empty table with the nologging clause
    02 SQL> create table logging_example nologging as select * from dba_objects where 1=2;
    03
    04 Table created.
    05
    06 --Now let's enable the statistics and perform a couple of tests:
    07
    08 SQL> set autotrace on statistics
    09 SQL> set timing on
    10
    11 -- insert the records the traditional way
    12 SQL> alter system flush buffer_cache; --clean the cache to compare the speeds in equal conditions
    13
    14 System altered.
    15
    16 Elapsed: 00:00:01.49
    17
    18 SQL> insert into logging_example select * from dba_objects;
    19
    20 50864 rows created.
    21
    22 Elapsed: 00:00:01.59
    23
    24 Statistics
    25 ----------------------------------------------------------
    26 0 recursive calls
    27 5250 db block gets
    28 6766 consistent gets
    29 982 physical reads
    30 5636712 redo size --without the APPEND hint
    31 670 bytes sent via SQL*Net to client
    32 586 bytes received via SQL*Net from client
    33 3 SQL*Net roundtrips to/from client
    34 1 sorts (memory)
    35 0 sorts (disk)
    36 50864 rows processed
    37
    38 -- insert the records with the APPEND hint (nologging)
    39 SQL> alter system flush buffer_cache; --clean the cache to compare the speeds in equal conditions
    40
    41 System altered.
    42
    43 Elapsed: 00:00:01.06
    44
    45 SQL> insert /*+ append */ into logging_example select * from dba_objects;
    46
    47 50864 rows created.
    48
    49 Elapsed: 00:00:00.59
    50
    51 Statistics
    52 ----------------------------------------------------------
    53 0 recursive calls
    54 743 db block gets
    55 5374 consistent gets
    56 944 physical reads
    57 2200 redo size --with the APPEND hint
    58 654 bytes sent via SQL*Net to client
    59 604 bytes received via SQL*Net from client
    60 3 SQL*Net roundtrips to/from client
    61 1 sorts (memory)
    62 0 sorts (disk)
    63 50864 rows processed

    We can see that there is a big difference on the redo size generated by each insert, there are many post and articles on the internet that show the speed benefits when using the NOLOGGING option, but here I mainly want to clarify that a regular insert (no APPEND hint) will still generate redologs even if the table have been created with the NOLOGGING option.

    What happens to the data after a restore when a nologging operation was performed on it?

    I will present some scenarios to show the consequences when we need to perform a restore after a nologging transaction, this way we will know what to expect and we can better prepare ourselves in case of a disaster.

    I took a full database backup, now I will create several tables with different options to see what happens after a restore, you might see some surprises here!

    Scenarios:

    • Table “create as select” with the nologging option (table_ctas_nologging).
    • Regular table “create as select” (table_ctas_logging)
    • A nologging table created empty, and a regular (logging) insert (table_ctas_nologging_insert)
    • Table created with nologging, then two inserts, one with and one without logging (table_insert_mixed)
    • Regular logging table, with a nologging index (table_ctas_index_nologging)
    01 SQL> create table table_ctas_nologging nologging as select * from dba_objects;
    02
    03 Table created.
    04
    05 SQL> create table table_ctas_logging as select * from dba_objects;
    06
    07 Table created.
    08
    09 SQL> create table table_ctas_nologging_insert nologging as select * from dba_objects where 1=2;
    10
    11 Table created.
    12
    13 SQL> insert into table_ctas_nologging_insert select * from dba_objects;
    14
    15 50864 rows created.
    16
    17 SQL> commit;
    18
    19 Commit complete.
    20
    21 SQL> create table table_insert_mixed nologging as select * from dba_objects where 1=2;
    22
    23 Table created.
    24
    25 SQL> insert into table_insert_mixed select * from dba_objects;
    26
    27 50866 rows created.
    28
    29 SQL> insert into table_insert_mixed select /*+ append */ * from dba_objects;
    30
    31 50866 rows created.
    32
    33 SQL> commit;
    34
    35 Commit complete.
    36
    37 SQL> select count(*) from table_insert_mixed;
    38
    39 COUNT(*)
    40 ----------
    41 101732
    42
    43 SQL> create table table_ctas_index_nologging as select * from dba_objects;
    44
    45 Table created.
    46
    47 SQL> create index IDXNOLOG on table_ctas_index_nologging (object_id) nologging;
    48
    49 Index created.

    Now I will shutdown the database and restore the tablespace from the backup.
    Next is an extract from RMAN

    01 rman target /
    02
    03 Recovery Manager: Release 10.2.0.4.0 - Production on Wed Aug 25 17:32:20 2010
    04
    05 Copyright (c) 1982, 2007, Oracle. All rights reserved.
    06
    07 connected to target database: ORCL (DBID=1247573001)
    08
    09 RMAN> shutdown immediate
    10
    11 using target database control file instead of recovery catalog
    12 database closed
    13 database dismounted
    14 Oracle instance shut down
    15
    16 RMAN> startup mount;
    17
    18 Oracle instance started
    19 database mounted
    20
    21 Total System Global Area 285212672 bytes
    22
    23 Fixed Size 1267068 bytes
    24 Variable Size 155191940 bytes
    25 Database Buffers 125829120 bytes
    26 Redo Buffers 2924544 bytes
    27
    28 RMAN> restore tablespace users;
    29
    30 Starting restore at 25-AUG-10
    31 using target database control file instead of recovery catalog
    32 allocated channel: ORA_DISK_1
    33 channel ORA_DISK_1: sid=152 devtype=DISK
    34
    35 channel ORA_DISK_1: starting datafile backupset restore
    36 channel ORA_DISK_1: specifying datafile(s) to restore from backup set
    37 restoring datafile 00004 to +DATA/orcl/datafile/users.259.719792191
    38 channel ORA_DISK_1: reading from backup piece +DATA/orcl/backupset/2010_08_25/nnndf0_tag20100825t171657_0.272.727982219
    39 channel ORA_DISK_1: restored backup piece 1
    40 piece handle=+DATA/orcl/backupset/2010_08_25/nnndf0_tag20100825t171657_0.272.727982219 tag=TAG20100825T171657
    41 channel ORA_DISK_1: restore complete, elapsed time: 00:00:05
    42 Finished restore at 25-AUG-10
    43
    44 RMAN> recover tablespace users;
    45
    46 Starting recover at 25-AUG-10
    47 using channel ORA_DISK_1
    48
    49 starting media recovery
    50 media recovery complete, elapsed time: 00:00:05
    51
    52 Finished recover at 25-AUG-10
    53
    54 RMAN> alter database open;
    55
    56 database opened

    Now lets see the status of the tables:

    1 SQL> select count(*) from table_ctas_nologging ;
    2 select count(*) from table_ctas_nologging
    3 *
    4 ERROR at line 1:
    5 ORA-01578: ORACLE data block corrupted (file # 4, block # 404)
    6 ORA-01110: data file 4: '+DATA/orcl/datafile/users.259.719792191'
    7 ORA-26040: Data block was loaded using the NOLOGGING option

    That doesn’t look good, lets see the next table

    1 SQL> select count(*) from table_ctas_logging ;
    2
    3 COUNT(*)
    4 ----------
    5 50863

    Good, no problem here, the next scenario is more interesting, the table was created with the NOLOGGING option, but the inserts were done without the APPEND hint

    1 SQL> select count (*) from table_ctas_nologging_insert;
    2
    3 COUNT(*)
    4 ----------
    5 50864

    Good, no problem here, now let’s see our table with half data inserted with logging and half with nologging

    1 SQL> select count(*) from table_insert_mixed;
    2 select count(*) from table_insert_mixed
    3 *
    4 ERROR at line 1:
    5 ORA-01578: ORACLE data block corrupted (file # 4, block # 4363)
    6 ORA-01110: data file 4: '+DATA/orcl/datafile/users.259.719792191'
    7 ORA-26040: Data block was loaded using the NOLOGGING option

    Wow, the whole table is unredable!

    Now lets see the table with the NOLOGGING index .

    1 <pre>SQL> select count(*) from table_ctas_index_nologging;
    2
    3 COUNT(*)
    4 ----------
    5 50865

    Ok, thats nice, the table is accessible, but what happend if we try to use the index?

    1 SQL> select object_id from table_ctas_index_nologging where object_id=1;
    2 select object_id from table_ctas_index_nologging where object_id=1
    3 *
    4 ERROR at line 1:
    5 ORA-01578: ORACLE data block corrupted (file # 4, block # 2821)
    6 ORA-01110: data file 4: '+DATA/orcl/datafile/users.259.719792191'
    7 ORA-26040: Data block was loaded using the NOLOGGING option

    I tried to rebuil the index but I was still getting the same error message, at the end I was forced to drop it and recreate it.

    Conclusions:

    • Use the NOLOGGING option only on temporary/working/staging tables.
    • Always perform a backup after a NOLOGGING operation.
    • Unless explicitly indicated, DDLs like CTAS and DMLs like inserts will log all operations.

    FROM ORACLE DOCUMENTATION:

    NOLOGGING is supported in only a subset of the locations that support LOGGING. Only the following operations support the NOLOGGING mode:

    DML:

    • Direct-path INSERT (serial or parallel) resulting either from an INSERT or a MERGE statement. NOLOGGING is not applicable to any UPDATE operations resulting from the MERGE statement.
    • Direct Loader (SQL*Loader)

    DDL:

    • CREATE TABLEAS SELECT
    • CREATE TABLELOB_storage_clauseLOB_parametersNOCACHE | CACHE READS
    • ALTER TABLELOB_storage_clauseLOB_parametersNOCACHE | CACHE READS (to specify logging of newly created LOB columns)
    • ALTER TABLEmodify_LOB_storage_clausemodify_LOB_parametersNOCACHE | CACHE READS (to change logging of existing LOB columns)
    • ALTER TABLEMOVE
    • ALTER TABLE… (all partition operations that involve data movement)
      • ALTER TABLEADD PARTITION (hash partition only)
      • ALTER TABLEMERGE PARTITIONS
      • ALTER TABLESPLIT PARTITION
      • ALTER TABLEMOVE PARTITION
      • ALTER TABLEMODIFY PARTITIONADD SUBPARTITION
      • ALTER TABLEMODIFY PARTITIONCOALESCE SUBPARTITION
    • CREATE INDEX
    • ALTER INDEXREBUILD
    • ALTER INDEXREBUILD [SUB]PARTITION
    • ALTER INDEXSPLIT PARTITION

    For objects other than LOBs, if you omit this clause, then the logging attribute of the object defaults to the logging attribute of the tablespace in which it resides.

    For LOBs, if you omit this clause, then:

    • If you specify CACHE, then LOGGING is used (because you cannot have CACHE NOLOGGING).
    • If you specify NOCACHE or CACHE READS, then the logging attribute defaults to the logging attribute of the tablespace in which it resides.

    NOLOGGING does not apply to LOBs that are stored internally (in the table with row data). If you specify NOLOGGING for LOBs with values less than 4000 bytes and you have not disabled STORAGE IN ROW, then Oracle ignores the NOLOGGING specification and treats the LOB data the same as other table data.

  • 相关阅读:
    微信公众账号报错 返回码说明
    2013与2014之流水
    【Leetcode刷题】字符串模式匹配算法知多少
    【数据库测试工具】认识Sysbench
    【论文笔记】《基于深度学习的中文命名实体识别研究》阅读笔记
    分享一份关于Hadoop2.2.0集群环境搭建文档
    【Java实践】Kettle从一次实验说起
    【资源共享】eBook分享大集合
    java入门知识
    python编程规范系列--建议08~18
  • 原文地址:https://www.cnblogs.com/tracy/p/2116990.html
Copyright © 2011-2022 走看看