fjzcau
作者fjzcau·2014-06-07 22:49
系统运维工程师·金融

Not Logged Initially Tables

字数 4612阅读 3672评论 7赞 1
Not Logged Initially Tables 意思知道,翻译的话感觉有点别扭,就这么着了。

The NOT LOGGED INITIALLY clause of the CREATE TABLE statement allows you to create a
table that will not be logged when an INSERT, UPDATE, DELETE, CREATE INDEX,
ALTER TABLE, or DROP INDEX operation is performed in the same unit of work in which the
CREATE TABLE statement was issued.

db2inst2@sles11:~> db2 get db cfg for zzdb2 |grep -i "log"         
 Log retain for recovery status                          = NO
 User exit for logging status                            = YES
 Catalog cache size (4KB)              (CATALOGCACHE_SZ) = 300
 Log buffer size (4KB)                        (LOGBUFSZ) = 256
 Log file size (4KB)                         (LOGFILSIZ) = 128
 Number of primary log files                (LOGPRIMARY) = 13
 Number of secondary log files               (LOGSECOND) = -1

--SecureCRT session 1
db2 +c -td@
db2 => create table t1 (c1 varchar(100), c2 varchar(100)) NOT LOGGED INITIALLY @

--SecureCRT session 2
db2inst2@sles11:/db2log2> db2 get db cfg for zzdb2 |grep -i "first active"       
 First active log file                                   = S0000045.LOG

--创建表时指定 NOT LOGGED INITIALLY ,commit前不产生日志
--SecureCRT session 1
db2 => begin
declare i int default 0;
while (i<100000) do
insert into t1 values('aaaaaaaaa' || char(i),'bbbbbb'||char(i) );
set i=i+1;
end while;
end @
db2 => COMMIT @
--SecureCRT session 2
db2inst2@sles11:/db2log2> db2 get db cfg for zzdb2 |grep -i "first active"       
 First active log file                                   = S0000045.LOG
 
--commit后,新的插入语句会产生日志
--SecureCRT session 1
db2 => begin
declare i int default 100000;
while (i<200000) do
insert into t1 values('aaaaaaaaa' || char(i),'bbbbbb'||char(i) );
set i=i+1;
end while;
end @
db2 => COMMIT @

--SecureCRT session 2
db2inst2@sles11:/db2log2> db2 get db cfg for zzdb2 |grep -i "first active"       
 First active log file                                   = S0000058.LOG
 
--手动使 NOT LOGGED INITIALLY 生效,在 commti 之前,不产生日志
--SecureCRT session 1
db2 => ALTER TABLE t1 ACTIVATE NOT LOGGED INITIALLY @
db2 => begin
declare i int default 200000;
while (i<300000) do
insert into t1 values('aaaaaaaaa' || char(i),'bbbbbb'||char(i) );
set i=i+1;
end while;
end@
db2 => commit@

--SecureCRT session 2
db2inst2@sles11:/db2log2> db2 get db cfg for zzdb2 |grep -i "first active"       
 First active log file                                   = S0000059.LOG
 
Any operation from the CREATE TABLE statement until the first COMMIT is not logged. Once
the COMMIT is issued, any subsequent operation is logged. For this example, the INSERT and
UPDATE statements after the first COMMIT are logged.

After creating the table as NOT LOGGED INITIALLY, if you would like to turn off logging
temporarily again, you can use the ALTER TABLE statement with the ACTIVATE NOT
LOGGED INITIALLY clause, as shown in the example. Any operations between the ALTER
TABLE and the second COMMIT are not logged.

--清空表
ALTER TABLE t1 ACTIVATE NOT LOGGED INITIALLY WITH EMPTY TABLE


如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续创作!

1

添加新评论7 条评论

fjzcaufjzcau系统运维工程师金融
2014-06-14 08:31
不记日志,利弊都有。
The advantage of using the NOT LOGGED INITIALLY parameter is that any changes made on a table (including insert, delete, update, or create index operations) in the same unit of work that creates the table will not be logged. This not only reduces the logging that is done, but can also increase the performance of your application. You can achieve the same result for existing tables by using the ALTER TABLE statement with the NOT LOGGED INITIALLY parameter.
fjzcaufjzcau系统运维工程师金融
2014-06-14 08:25
http://www-01.ibm.com/support/knowledgecenter/SSEPGG_9.7.0/com.ibm.db2.luw.messages.sql.doc/doc/msql01477n.html?cp=SSEPGG_9.7.0%2F2-6-27-2-465&lang=en

SQL1477N 报错解释:

An attempt was made to access a table where one of its objects is not accessible.
The table may not be accessible because of one of the following reasons:

1) The table had NOT LOGGED INITIALLY activated when the unit of work was rolled back.
2) The table is a partitioned created temporary table or declared temporary table and
  one or more database partitions failed since the temporary table was instantiated or declared.
3) ROLLFORWARD encountered the activation of NOT LOGGED INITIALLY on this table or a NONRECOVERABLE load on this table.


SQL1477N 解决方案:

One of the following actions can be taken.

1) If the object is a table and it had NOT LOGGED INITIALLY activated,
drop the table. If this table is required, re-create it.
2) If the object is a data partition, detach it from the table.
If this data partition is required, add a new one.
3) If the object is a non-partitioned index, drop the index.
If this index is required, create a new one.
4) If the table is a created temporary table, disconnect from the server and connect again to
  instantiate a new copy of the created temporary table.
5)If the table is a declared temporary table, drop the table.
If this table is required, declare it again.
6)Otherwise, restore from a table space or database backup.
The backup image must have been taken subsequent to the commit point following the completion of
the non-recoverable operation (NOT LOGGED INITIALLY operation, or NONRECOVERABLE load).
fjzcaufjzcau系统运维工程师金融
2014-06-14 08:22
information center的资料,原滋原味:

http://www-01.ibm.com/support/knowledgecenter/SSEPGG_9.7.0/com.ibm.db2.luw.admin.ha.doc/doc/c0006079.html?lang=en

Because changes to the table are not logged,
you should consider the following when deciding to use the NOT LOGGED INITIALLY table attribute:
1) All changes to the table will be flushed out to disk at commit time. This means that the commit might take longer.
2) If the NOT LOGGED INITIALLY attribute is activated and an activity occurs that is not logged,
  the entire unit of work will be rolled back if a statement fails or a ROLLBACK TO SAVEPOINT is executed (SQL1476N).
3) If you are using high availability disaster recovery (HADR) you should not use the NOT LOGGED INITIALLY table attribute.
Tables created on the primary database with the NOT LOGGED INITIALLY option specified are not replicated to the standby database.
Attempts to access such tables on an active standby database or after the standby becomes the primary
as a result of a takeover operation will result in an error (SQL1477N).
4) You cannot recover these tables when rolling forward.
If the rollforward operation encounters a table that was created or altered with the NOT LOGGED INITIALLY option,
the table is marked as unavailable. After the database is recovered, any attempt to access the table returns SQL1477N.
fjzcaufjzcau系统运维工程师金融
2014-06-14 08:15
数据库恢复是靠全备份和日志的,如果事务对一个表增删改操作而不记日志的,在恢复时数据库也就不知道这个表的变化,也就无法使该表“与时俱进”,无法保持一致,也就无法被访问了。
zhendazhenda数据库管理员昆仑银行
2014-06-12 16:59
问个问题,假设NOT LOGGED INITIALLY操作后,此时表损坏,需要之前介质备份恢复并前滚,此表还可用么?
hchaohchao网站运营经理TWT
2014-06-09 10:10
是的,希望你能坚持更新博客。
fjzcaufjzcau系统运维工程师金融
2014-06-07 22:50
oracle 3年了,积累的知识点都在电子笔记,很少发到博客。DB2的话,有空发发,虽然比较简单,但习惯养成还是很难的。
Ctrl+Enter 发表

作者其他文章

相关文章

相关问题

相关资料

X社区推广