Oracle 11g Concepts 笔记1:联合索引的创建及注意事项

众所周知,每一位Oracle数据库技术牛人,每一名Oracle DBA在谈到自己的成长历程或者分享学习经验时,都会强调阅读Oracle的官方文档的重要性和必要性。而Oracle提供的官方文档数量之多,范围之广让很多初学者都望而却步,尤其是对于英语基础不好的网友更是头痛不已。其实,就我个人而言,也是经历过诸多痛苦的。事实上,我想但凡每一位认认真真地阅读过官方文档的过来人都或多或少的会有同感。既然Oracle提供给我们的学习文档如此之多,那么我们又该选择哪些文档入手呢?

我的建议是,优先阅读也是最应该阅读 Oracle Database Concepts这本书,因为这本书涵盖了Oracle数据库的全部基本概念,多读此书,收获颇多。记得当初08年的时候,自学Oracle,备考Oracle 10g OCA时,自己坚持着把10g 版本的 Oracle Concepts 10g Release 2前2/3部分反复阅读了几次,得以对Oracle数据库算是有个基本了解。

本系列文章将以笔记的形式记录自己在阅读和学习Oracle 11gR2 Database Concepts 【文档编号为E16508-05】的心得和体会。一方面算是对自己的知识总结和记录,另一方面也希望这一系列的文章能够对广大Oracle数据库技术爱好者带来帮助。当然,如果有理解的不对或者是纰漏之处,还恳请广大网友及时回复给予指正。 好了,废话不再赘述。

Oracle 11gR2 Database Concepts第3章Page 57讲到Indexes and Index-Organized Tables,其中Page 59讲到Composite Indexes。

首先,我们创建测试表,并在该表上创建联合索引:


SQL> conn / as sysdba
Connected.
SQL> select * from v$version;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit 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 

SQL> conn hr/hr

Connected.

SQL> create table t(id1 number,id2 number,name varchar2(10));

Table created.

SQL> create index idx_t_id1_id2 on t(id1,id2);

Index created.

SQL> insert into t values(1,1,'oracle');

1 row created.

SQL> insert into t values(2,2,'on');

1 row created.

SQL> insert into t values(3,2,'linux');

1 row created.

SQL> commit;

Commit complete.

SQL> select * from t;

ID1        ID2 NAME

---------- ---------- ----------

1          1 oracle

2          2 on

3          2 linux

然后,查看执行计划1:

SQL> set autot trace exp

SQL> select * from t where id1=1;

Execution Plan

----------------------------------------------------------

Plan hash value: 1660670018

---------------------------------------------------------------------------------------------

| Id  | Operation                   | Name          | Rows  | Bytes | Cost (%CPU)| Time     |

---------------------------------------------------------------------------------------------

|   0 | SELECT STATEMENT            |               |     1 |    33 |     1   (0)| 00:00:01 |

|   1 |  TABLE ACCESS BY INDEX ROWID| T             |     1 |    33 |     1   (0)| 00:00:01 |

|*  2 |   INDEX RANGE SCAN          | IDX_T_ID1_ID2 |     1 |       |     1   (0)| 00:00:01 |

---------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):

---------------------------------------------------

2 - access("ID1"=1)

Note

-----

- dynamic sampling used for this statement (level=2)

SQL>

发现该语句走索引。

查看执行计划2:

SQL> select * from t where id2=1;

Execution Plan

----------------------------------------------------------

Plan hash value: 1601196873

--------------------------------------------------------------------------

| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |

--------------------------------------------------------------------------

|   0 | SELECT STATEMENT  |      |     1 |    33 |     3   (0)| 00:00:01 |

|*  1 |  TABLE ACCESS FULL| T    |     1 |    33 |     3   (0)| 00:00:01 |

--------------------------------------------------------------------------

Predicate Information (identified by operation id):

---------------------------------------------------

1 - filter("ID2"=1)

Note

-----

- dynamic sampling used for this statement (level=2)

SQL>

SQL> select * from t where id2=2;

Execution Plan

----------------------------------------------------------

Plan hash value: 1601196873

--------------------------------------------------------------------------

| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |

--------------------------------------------------------------------------

|   0 | SELECT STATEMENT  |      |     2 |    66 |     3   (0)| 00:00:01 |

|*  1 |  TABLE ACCESS FULL| T    |     2 |    66 |     3   (0)| 00:00:01 |

--------------------------------------------------------------------------

Predicate Information (identified by operation id):

---------------------------------------------------

1 - filter("ID2"=2)

Note

-----

- dynamic sampling used for this statement (level=2)

SQL>

发现该语句走的全表扫描,而没走索引,id2字段上创建了联合索引,为什么不走索引呢?

查看执行计划3:

SQL> select * from t where id1=3 and id2=2;

Execution Plan

----------------------------------------------------------

Plan hash value: 1660670018

---------------------------------------------------------------------------------------------

| Id  | Operation                   | Name          | Rows  | Bytes | Cost (%CPU)| Time     |

---------------------------------------------------------------------------------------------

|   0 | SELECT STATEMENT            |               |     1 |    33 |     1   (0)| 00:00:01 |

|   1 |  TABLE ACCESS BY INDEX ROWID| T             |     1 |    33 |     1   (0)| 00:00:01 |

|*  2 |   INDEX RANGE SCAN          | IDX_T_ID1_ID2 |     1 |       |     1   (0)| 00:00:01 |

---------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):

---------------------------------------------------

2 - access("ID1"=3 AND "ID2"=2)

Note

-----

- dynamic sampling used for this statement (level=2)

SQL> select * from t where id2=2 and id1=3;

Execution Plan

----------------------------------------------------------

Plan hash value: 1660670018

---------------------------------------------------------------------------------------------

| Id  | Operation                   | Name          | Rows  | Bytes | Cost (%CPU)| Time     |

---------------------------------------------------------------------------------------------

|   0 | SELECT STATEMENT            |               |     1 |    33 |     1   (0)| 00:00:01 |

|   1 |  TABLE ACCESS BY INDEX ROWID| T             |     1 |    33 |     1   (0)| 00:00:01 |

|*  2 |   INDEX RANGE SCAN          | IDX_T_ID1_ID2 |     1 |       |     1   (0)| 00:00:01 |

---------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):

---------------------------------------------------

2 - access("ID1"=3 AND "ID2"=2)

Note

-----

- dynamic sampling used for this statement (level=2)

SQL>

Where从句中包含联合索引的所有字段时,优化器都会选择走索引,不论是将id1放前还是id2放前。

查看执行计划4:

SQL> select * from t where id1=2 and name='on';

Execution Plan

----------------------------------------------------------

Plan hash value: 1660670018

---------------------------------------------------------------------------------------------

| Id  | Operation                   | Name          | Rows  | Bytes | Cost (%CPU)| Time     |

---------------------------------------------------------------------------------------------

|   0 | SELECT STATEMENT            |               |     1 |    33 |     1   (0)| 00:00:01 |

|*  1 |  TABLE ACCESS BY INDEX ROWID| T             |     1 |    33 |     1   (0)| 00:00:01 |

|*  2 |   INDEX RANGE SCAN          | IDX_T_ID1_ID2 |     1 |       |     1   (0)| 00:00:01 |

---------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):

---------------------------------------------------

1 - filter("NAME"='on')

2 - access("ID1"=2)

Note

-----

- dynamic sampling used for this statement (level=2)

SQL> select * from t where id2=2 and name='on';

Execution Plan

----------------------------------------------------------

Plan hash value: 1601196873

--------------------------------------------------------------------------

| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |

--------------------------------------------------------------------------

|   0 | SELECT STATEMENT  |      |     1 |    33 |     3   (0)| 00:00:01 |

|*  1 |  TABLE ACCESS FULL| T    |     1 |    33 |     3   (0)| 00:00:01 |

--------------------------------------------------------------------------

Predicate Information (identified by operation id):

---------------------------------------------------

1 - filter("ID2"=2 AND "NAME"='on')

Note

-----

- dynamic sampling used for this statement (level=2)

SQL>

看到,第一条SQL语句(id1=2 and name=’on’)走了索引,而第二条SQL语句(id2=2 and name=’on’)走的却是全表扫描,似乎更奇怪?

最后,我们可以得出以下结论:

1 对于联合索引,在Where限制条件中出现所有被索引的列时,优化器会选择走索引(上述执行计划3);

2 对于联合索引,在Where限制条件中出现联合索引中前导的列,即创建联合索引时前导的列时,优化器会选择走索引(执行计划1,2,4中,凡是有id1作为限制条件时都会走索引,而将id2作为限制条件时均不走索引,而走全表扫描);

3 对于创建联合索引时,应考虑被索引字段的优先顺序,应将经常作为限制条件的字段放在首位;重复值少,即高基数(high-cardinaltiy)的列往前靠,而重复值多的字段往后靠;

4 对于同一张表,可以创建多个不同的联合索引,前提是,被索引的字段的个数、顺序不能一样,否则报错,ORA-01408如:

SQL> create index idx1_t_id1_id2 on t(id1,id2);

create index idx1_t_id1_id2 on t(id1,id2)

*

ERROR at line 1:

ORA-01408: such column list already indexed

SQL> create index idx1_t_id1_id2 on t(id2,id1);

Index created.

SQL>

5 联合索引及前导列的解释:

A composite index, also called a concatenated index,is an index on multiple columns in a table.
A leading portion of an index is a set of one or more columns that were specified first and consecutively in the list of columns in the CREATE INDEX statement that created the index.

Consider this CREATE INDEX statement:

CREATE INDEX comp_ind ON tab1(x, y, z);

These combinations of columns are leading portions of the index: x, xy, and xyz.

These combinations of columns are not leading portions of the index: yz, y, and z.

A SQL statement can use an access path involving a composite index if the statement contains constructs that use a leading portion of the index.

iPod Nano 6入手初体验

经过长达两个多月的等待,终于收到了这份迟来的小礼物,iPod Nano 6 8GB。虽然来的有些迟,甚至因为新加坡那边儿投递的原因差点儿导致与这款小巧却功能强大的MP3失之交臂,但是收到快递包裹的那一刻,还是有一点儿小兴奋的。

话说这个免费礼物的由来,7月份的时候,接到365信息科技大讲堂的邮件邀请,于2011年7月20日参加了甲骨文网上讲座 “最高的数据库性能和快速 100% 的投资回报率”,会后成为众多参与者中的幸运儿。

初入手时,精致而又环保的包装,着实给用户一个完美的体验,尤其是那个设计别有用心的卡片式基本使用说明手册。拆解开来,银白色的机身,延续苹果产品的主打色系。据称,苹果的这款机器投入生产时白黑比是6:1,而市场反应是,消费者的喜好却是白黑比1:6,黑色系的机器颇受消费者的青睐。个人而言,白也好,黑也罢,好用就行,当然更不能颠倒黑白。

接下来,第一件事就是阅读手册,登录苹果官网,获取更多的用户指南。

第二,连接USB接口进行充电,在充电的过程中,顺便安装配置iTunes。这里出了一个小差错,充电大概10分钟左右,误操作断开了iPod Nano,导致充电结束。苹果官方申明初次充电要到2-3小时左右,担心会不会出什么问题,以后电池会不会都充不满?没辙呀,自己是个土包子,从未拥有过苹果的任何产品,使用经验更几乎为零。在此,向各位请教,为什么现在用USB连接线给Nano充电都充不满?是否需要购买iPod 专属充电器?

第三,使用iTunes同步数据,这个应用程序的功能比较强大,就是要注册用户的那个玩意儿比较繁琐,貌似要Visa信用卡号之类的,目前还未注册成功。

第四,Nano的收音机功能很好用,可以迅速搜索当地的所有无线电台。

第五,健身功能很好用,可以记录步行、跑步的数据,并且可以同步到Nike +。

第六,时钟不错,好用。

第七,MP3播放功能和效果自是不用说了,超牛逼。

第八,感谢365信息科技大讲堂,那次会议不仅给予我Oracle相关的知识,还赠送我这个可爱的小礼物。当然还要感谢自己对于Oracle数据库技术的热爱,看来选择从业Oracle DBA还是非常不错的,至少有Nano可以免费玩儿。欢迎Nano网友回复交流用户经验及Oracle数据库相关技术经验,谢谢!

最后,附上邮件确认函一封:

From: Oracle APAC Responses Team <response@oracleevent.com>
Date: Fri, 22 Jul 2011 07:44:40 +0100
Subject: 甲骨文网上讲座: 祝贺你, 您已成为甲骨文网上讲座的iPod Nano幸运儿
To: “3dian14@gmail.com” <3dian14@gmail.com>

Dear黄伟,

谢谢您出席2011 年 7 月 20 日, 星期三的甲骨文网上讲座 “最高的数据库性能和快速 100% 的投资回报率”

您已成为甲骨文网上讲座的iPod Nano幸运儿!

请您确认以下的地址:
厦门火炬高新区软件园二期望海路29#吉联研发大楼, 厦门, 福建 361006, China
(Xiamen Torch Hi-tech Software Park two WangHai 29 # Kyrgyzstan joint
R & D Building, Xiamen, Fujian 361006, China)

我们期待您的答复。

The Oracle Corporation

记一次10g RAC 收缩表空间

这是公司一套10g RAC双节点数据库,近期业务量增长比较大,导致硬盘空间有些吃紧,在新的存储添加之前。我想到的是收缩表空间,缓解存储。做一简单记录如下:
① 操作系统版本:

[root@oracle-rac2 bin]# su - oracle
[oracle@oracle-rac2 ~]$ uname -a
Linux oracle-rac2.gillion.com.cn 2.6.18-194.0.0.0.3.el5 #1 SMP Mon Mar 29 18:14:09 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux
[oracle@oracle-rac2 ~]$ uname -r
2.6.18-194.0.0.0.3.el5

②数据库版本:

[oracle@oracle-rac2 ~]$ . oraenv
ORACLE_SID = [glndb2] ? glndb
[oracle@oracle-rac2 ~]$ export ORACLE_SID=glndb2
[oracle@oracle-rac2 ~]$ sqlplus

SQL*Plus: Release 10.2.0.5.0 - Production on Wed May 25 09:32:03 2011

Copyright (c) 1982, 2010, Oracle.  All Rights Reserved.

SQL> conn / as sysdba;
Connected.
SQL> select * from v$version;

BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - 64bi
PL/SQL Release 10.2.0.5.0 - Production
CORE    10.2.0.5.0      Production
TNS for Linux: Version 10.2.0.5.0 - Production
NLSRTL Version 10.2.0.5.0 - Production

SQL>

③ 采用ASM存储,收缩表空间之前,存储信息如下:

SQL> select name,block_size,state,type,total_mb,free_mb from v$asm_diskgroup;

NAME                           BLOCK_SIZE STATE       TYPE     TOTAL_MB    FREE_MB
------------------------------ ---------- ----------- ------ ---------- ----------
ORADATA                              4096 MOUNTED     EXTERN     262138      61364

SQL> select mount_status,header_status ,state,redundancy ,total_mb,free_mb ,name ,path from v$asm_disk;

MOUNT_S HEADER_STATU STATE    REDUNDA   TOTAL_MB    FREE_MB NAME                           PATH
------- ------------ -------- ------- ---------- ---------- ------------------------------ ----------
CACHED  MEMBER       NORMAL   UNKNOWN     262138      61364 ORADATA_0000                   /dev/oradata1

④ 表空间FR7_SA_DATA在收缩之前占用的空间如下:

SQL> select file_name,tablespace_name,bytes/1024/1024 M from dba_data_files where tablespace_name='FR7SA_DATA';

FILE_NAME
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TABLESPACE_NAME                         M
------------------------------ ----------
+ORADATA/glndb/datafile/fr7sa_data.425.751556425
FR7SA_DATA                           4996
接下来,采用如下命令resize该表空间下的数据文件:
SQL> alter database datafile '+ORADATA/glndb/datafile/fr7sa_data.425.751556425' resize 3325M;

Database altered.
收缩之后,该数据文件大小为3325M,大约释放了1670M的空间。
SQL> select file_name,tablespace_name,bytes/1024/1024 M from dba_data_files where tablespace_name='FR7SA_DATA';

FILE_NAME
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TABLESPACE_NAME                         M
------------------------------ ----------
+ORADATA/glndb/datafile/fr7sa_data.425.751556425
FR7SA_DATA                           3325

SQL>

⑤ 最后,再查看ASM磁盘、磁盘组的存储信息:

SQL>select name,block_size,state,type,total_mb,free_mb from v$asm_diskgroup;

NAME                           BLOCK_SIZE STATE       TYPE     TOTAL_MB    FREE_MB
------------------------------ ---------- ----------- ------ ---------- ----------
ORADATA                              4096 MOUNTED     EXTERN     262138      63035

SQL>select mount_status,header_status ,state,redundancy ,total_mb,free_mb ,name ,path from v$asm_disk;

MOUNT_S HEADER_STATU STATE    REDUNDA   TOTAL_MB    FREE_MB NAME                           PATH
------- ------------ -------- ------- ---------- ---------- ------------------------------ ----------
CACHED  MEMBER       NORMAL   UNKNOWN     262138      63035 ORADATA_0000                   /dev/oradata1

⑥ 至此,存储回收了大概1.6G。附上:收缩表空间的脚本。

col file# for 999
col name for a50
col resizecmd for a100

select a.file#,a.name,a.bytes/1024/1024 CurrentMB,
       ceil(HWM * a.block_size)/1024/1024 ResizeTo,
       (a.bytes - HWM * a.block_size)/1024/1024 ReleaseMB,
       'alter database datafile '''||a.name||''' resize '||
       ceil(HWM * a.block_size/1024/1024) || 'M;' ResizeCMD
from v$datafile a,
     (select file_id,max(block_id+blocks-1) HWM
       from dba_extents
       group by file_id) b
where a.file# = b.file_id(+)
and (a.bytes - HWM *block_size)>0
order by 5

如果只是想对某个表个间的datafile resize,可采用:
select a.file#,a.name,a.bytes/1024/1024 CurrentMB,
       ceil(HWM * a.block_size)/1024/1024 ResizeTo,
       (a.bytes - HWM * a.block_size)/1024/1024 ReleaseMB,
       'alter database datafile '''||a.name||''' resize '||
       ceil(HWM * a.block_size/1024/1024) || 'M;' ResizeCMD
from v$datafile a,
     (select file_id,max(block_id+blocks-1) HWM
       from dba_extents where file_id in
              (select b.file#  From v$tablespace a ,v$datafile b
                where a.ts#=b.ts# and a.name='MP2000')
       group by file_id) b
where a.file# = b.file_id(+)
and (a.bytes - HWM *block_size)>0
order by 5

计划tempfile可以resize的空间.on apply that have only one tempfile

select 'alter database tempfile '''||a.name ||''' reize '||b.siz||'M;' from v$tempfile a,
     (select tmsize.maxblk*bk.value/1024/1024 siz from
         (select nvl(max(segblk#),128) maxblk  from v$sort_usage) tmsize,
         (select value From v$parameter where name = 'db_block_size') bk) b
生产环境,需慎重使用上述脚本。

ORA-00600: internal error code, arguments: [4194]

在修改_allow_resetlogs_corruption参数为TRUE,并强制打开数据库后,遭遇下述错误信息: 该错误信息来自alert告警日志文件

Thu Feb 17 14:07:30 2011
Errors in file /u01/app/admin/orcl/bdump/orcl_smon_26850.trc:
ORA-00600: internal error code, arguments: [4194], [41], [31], [], [], [], [], []

通常ORA-00600 4194级的错误是跟回滚段错误相关! 继续查看TRACE文件(/u01/app/admin/orcl/bdump/orcl_smon_26850.trc) 看到有下述相关信息:

 Acq rbs _SYSSMU1$
 KSL POST SENT postee=6 loc='ksasnd' id1=0 id2=0 name=   type=0
 Undo Segment 1 Onlined
 KSL POST SENT postee=6 loc='ksasnd' id1=0 id2=0 name=   type=0
 Acq rbs _SYSSMU2$
 KSL POST SENT postee=6 loc='ksasnd' id1=0 id2=0 name=   type=0
 Undo Segment 2 Onlined
 KSL POST SENT postee=6 loc='ksasnd' id1=0 id2=0 name=   type=0
 Acq rbs _SYSSMU3$
 KSL POST SENT postee=6 loc='ksasnd' id1=0 id2=0 name=   type=0
 Undo Segment 3 Onlined
 KSL POST SENT postee=6 loc='ksasnd' id1=0 id2=0 name=   type=0
 Acq rbs _SYSSMU4$
 KSL POST SENT postee=6 loc='ksasnd' id1=0 id2=0 name=   type=0
 Undo Segment 4 Onlined
 KSL POST SENT postee=6 loc='ksasnd' id1=0 id2=0 name=   type=0
 Acq rbs _SYSSMU5$
 KSL POST SENT postee=6 loc='ksasnd' id1=0 id2=0 name=   type=0
 Undo Segment 5 Onlined
 KSL POST SENT postee=6 loc='ksasnd' id1=0 id2=0 name=   type=0
 Acq rbs _SYSSMU6$
 KSL POST SENT postee=6 loc='ksasnd' id1=0 id2=0 name=   type=0
 Undo Segment 6 Onlined
 KSL POST SENT postee=6 loc='ksasnd' id1=0 id2=0 name=   type=0
 Acq rbs _SYSSMU7$
 KSL POST SENT postee=6 loc='ksasnd' id1=0 id2=0 name=   type=0
 Undo Segment 7 Onlined
 KSL POST SENT postee=6 loc='ksasnd' id1=0 id2=0 name=   type=0
 Acq rbs _SYSSMU8$
 KSL POST SENT postee=6 loc='ksasnd' id1=0 id2=0 name=   type=0
 Undo Segment 8 Onlined
 KSL POST SENT postee=6 loc='ksasnd' id1=0 id2=0 name=   type=0
 Acq rbs _SYSSMU9$
 KSL POST SENT postee=6 loc='ksasnd' id1=0 id2=0 name=   type=0
 Undo Segment 9 Onlined
 KSL POST SENT postee=6 loc='ksasnd' id1=0 id2=0 name=   type=0
 Acq rbs _SYSSMU10$
 KSL POST SENT postee=6 loc='ksasnd' id1=0 id2=0 name=   type=0
 Undo Segment 10 Onlined

Google之,获取相关解决办法: ① 启动数据库,获取回滚段信息:

SYS@orcl > conn / as sysdba;
Connected to an idle instance.
SYS@orcl > startup
ORACLE instance started.
Total System Global Area  507510784 bytes
Fixed Size                  1220240 bytes
Variable Size             171966832 bytes
Database Buffers          327155712 bytes
Redo Buffers                7168000 bytes
Database mounted.
ORA-00603: ORACLE server session terminated by fatal error
SYS@orcl > select * from v$rollname;
ERROR:
ORA-03114: not connected to ORACLE
SYS@orcl > conn / as sysdba;
Connected.
SYS@orcl > startup mount;
ORA-01081: cannot start already-running ORACLE - shut it down first
SYS@orcl > conn / as sysdba;
Connected.
SYS@orcl > select * from V$rollname;
       USN NAME
---------- ------------------------------
         0 SYSTEM
         1 _SYSSMU1$
         2 _SYSSMU2$
         3 _SYSSMU3$
         4 _SYSSMU4$
         5 _SYSSMU5$
         6 _SYSSMU6$
         7 _SYSSMU7$
         8 _SYSSMU8$
         9 _SYSSMU9$
        10 _SYSSMU10$
11 rows selected.
SYS@orcl >

此时,数据库虽可以使用,但是还是有问题的,过一段儿时间,莫名其妙的宕机!!!再次尝试启动数据库的时候,却非常正常,并未报错!!!

SYS@orcl > startup
ORACLE instance started.
Total System Global Area  507510784 bytes
Fixed Size                  1220240 bytes
Variable Size             171966832 bytes
Database Buffers          327155712 bytes
Redo Buffers                7168000 bytes
Database mounted.
Database opened.
SYS@orcl > select segment_name from dba_rollback_segs;
SEGMENT_NAME
------------------------------
SYSTEM
_SYSSMU1$
_SYSSMU2$
_SYSSMU3$
_SYSSMU4$
_SYSSMU5$
_SYSSMU6$
_SYSSMU7$
_SYSSMU8$
_SYSSMU9$
_SYSSMU10$
SEGMENT_NAME
------------------------------
_SYSSMU11$
12 rows selected.
SYS@orcl > select segment_name from dba_rollback_segs;
select segment_name from dba_rollback_segs
*
ERROR at line 1:
ORA-03135: connection lost contact
SYS@orcl >

② 开始通过调整参数的方式,来修复数据库:

SYS@orcl > conn / as sysdba;
Connected to an idle instance.
SYS@orcl > create pfile from spfile;
File created.
SYS@orcl >

修改pfile,在该文件中,添加如下信息:

[oracle@rhel10g dbs]$ tail -2  /u01/app/oracle/dbs/initorcl.ora
*.undo_management='MANUAL'
_corrupted_rollback_segments='_SYSSMU1$','_SYSSMU2$','_SYSSMU3$',
'_SYSSMU4$','_SYSSMU5$','_SYSSMU6$','_SYSSMU7$','_SYSSMU8$',
'_SYSSMU9$','_SYSSMU10$','_SYSSMU11$'

这两个参数表示,回滚段手工管理,并设置_corrupted_rollback_segments后面的那11个回滚段为损坏状态! ③ 然后,用该PFILE来启动数据库:

SYS@orcl > startup pfile=/u01/app/oracle/dbs/initorcl.ora
ORACLE instance started.
Total System Global Area  507510784 bytes
Fixed Size                  1220240 bytes
Variable Size             171966832 bytes
Database Buffers          327155712 bytes
Redo Buffers                7168000 bytes
Database mounted.
Database opened.
SYS@orcl > select * from V$rollname;
       USN NAME
---------- ------------------------------
         0 SYSTEM
SYS@orcl > select segment_name from dba_rollback_segs;
SEGMENT_NAME
------------------------------
SYSTEM
_SYSSMU1$
_SYSSMU2$
_SYSSMU3$
_SYSSMU4$
_SYSSMU5$
_SYSSMU6$
_SYSSMU7$
_SYSSMU8$
_SYSSMU9$
_SYSSMU10$
SEGMENT_NAME
------------------------------
_SYSSMU11$
12 rows selected.

数据库正常启动,并未像刚才的那样,莫名其妙的“死”掉!!!但是,数据看出现下述症状,普通用户在数据库内无法做事务操作,而SYS用户却可以。

SYS@orcl > select count(*) from m;
  COUNT(*)
----------
       176
SYS@orcl > insert into m select * from m;
176 rows created.
SYS@orcl >

上述看到,SYS用户正常做操作。而HR用户执行事务就报错:ORA-01552!

SYS@orcl > conn hr/hr;
Connected.
HR@orcl > select count(*) from t;
  COUNT(*)
----------
       214
HR@orcl > insert into t select * from t;
insert into t select * from t
            *
ERROR at line 1:
ORA-01552: cannot use system rollback segment for non-system tablespace 'USERS'
HR@orcl >

④ 删除,并重建UNDO表空间:

SYS@orcl > conn / as sysdba;
Connected.
SYS@orcl > show parameter undo
NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
undo_management                      string      MANUAL
undo_retention                       integer     900
undo_tablespace                      string      UNDOTBS1
SYS@orcl > col file_name for a40
SYS@orcl > col tablespace_name for a15
SYS@orcl > set line 120
SYS@orcl > select file_name,tablespace_name from dba_data_files;
FILE_NAME                                TABLESPACE_NAME
---------------------------------------- ---------------
/u01/app/oradata/orcl/users01.dbf        USERS
/u01/app/oradata/orcl/sysaux01.dbf       SYSAUX
/u01/app/oradata/orcl/undotbs1.dbf       UNDOTBS1
/u01/app/oradata/orcl/system01.dbf       SYSTEM
/u01/app/oradata/orcl/example01.dbf      EXAMPLE
SYS@orcl > drop tablespace undotbs1 including contents and datafiles;
Tablespace dropped.
SYS@orcl > select file_name,tablespace_name from dba_data_files;
FILE_NAME                                TABLESPACE_NAME
---------------------------------------- ---------------
/u01/app/oradata/orcl/users01.dbf        USERS
/u01/app/oradata/orcl/sysaux01.dbf       SYSAUX
/u01/app/oradata/orcl/system01.dbf       SYSTEM
/u01/app/oradata/orcl/example01.dbf      EXAMPLE
SYS@orcl > create undo tablespace undotbs1 datafile '/u01/app/oradata/orcl/undotbs1.dbf' size 50m;
Tablespace created.
SYS@orcl >

⑤ 关闭实例,生成新的参数文件:

SYS@orcl > shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SYS@orcl >

⑥ 修改PFILE,这次要去掉_corrupted_rollback_segments参数的设置,并将undo_management改回为AUTO:

[oracle@rhel10g ~]$ tail -3 /u01/app/oracle/dbs/initorcl.ora
*.undo_tablespace='UNDOTBS1'
*.user_dump_dest='/u01/app/admin/orcl/udump'
*.undo_management='auto'

⑦ 然后创建SPFILE:

SYS@orcl > create spfile from pfile;
File created.
SYS@orcl > !strings /u01/app/oracle/dbs/spfileorcle.ora | grep undo
strings: '/u01/app/oracle/dbs/spfileorcle.ora': No such file
SYS@orcl > !strings /u01/app/oracle/dbs/spfileorcl.ora | grep undo
*.undo_management='auto'
*.undo_tablespace='UNDOTBS1'
SYS@orcl >

⑧ 用新SPFILE启动数据库,并做校验:

SYS@orcl > startup
ORACLE instance started.
Total System Global Area  507510784 bytes
Fixed Size                  1220240 bytes
Variable Size             171966832 bytes
Database Buffers          327155712 bytes
Redo Buffers                7168000 bytes
Database mounted.
Database opened.
SYS@orcl > select count(*) from m;
  COUNT(*)
----------
       352
SYS@orcl > select * from v$rollname;
       USN NAME
---------- ------------------------------
         0 SYSTEM
         1 _SYSSMU1$
         2 _SYSSMU2$
         3 _SYSSMU3$
         4 _SYSSMU4$
         5 _SYSSMU5$
         6 _SYSSMU6$
         7 _SYSSMU7$
         8 _SYSSMU8$
         9 _SYSSMU9$
        10 _SYSSMU10$
11 rows selected.
SYS@orcl > select segment_name from dba_rollback_segs;
SEGMENT_NAME
------------------------------
SYSTEM
_SYSSMU1$
_SYSSMU2$
_SYSSMU3$
_SYSSMU4$
_SYSSMU5$
_SYSSMU6$
_SYSSMU7$
_SYSSMU8$
_SYSSMU9$
_SYSSMU10$
11 rows selected.
SYS@orcl > select count(*) from m;
  COUNT(*)
----------
       352
SYS@orcl > conn hr/hr;
Connected.
HR@orcl > select count(*) from t;
  COUNT(*)
----------
       214
HR@orcl > insert into t select * from t;
214 rows created.
HR@orcl > commit;
Commit complete.
HR@orcl > select count(*) from t;
  COUNT(*)
----------
       428
HR@orcl >

运行一段时间,发现数据库并无其它异常。至此,解决ORA-00600 4194的错误!

Oracle _disable_logging and _allow_resetlogs_corruption

本文简单研究oracle隐含参数_disable_logging_allow_resetlogs_corruption两个参数。
仅作学习测试用,实际情况需慎重,做好数据库的备份,以防丢失数据。
1 启动数据库,获取相关信息:

SYS@orcl > startup
ORACLE instance started.

Total System Global Area  507510784 bytes
Fixed Size                  1220240 bytes
Variable Size             167772528 bytes
Database Buffers          331350016 bytes
Redo Buffers                7168000 bytes
Database mounted.
Database opened.
SYS@orcl > show parameter spfile;

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
spfile                               string      /u01/app/oracle/dbs/spfileorcl
                                                 .ora
SYS@orcl > show parameter _disable

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
_disable_logging                     boolean     FALSE
SYS@orcl > select count(*) from m;

  COUNT(*)
----------
        44

SYS@orcl > select * from V$log;

    GROUP#    THREAD#  SEQUENCE#      BYTES    MEMBERS ARC STATUS           FIRST_CHANGE# FIRST_TIME
---------- ---------- ---------- ---------- ---------- --- ---------------- ------------- ------------------
         1          1          0   52428800          1 YES UNUSED                       0
         2          1          0   52428800          1 YES UNUSED                       0
         3          1          1   52428800          1 NO  CURRENT                1098823 17-FEB-11

可以看到,当前数据库使用SPFILE参数文件,_disable_logging参数为FALSE,测试表m有44条记录,当前日志组为第3组。
2修改隐含参数_disable_logging为TRUE,并重启数据库使之生效:

SYS@orcl > alter system set "_disable_logging"=true scope=spfile;

System altered.

SYS@orcl > shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SYS@orcl > startup;
ORACLE instance started.

Total System Global Area  507510784 bytes
Fixed Size                  1220240 bytes
Variable Size             167772528 bytes
Database Buffers          331350016 bytes
Redo Buffers                7168000 bytes
Database mounted.
Database opened.
SYS@orcl > show parameter _disable

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
_disable_logging                     boolean     TRUE
SYS@orcl > select * from V$log;

    GROUP#    THREAD#  SEQUENCE#      BYTES    MEMBERS ARC STATUS           FIRST_CHANGE# FIRST_TIME
---------- ---------- ---------- ---------- ---------- --- ---------------- ------------- ------------------
         1          1          0   52428800          1 YES UNUSED                       0
         2          1          0   52428800          1 YES UNUSED                       0
         3          1          1   52428800          1 NO  CURRENT                1098823 17-FEB-11

SYS@orcl > select count(*) from m;

  COUNT(*)
----------
        44

SYS@orcl > insert into m select * from m;

44 rows created.

SYS@orcl > commit;

Commit complete.

SYS@orcl > select * from V$log;

    GROUP#    THREAD#  SEQUENCE#      BYTES    MEMBERS ARC STATUS           FIRST_CHANGE# FIRST_TIME
---------- ---------- ---------- ---------- ---------- --- ---------------- ------------- ------------------
         1          1          0   52428800          1 YES UNUSED                       0
         2          1          0   52428800          1 YES UNUSED                       0
         3          1          1   52428800          1 NO  CURRENT                1098823 17-FEB-11

插入44条记录,这44条记录写入当前组第3组日志文件中的。

SYS@orcl > alter system switch logfile;

System altered.

SYS@orcl > select * from V$log;

    GROUP#    THREAD#  SEQUENCE#      BYTES    MEMBERS ARC STATUS           FIRST_CHANGE# FIRST_TIME
---------- ---------- ---------- ---------- ---------- --- ---------------- ------------- ------------------
         1          1          2   52428800          1 NO  CURRENT                1101521 17-FEB-11
         2          1          0   52428800          1 YES UNUSED                       0
         3          1          1   52428800          1 NO  ACTIVE                 1098823 17-FEB-11

SYS@orcl > select count(*) from m;

  COUNT(*)
----------
        88

SYS@orcl > insert into m select * from m;

88 rows created.

SYS@orcl > commit;

Commit complete.

SYS@orcl > select count(*) from m;

  COUNT(*)
----------
       176

SYS@orcl > select * from V$log;

    GROUP#    THREAD#  SEQUENCE#      BYTES    MEMBERS ARC STATUS           FIRST_CHANGE# FIRST_TIME
---------- ---------- ---------- ---------- ---------- --- ---------------- ------------- ------------------
         1          1          2   52428800          1 NO  CURRENT                1101521 17-FEB-11
         2          1          0   52428800          1 YES UNUSED                       0
         3          1          1   52428800          1 NO  ACTIVE                 1098823 17-FEB-11

SYS@orcl > alter system checkpoint;

System altered.
SYS@orcl > select * from V$log;

    GROUP#    THREAD#  SEQUENCE#      BYTES    MEMBERS ARC STATUS           FIRST_CHANGE# FIRST_TIME
---------- ---------- ---------- ---------- ---------- --- ---------------- ------------- ------------------
         1          1          2   52428800          1 NO  CURRENT                1101521 17-FEB-11
         2          1          0   52428800          1 YES UNUSED                       0
         3          1          1   52428800          1 NO  INACTIVE               1098823 17-FEB-11

SYS@orcl >

向m表插入记录(此时共有176条记录),做完日志切换,并做检查点,看到第三组状态有ACTIVE变为INACTIVE,说明检查点完成。
3 强制启动数据库FORCE:

SYS@orcl > startup force;
ORACLE instance started.

Total System Global Area  507510784 bytes
Fixed Size                  1220240 bytes
Variable Size             167772528 bytes
Database Buffers          331350016 bytes
Redo Buffers                7168000 bytes
Database mounted.
Database opened.
SYS@orcl > select count(*) from m;

  COUNT(*)
----------
       176

SYS@orcl > select * from V$log;

    GROUP#    THREAD#  SEQUENCE#      BYTES    MEMBERS ARC STATUS           FIRST_CHANGE# FIRST_TIME
---------- ---------- ---------- ---------- ---------- --- ---------------- ------------- ------------------
         1          1          2   52428800          1 NO  INACTIVE               1101521 17-FEB-11
         2          1          3   52428800          1 NO  CURRENT                1121523 17-FEB-11
         3          1          1   52428800          1 NO  INACTIVE               1098823 17-FEB-11

看到数据库一切正常,且M表中的176条记录完好。
4 继续插入测试数据,又插入176条记录,这176条记录对应的日志是在第2组的。

SYS@orcl > insert into m select * from m;

176 rows created.

SYS@orcl > alter system switch logfile;

System altered.

SYS@orcl > select * from V$log;

    GROUP#    THREAD#  SEQUENCE#      BYTES    MEMBERS ARC STATUS           FIRST_CHANGE# FIRST_TIME
---------- ---------- ---------- ---------- ---------- --- ---------------- ------------- ------------------
         1          1          2   52428800          1 NO  INACTIVE               1101521 17-FEB-11
         2          1          3   52428800          1 NO  ACTIVE                 1121523 17-FEB-11
         3          1          4   52428800          1 NO  CURRENT                1121706 17-FEB-11

SYS@orcl > select count(*) from m;

  COUNT(*)
----------
       352

SYS@orcl > insert into m select * from m;

352 rows created.

SYS@orcl > select * from V$log;

    GROUP#    THREAD#  SEQUENCE#      BYTES    MEMBERS ARC STATUS           FIRST_CHANGE# FIRST_TIME
---------- ---------- ---------- ---------- ---------- --- ---------------- ------------- ------------------
         1          1          2   52428800          1 NO  INACTIVE               1101521 17-FEB-11
         2          1          3   52428800          1 NO  ACTIVE                 1121523 17-FEB-11
         3          1          4   52428800          1 NO  CURRENT                1121706 17-FEB-11

SYS@orcl > startup force;
ORACLE instance started.

Total System Global Area  507510784 bytes
Fixed Size                  1220240 bytes
Variable Size             167772528 bytes
Database Buffers          331350016 bytes
Redo Buffers                7168000 bytes
Database mounted.
ORA-00354: corrupt redo log block header
ORA-00353: log corruption near block 3 change 1121523 time 02/17/2011 11:45:37
ORA-00312: online log 2 thread 1: '/u01/app/oradata/orcl/redo02.log'

SYS@orcl > select * from v$log;

    GROUP#    THREAD#  SEQUENCE#      BYTES    MEMBERS ARC STATUS           FIRST_CHANGE# FIRST_TIME
---------- ---------- ---------- ---------- ---------- --- ---------------- ------------- ------------------
         1          1          2   52428800          1 NO  INACTIVE               1101521 17-FEB-11
         3          1          4   52428800          1 NO  CURRENT                1121706 17-FEB-11
         2          1          3   52428800          1 NO  ACTIVE                 1121523 17-FEB-11

不做检查点,FORCE强制启动数据库,发现报错!!!第2个日志组需要恢复。为什么呢,该怎么恢复呢?
5 修改另外一个隐含参数_allow_resetlogs_corruption为TRUE,并做恢复:

SYS@orcl > show parameter _allow

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
_allow_resetlogs_corruption          boolean     FALSE
SYS@orcl > alter system set "_allow_resetlogs_corruption"=true scope=spfile;

System altered.

SYS@orcl > startup force;
ORACLE instance started.

Total System Global Area  507510784 bytes
Fixed Size                  1220240 bytes
Variable Size             167772528 bytes
Database Buffers          331350016 bytes
Redo Buffers                7168000 bytes
Database mounted.
ORA-00354: corrupt redo log block header
ORA-00353: log corruption near block 3 change 1121523 time 02/17/2011 11:45:37
ORA-00312: online log 2 thread 1: '/u01/app/oradata/orcl/redo02.log'

SYS@orcl > alter database open resetlogs;
alter database open resetlogs
*
ERROR at line 1:
ORA-01139: RESETLOGS option only valid after an incomplete database recovery

SYS@orcl > recover database using backup controlfile until cancel;
ORA-00279: change 1121524 generated at 02/17/2011 11:45:37 needed for thread 1
ORA-00289: suggestion : /u01/app/flash_recovery_area/ORCL/
archivelog/2011_02_17/o1_mf_1_3_%u_.arc
ORA-00280: change 1121524 for thread 1 is in sequence #3

Specify log: {=suggested | filename | AUTO | CANCEL}

ORA-00308: cannot open archived log '/u01/app/flash_recovery_area/ORCL/archivelog/2011_02_17/o1_mf_1_3_%u_.arc'
ORA-27037: unable to obtain file status
Linux Error: 2: No such file or directory
Additional information: 3

ORA-01547: warning: RECOVER succeeded but OPEN RESETLOGS would get error below
ORA-01194: file 1 needs more recovery to be consistent
ORA-01110: data file 1: '/u01/app/oradata/orcl/system01.dbf'

SYS@orcl > alter database open resetlogs;

Database altered.

SYS@orcl > select count(*) from m;

  COUNT(*)
----------
       176

SYS@orcl >

6 结果,数据库不完全恢复成功,但是丢失数据了。那么又丢失了哪些数据呢?
结论:①_disable_logging参数是禁止数据库产生日志的。可以看到,在日志组为ACTIVE状态下(检查点未完成),startup force时,该日志组需要恢复。也就是为什么做过检查点(alter system checkpoint)后,startup force时,数据还在,而未做检查点时,数据丢失;
②_allow_resetlogs_corruption参数允许在特定情况下将数据库强制打开,不做校验;
③本测试环境:数据库非归档模式,未做任何备份;

SYS@orcl > !uname -a
Linux rhel10g 2.6.18-164.el5xen #1 SMP Tue Aug 18 16:06:30 EDT 2009 i686 athlon i386 GNU/Linux

SYS@orcl > select * from v$version;

BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
PL/SQL Release 10.2.0.1.0 - Production
CORE    10.2.0.1.0      Production
TNS for Linux: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production

SYS@orcl >

asmcmd 报错解决

RHEL 5.4 服务器上运行的一套 oracle 10g ASM的数据库,在调用asmcmd的时候遇到如下错误:

[oracle@rhel10g ~]$ asmcmd
install_driver(Oracle) failed: Can't load 
'/u01/app/oracle/perl/lib/site_perl/5.8.3/i686-linux-thread-multi/auto/DBD/Oracle/Oracle.so'
 for module DBD::Oracle: /u01/app/oracle/lib/libnnz10.so: cannot restore
 segment prot after reloc: Permission denied at
 /u01/app/oracle/perl/lib/5.8.3/i686-linux-thread-multi/DynaLoader.pm line 229.
 at (eval 2) line 3Compilation failed in require at (eval 2) line 3.
Perhaps a required shared library or dll isn't installed where expected
 at /u01/app/oracle/bin/asmcmdcore line 5953
[oracle@rhel10g ~]$ 

Google 之,给出解决方法:

关闭SELinux:

[oracle@rhel10g ~]$ su - root

Password:

[root@rhel10g ~]# setenforce 0

[root@rhel10g ~]# exit

logout

[oracle@rhel10g ~]$ export ORACLE_SID=+ASM

[oracle@rhel10g ~]$ asmcmd

ASMCMD>

问题基本解决,具体原因有待进一步探究!!!

Oracle time_zone

1. How to check the session time zone ?
=======================================
Answer
——
The new SESSIONTIMEZONE built-in SQL function returns the value of the current session’s time zone.
SQL> SELECT SESSIONTIMEZONE FROM DUAL;
SESSIONTIMEZONE
—————
+01:00
2. How to set the session time zone ?
=====================================
Answer
——
The session time zone can be set to:
– O/S local time zone
– Database time zone
– An absolute offset
– A named region
1. The first method consists to use one of the following ALTER SESSION SET TIME_ZONE statements:
SQL> ALTER SESSION SET TIME_ZONE = local;
SQL> ALTER SESSION SET TIME_ZONE = dbtimezone;
SQL> ALTER SESSION SET TIME_ZONE = ‘-05:00’;
SQL> ALTER SESSION SET TIME_ZONE = ‘Europe/London’;
2. As an alternative, the operating system environment variable ORA_SDTZ can also be used to set the session time zone:
ORA_SDTZ = ‘OS_TZ’ | ‘DB_TZ’ | ‘[+ | -] hh:mm’ | ‘timezone_region’
Example:
$ ORA_SDTZ=’OS_TZ’
$ export ORA_SDTZ
$ ORA_SDTZ=’-05:00′
$ export ORA_SDTZ
3. How to check the database time zone ?
========================================
Answer
——
The DBTIMEZONE SQL function returns the value of the database time zone.
SQL> SELECT DBTIMEZONE FROM DUAL;
DBTIMEZONE
————–
Europe/Lisbon
Note that the database timezone is also included in both database_properties and props$ views:
SQL> SELECT property_name, property_value
FROM database_properties
WHERE property_name=’DBTIMEZONE’;
and
SQL> SELECT name, value$
FROM props$
WHERE name=’DBTIMEZONE’;
Be aware that you should not rely on these views because in case of db time zone change, these views reflect the new db time zone too early: they should reflect
it only after database shutdown and restart.
4. How to set the database time zone ?
======================================
Answer
——
Note that the database time zone is only relevant for TIMESTAMP WITH LOCAL TIME ZONE columns.
1. At creation time
SQL> CREATE DATABASE …
SET TIME_ZONE=’Europe/London’;
If not specified with the CREATE DATABASE statement, the database time zone defaults to the server抯 O/S timezone offset.
2. After database creation, use the ALTER DATABASE SET TIME_ZONE statement and then shut down and restart the database.
SQL> ALTER DATABASE SET TIME_ZONE = ‘-05:00’;
SQL> ALTER DATABASE SET TIME_ZONE = ‘Europe/Lisbon’;
The change will not take effect until the database is bounced.
5. How to list the valid time zone regions ?
============================================
Answer
——
SQL> SELECT * FROM v$timezone_names;
TZNAME TZABBREV
—————————— ———-
Pacific/Tahiti LMT
Poland LMT
US/Pacific PST
Europe/Zurich CET
The result will depend on which timezone file is currently in use.
See Q&A 7-8 for further details.
6. How to retrieve the time zone offset corresponding to a time zone region ?
=============================================================================
The new 9i TZ_OFFSET() SQL function returns the time zone offset displacement to the input time zone region.
SQL> SELECT TZ_OFFSET(‘Europe/London’) FROM DUAL;
TZ_OFFS
——-
+01:00
The returned offset depends on the date this statement is executed. For example, in the ‘US/Pacific’ time zone, it may return ‘-07:00’ or ‘-08:00’ whether daylight saving is in effect or not.
SQL> SELECT TZ_OFFSET(SESSIONTIMEZONE), TZ_OFFSET(DBTIMEZONE) FROM DUAL;
returns the time zone offset corresponding to the time zone set for session and database.
TZ_OFFS TZ_OFFS
——- ——-
+01:00 +00:00
7. How to use a larger set of defined time zones ?
==================================================
2 different time zone files contain for each zone the offset from UTC,the transition times for daylight savings and abbreviation :
– ORACLE_HOME/oracore/zoneinfo/timezone.dat
This is the default and contains the most commonly used time zones.
This is the smallest file.
– ORACLE_HOME/oracore/zoneinfo/timezlrg.dat
This file contains the larger set of defined time zones and can be used
if you require time zones that are not defined in the default time zone file.
To enable the use of this larger time zone data file :
1. Shutdown the database
2.1 On UNIX platforms :
set the ORA_TZFILE environment variable to the full pathname of
the location for the timezlrg.dat. By default, this should be
$ORACLE_HOME/oracore/zoneinfo/timezlrg.dat:
$ setenv ORA_TZFILE $ORACLE_HOME/oracore/zoneinfo/timezlrg.dat
2.2 On Windows systems :
You should add an ORA_TZFILE registry subkey for each of your Oracle Homes
with the Windows Registry Editor:
Start -> Run…
Type “regedit”, and click “ok”
Add or edit the following registry entry:
HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\HOMEID\ORA_TZFILE
where ID is the unique number identifying the Oracle home.
Set the ORA_TZFILE to the full pathname of the location for the timezlrg.dat: By default, the value should be %ORACLE_HOME%\oracore\zoneinfo\timezlrg.dat
3. Restart the database
Once the larger timezlrg.dat is used, it must continue to be used unless the user is sure that none of the non-default zones are used for data that is stored in the database.
Also, all databases that share information should use the same time zone data file.
Be aware that you can neither create nore alter these timezone definition files !
8. Which timezone-set is currently used ?
=========================================
Beside checking the UNIX ORA_TZFILE parameter or the Windows registry subkey,you can easily check whether you are using the large or the default timezone file with the following query:
SQL> SELECT COUNT(*) FROM v$timezone_names;
If the default-smallest timezone region file is in use, it will return:
COUNT(*)
———-
616 (in 9.0.1.x and 9.2.x)
or
COUNT(*)
———-
1250 (in 9.0.1.x and 9.2.x)
if the largest file is in use.
9. Can customers rely on Oracle time zones ?
============================================
Timezone.dat and timezlrg.dat are based on information maintained by the U.S. Navy Observatory.

1. How to check the session time zone ?

=======================================

Answer

——

The new SESSIONTIMEZONE built-in SQL function returns the value of the current session’s time zone.

SQL> SELECT SESSIONTIMEZONE FROM DUAL;

SESSIONTIMEZONE

—————

+01:00

2. How to set the session time zone ?

=====================================

Answer

——

The session time zone can be set to:

– O/S local time zone

– Database time zone

– An absolute offset

– A named region

1. The first method consists to use one of the following ALTER SESSION SET TIME_ZONE statements:

SQL> ALTER SESSION SET TIME_ZONE = local;

SQL> ALTER SESSION SET TIME_ZONE = dbtimezone;

SQL> ALTER SESSION SET TIME_ZONE = ‘-05:00’;

SQL> ALTER SESSION SET TIME_ZONE = ‘Europe/London’;

2. As an alternative, the operating system environment variable ORA_SDTZ can also be used to set the session time zone:

ORA_SDTZ = ‘OS_TZ’ | ‘DB_TZ’ | ‘[+ | -] hh:mm’ | ‘timezone_region’

Example:

$ ORA_SDTZ=’OS_TZ’

$ export ORA_SDTZ

$ ORA_SDTZ=’-05:00′

$ export ORA_SDTZ

3. How to check the database time zone ?

========================================

Answer

——

The DBTIMEZONE SQL function returns the value of the database time zone.

SQL> SELECT DBTIMEZONE FROM DUAL;

DBTIMEZONE

————–

Europe/Lisbon

Note that the database timezone is also included in both database_properties and props$ views:

SQL> SELECT property_name, property_value

FROM database_properties

WHERE property_name=’DBTIMEZONE’;

and

SQL> SELECT name, value$

FROM props$

WHERE name=’DBTIMEZONE’;

Be aware that you should not rely on these views because in case of db time zone change, these views reflect the new db time zone too early: they should reflect

it only after database shutdown and restart.

4. How to set the database time zone ?

======================================

Answer

——

Note that the database time zone is only relevant for TIMESTAMP WITH LOCAL TIME ZONE columns.

1. At creation time

SQL> CREATE DATABASE …

SET TIME_ZONE=’Europe/London’;

If not specified with the CREATE DATABASE statement, the database time zone defaults to the server抯 O/S timezone offset.

2. After database creation, use the ALTER DATABASE SET TIME_ZONE statement and then shut down and restart the database.

SQL> ALTER DATABASE SET TIME_ZONE = ‘-05:00’;

SQL> ALTER DATABASE SET TIME_ZONE = ‘Europe/Lisbon’;

The change will not take effect until the database is bounced.

5. How to list the valid time zone regions ?

============================================

Answer

——

SQL> SELECT * FROM v$timezone_names;

TZNAME TZABBREV

—————————— ———-

Pacific/Tahiti LMT

Poland LMT

US/Pacific PST

Europe/Zurich CET

The result will depend on which timezone file is currently in use.

See Q&A 7-8 for further details.

6. How to retrieve the time zone offset corresponding to a time zone region ?

======================================================

The new 9i TZ_OFFSET() SQL function returns the time zone offset displacement to the input time zone region.

SQL> SELECT TZ_OFFSET(‘Europe/London’) FROM DUAL;

TZ_OFFS

——-

+01:00

The returned offset depends on the date this statement is executed. For example, in the ‘US/Pacific’ time zone, it may return ‘-07:00’ or ‘-08:00’ whether daylight saving is in effect or not.

SQL> SELECT TZ_OFFSET(SESSIONTIMEZONE), TZ_OFFSET(DBTIMEZONE) FROM DUAL;

returns the time zone offset corresponding to the time zone set for session and database.

TZ_OFFS TZ_OFFS

——- ——-

+01:00 +00:00

7. How to use a larger set of defined time zones ?

==================================================

2 different time zone files contain for each zone the offset from UTC,the transition times for daylight savings and abbreviation :

– ORACLE_HOME/oracore/zoneinfo/timezone.dat

This is the default and contains the most commonly used time zones.

This is the smallest file.

– ORACLE_HOME/oracore/zoneinfo/timezlrg.dat

This file contains the larger set of defined time zones and can be used

if you require time zones that are not defined in the default time zone file.

To enable the use of this larger time zone data file :

1. Shutdown the database

2.1 On UNIX platforms :

set the ORA_TZFILE environment variable to the full pathname of

the location for the timezlrg.dat. By default, this should be

$ORACLE_HOME/oracore/zoneinfo/timezlrg.dat:

$ setenv ORA_TZFILE $ORACLE_HOME/oracore/zoneinfo/timezlrg.dat

2.2 On Windows systems :

You should add an ORA_TZFILE registry subkey for each of your Oracle Homes

with the Windows Registry Editor:

Start -> Run…

Type “regedit”, and click “ok”

Add or edit the following registry entry:

HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\HOMEID\ORA_TZFILE

where ID is the unique number identifying the Oracle home.

Set the ORA_TZFILE to the full pathname of the location for the timezlrg.dat: By default, the value should be %ORACLE_HOME%\oracore\zoneinfo\timezlrg.dat

3. Restart the database

Once the larger timezlrg.dat is used, it must continue to be used unless the user is sure that none of the non-default zones are used for data that is stored in the database.

Also, all databases that share information should use the same time zone data file.

Be aware that you can neither create nore alter these timezone definition files !

8. Which timezone-set is currently used ?

=========================================

Beside checking the UNIX ORA_TZFILE parameter or the Windows registry subkey,you can easily check whether you are using the large or the default timezone file with the following query:

SQL> SELECT COUNT(*) FROM v$timezone_names;

If the default-smallest timezone region file is in use, it will return:

COUNT(*)

———-

616 (in 9.0.1.x and 9.2.x)

or

COUNT(*)

———-

1250 (in 9.0.1.x and 9.2.x)

if the largest file is in use.

9. Can customers rely on Oracle time zones ?

============================================

Timezone.dat and timezlrg.dat are based on information maintained by the U.S. Navy Observatory.

ORA-00600:[keltnfy-ldmInit],[46]错误

今天下午,在一天RHEL as 5.4的PC server上,安装完Oracle软件之后,利用DBCA创建oracle 10g数据库的时候,报错:

触发ORA-00600[keltnfy-ldmInit][46]类似这样的错误。

以前,在红帽Linux系统上创建Oracle数据库向来都是轻车熟路的。今天,怎么回事儿?

不得已,Google,很快定位到问题的根源:

我在安装Oracle软件之后,创建数据库之前,修改了主机的/etc/hosts配置文件中的关于hostname的配置信息。

于是,修改/etc/hosts文件与/etc/sysconfig/network同步更新hostname。

然后,不管是执行Net Configuration Assistant配置网络,还是利用Database Configuration Assistant来建库,均无报错!

千万记得,在配置主机IP,或者修改主机名的时候,稍有不慎,往往会给Oracle数据库带来麻烦!

How to solve No termcap nor curses library found

Linux平台下,想在Oracle数据库的SQL*PLUS的命令行提示符下,利用键盘上的上下箭头,调出历史执行的SQL命令,可以考虑采用rlwrap这一开源工具

现,简单记录一下安装过程(Red Hat Enterprise Linux as 5.4环境)。以前在4版本的操作系统上安装时很顺利,就不再记录。

①解压并安装rlwrap工具:

[root@server1 ~]# tar -zxvf rlwrap-0.30.tar.gz

.......

.......

[root@server1 ~]# cd rlwrap-0.30 && ll

total 464

-rw-rw-r-- 1 hekan hekan  32266 Jan  8  2008 aclocal.m4

-rw-r--r-- 1 hekan hekan    638 Aug 15  2006 AUTHORS

-rw-r--r-- 1 hekan hekan   4740 Nov 10  2007 BUGS

-rw-r--r-- 1 hekan hekan    105 Aug 10  2003 ChangeLog

drwxrwxrwx 2 hekan hekan   4096 Jan  8  2008 completions

-rw-rw-r-- 1 hekan hekan   5489 Jan  8  2008 config.h.in

-rwxr-xr-x 1 hekan hekan 217282 Jan  8  2008 configure

-rw-r--r-- 1 hekan hekan   9860 Jan  8  2008 configure.ac

-rw-r--r-- 1 hekan hekan  17992 Aug  8  2006 COPYING

drwxrwxrwx 2 hekan hekan   4096 Jan  8  2008 distribution

drwxrwxrwx 2 hekan hekan   4096 Jan  8  2008 doc

-rw-r--r-- 1 hekan hekan   1927 Nov 10  2007 INSTALL

-rw-r--r-- 1 hekan hekan    436 Nov 10  2007 Makefile.am

-rw-rw-r-- 1 hekan hekan  22987 Jan  8  2008 Makefile.in

-rw-r--r-- 1 hekan hekan  11187 Jan  3  2008 NEWS

-rw-r--r-- 1 hekan hekan   2745 Nov 10  2007 README

drwxrwxrwx 2 hekan hekan   4096 Jan  8  2008 src

drwxrwxrwx 2 hekan hekan   4096 Jan  8  2008 test

-rw-r--r-- 1 hekan hekan    480 Nov 10  2007 TODO

drwxrwxrwx 2 hekan hekan   4096 Jan  8  2008 tools

[root@server1 rlwrap-0.30]# ./configure

......

checking for tgetent... no

checking for tgetent in -lcurses... no

checking for tgetent in -lncurses... no

checking for tgetent in -ltermcap... no

configure: WARNING: No termcap nor curses library found

checking for readline in -lreadline... no

configure: error:

You need the GNU readline library(ftp://ftp.gnu.org/gnu/readline/ ) to build

this program!

[root@server1 rlwrap-0.30]#

报错!提示需要安装readline-devel-5.1-1.1.i386.rpm包。
② 安装readline-devel-5.1-1.1.i386.rpm 包:

[root@server1 ~]# rpm -ivh readline-devel-5.1-1.1.i386.rpm

warning: readline-devel-5.1-1.1.i386.rpm: Header V3 DSA signature: NOKEY, key ID 37017186

error: Failed dependencies:

libtermcap-devel is needed by readline-devel-5.1-1.1.i386

[root@server1 ~]#

又报错提示缺失libtermcap-devel包!

继续安装:

[root@server1 ~]# rpm -ivh libtermcap-devel-2.0.8-46.1.i386.rpm

warning: libtermcap-devel-2.0.8-46.1.i386.rpm: Header V3 DSA signature: NOKEY, key ID 37017186

Preparing...                ########################################### [100%]

1:libtermcap-devel       ########################################### [100%]

[root@server1 ~]# rpm -ivh readline-devel-5.1-1.1.i386.rpm

warning: readline-devel-5.1-1.1.i386.rpm: Header V3 DSA signature: NOKEY, key ID 37017186

Preparing...                ########################################### [100%]

1:readline-devel         ########################################### [100%]

[root@server1 ~]# cd rlwrap-0.30

[root@server1 rlwrap-0.30]# ./configure

checking build system type... i686-pc-linux-gnu

checking host system type... i686-pc-linux-gnu

checking for a BSD-compatible install... /usr/bin/install -c

checking whether build environment is sane... yes

checking for a thread-safe mkdir -p... /bin/mkdir -p

checking for gawk... gawk

checking whether make sets $(MAKE)... yes

............

............

configure: creating ./config.status

config.status: creating Makefile

config.status: creating doc/Makefile

config.status: creating src/Makefile

config.status: creating doc/rlwrap.man

config.status: creating distribution/rlwrap.spec

config.status: creating config.h

config.status: executing depfiles commands

Now do:

make (or gmake)  to build rlwrap

make check       for instructions how to test it

make install     to install it

[root@server1 rlwrap-0.30]#

③ 根据执行执行,make,make install:

[root@server1 rlwrap-0.30]# make

make  all-recursive

......

[root@server1 rlwrap-0.30]# make install

Making install in doc

......

make[2]: Leaving directory `/root/rlwrap-0.30'

make[1]: Leaving directory `/root/rlwrap-0.30'

[root@server1 rlwrap-0.30]#

④ 修改oracle 用户的配置文件,

[oracle@server1 ~]$ pwd

/home/oracle

[oracle@server1 ~]$ vi .bash_profile

在其中,添加类似配置信息:

alias sqlplus='rlwrap sqlplus /nolog'

⑤至此,oracle在SHELL提示符下,只需要执行sqlplus就可以进入SQL*PLUS的命令行提示符,并且可以利用键盘的上下箭头调出之前执行过的历史命令。

这是在RHEL 5.4版本的操作系统上安装时的简单记录,而之前没遇到过类似的问题,故简单记录之。

即先安装libtermcap-devel-2.0.8-46.1.i386.rpm

然后安装 readline-devel-5.1-1.1.i386.rpm

最后安装rlwrap工具包。


How to solve ORA-12560 Error ?

记得,前几日,在一台RHEL as 5.4 server上,用Windows下的Oracle客户端访问该服务器上的Oracle服务时,遭遇ORA-12560级别的错误。当时,很是费解: ①确认Windows客户端可以正常与该服务器正常通信; ②确认该服务器上的Oracle监听程序已经正确开启; ③采用SSH2协议通过22端口连接到该服务器上去的时候,可以正常操纵数据库; 犹豫之际,难到Linux服务器上的防火墙问题? 果不其然,就是Oracle所在服务器上开启防火墙,并且没有开放1521端口。 给出解决办法: ①直接编辑(以root用户执行)/etc/sysconfig/system-config-securitylevel 配置文件: 如下是已经编辑之后的该文件:

[oracle@JM ~]$ cat /etc/sysconfig/system-config-securitylevel
# Configuration file for system-config-securitylevel
–enabled
–port=1521:tcp
[oracle@JM ~]$
[oracle@JM ~]$ cat /etc/sysconfig/system-config-securitylevel

# Configuration file for system-config-securitylevel

--enabled

--port=1521:tcp

[oracle@JM ~]$

如果,想要禁用防火墙,需要将该文件中的–enabled改成–disabled.如上,是启用防火墙,但是放开1521端口号,允许的协议是TCP协议,需要注意的是,该文件中的格式问题,即以–开始。 ②进入Linux的图形界面,进行操作。图形操作较为简单,在此不再赘述。 最终,Windows客户端可以正常访问该Linux服务器上的Oracle数据库!