自动内存调整中真正决定自动调整的参数

SQL> select x.ksppinm NAME,y.ksppstvl value,x.ksppdesc describ

  2  from x$ksppi x, x$ksppcv y
  3  where x.inst_id=USERENV('Instance')
  4  and y.inst_id=USERENV('Instance')
  5  and x.indx=y.indx
  6  and x.ksppinm like '%pool_size%';

NAME                           VALUE                     DESCRIB
------------------------------ ------------------------- ---------------------------------------
_NUMA_pool_size                Not specified             aggregate size in bytes of NUMA pool
__shared_pool_size             1073741824                Actual size in bytes of shared pool
shared_pool_size               1073741824                size in bytes of shared pool
__large_pool_size              117440512                 Actual size in bytes of large pool
large_pool_size                117440512                 size in bytes of large pool
__java_pool_size               134217728                 Actual size in bytes of java pool
java_pool_size                 134217728                 size in bytes of java pool
__streams_pool_size            0                         Actual size in bytes of streams pool
streams_pool_size              0                         size in bytes of the streams pool
_io_shared_pool_size           4194304                   Size of I/O buffer pool from SGA
_backup_io_pool_size           1048576                   memory to reserve from the large pool
global_context_pool_size                                                                                                                                          Global Application Context Pool Size in Bytes
olap_page_pool_size            0                         size of the olap page pool in bytes

13 rows selected

这些由两个下划线开头的参数决定了当前的SGA的分配
这也是动态内存管理调整的参数,这些参数的更改也会
记录到spfile文件当中,在下一次数据库启动时仍然有效
通过create pfile from spfile我们可以看到如下内容:

jingyong.__db_cache_size=88080384
jingyong.__java_pool_size=4194304
jingyong.__large_pool_size=4194304
jingyong.__shared_pool_size=67108864
jingyong.__streams_pool_size=0
*.audit_file_dest='/u01/app/oracle/admin/jingyong/adump'
*.background_dump_dest='/u01/app/oracle/admin/jingyong/bdump'
*.compatible='10.2.0.1.0'
*.control_files='/u01/app/oracle/product/10.2.0/oradata/jingyong/control01.ctl','/u01/app/oracle/product/10.2.0/oradata/jingyong/control02.ctl','/u01/app/oracle/product/10.2.0/oradata/jingyong/control03.ctl'
*.core_dump_dest='/u01/app/oracle/admin/jingyong/cdump'
*.db_block_size=8192
*.db_domain=''
*.db_file_multiblock_read_count=16
*.db_name='jingyong'
*.db_recovery_file_dest='/u01/app/oracle/product/10.2.0/flash_recovery_area'
*.db_recovery_file_dest_size=2147483648
*.dispatchers='(PROTOCOL=TCP) (SERVICE=jingyongXDB)'
*.job_queue_processes=10
*.open_cursors=300
*.pga_aggregate_target=16777216
*.processes=150
*.remote_login_passwordfile='EXCLUSIVE'
*.sga_target=167772160
*.undo_management='AUTO'
*.undo_tablespace='UNDOTBS1'
*.user_dump_dest='/u01/app/oracle/admin/jingyong/udump'

还可以通过查询v$sga_dynamic_components视图来各动态内存组件的调整信息

SQL> select component,current_size,min_size,last_oper_type,last_oper_mode,last_oper_time from v$sga_dynamic_components;

COMPONENT                                                        CURRENT_SIZE   MIN_SIZE LAST_OPER_TYPE LAST_OPER_MODE LAST_OPER_TIME
---------------------------------------------------------------- ------------ ---------- -------------- -------------- --------------
shared pool                                                        1124073472 1073741824 SHRINK         DEFERRED       2012-12-27 16:
large pool                                                          117440512  117440512 STATIC
java pool                                                           134217728  134217728 STATIC
streams pool                                                                0          0 STATIC
DEFAULT buffer cache                                              11442061312 1114007142 GROW           DEFERRED       2012-12-27 16:
KEEP buffer cache                                                    50331648   50331648 STATIC
RECYCLE buffer cache                                                        0          0 STATIC
DEFAULT 2K buffer cache                                                     0          0 STATIC
DEFAULT 4K buffer cache                                                     0          0 STATIC
DEFAULT 8K buffer cache                                                     0          0 STATIC
DEFAULT 16K buffer cache                                                    0          0 STATIC
DEFAULT 32K buffer cache                                                    0          0 STATIC
ASM Buffer Cache                                                            0          0 STATIC

ORACLE SGA与共享内存的联系

SGA与共享内存

SGA的设置在linux/unix上和一个操作系统内核参数有关,这个参数是shmmax.
不同的操作系统中这个参数据设置的地方一样.在solaris上,这个参数是由
/etc/system文件中的shmsys:shminfo_shmmax来定义的.在linux上,该参数
由/proc/sys/kernel/shmmax参数定义.

shmmax内核参数的作用是系统允许的单个共享内存段的最大值.如果该参数
设置小于oracle sga的大小,那么sga仍然可以创建成功,但是会被分配成多
个共享内存段.通常建议通过调整shmmax的大小来使用sga限制在一个共享
内存段.

在windows系统中,由于系统采用多线程服务器(实际上所有的oracle server procees
都是一个进程中的线程),所以不存在共享内存的问题.无需进行特殊设置.

以32位linux平台为例,来看shmmax参数于数据库的影响.linux上该参数据的缺省值
一般是32M

[root@jingyong ~]# more /proc/sys/kernel/shmmax
33554432

操作系统的版本

[root@jingyong ~]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 5.4 (Tikanga)

通过ipcs命令可以查看在shmmax参数为缺省情况下共享内存的分配情况.
可以看到oracle分配了多个共享内存段来满足sga的设置要求:

[root@jingyong ~]# ipcs -sa

------ Shared Memory Segments --------
key        shmid      owner      perms      bytes      nattch     status
0x289516a4 32768      oracle    640        33554432  18
0x289516a4 32778      oracle    640        33554432  18
0x289516a4 32779      oracle    640        33554432  18
0x289516a4 32781      oracle    640        33554432  18
0x289516a4 32784      oracle    640        33554432  18
0x289516a4 32784      oracle    640        4194304  18
0x00000000 65537      gdm       600        393216     2          dest

------ Semaphore Arrays --------
key        semid      owner      perms      nsems
0x0bbc1610 98304      oracle    640        154

------ Message Queues --------
key        msqid      owner      perms      used-bytes   messages

从上面可以看到sga为160M(171966464字节)被分成了6个共享内存段.

对于每一个后台进程,使用pmap工具可以看到每个共享内存段的地址空间:

[root@jingyong ~]# ps -ef|grep dbw
oracle    2220     1  0 Dec26 ?        00:00:02 ora_dbw0_jingyong
root      3390  3309  0 02:39 pts/2    00:00:00 grep dbw

[root@jingyong ~]# pmap 2220
2220:   ora_dbw0_jingyong
00110000    344K r-x--  /u01/app/oracle/product/10.2.0/db/lib/libocrutl10.so
00166000     16K rwx--  /u01/app/oracle/product/10.2.0/db/lib/libocrutl10.so
0016a000      4K rwx--    [ anon ]
0016b000   1904K r-x--  /u01/app/oracle/product/10.2.0/db/lib/libnnz10.so
00347000    152K rwx--  /u01/app/oracle/product/10.2.0/db/lib/libnnz10.so
0036d000    432K rwx--    [ anon ]
003d9000     36K r-x--  /lib/libnss_files-2.5.so
003e2000      4K r-x--  /lib/libnss_files-2.5.so
003e3000      4K rwx--  /lib/libnss_files-2.5.so
00458000      4K r-x--    [ anon ]
00459000   1276K r-x--  /lib/libc-2.5.so
00598000      4K --x--  /lib/libc-2.5.so
00599000      8K r-x--  /lib/libc-2.5.so
0059b000      4K rwx--  /lib/libc-2.5.so
0059c000     12K rwx--    [ anon ]
005b4000    436K r-x--  /u01/app/oracle/product/10.2.0/db/lib/libocr10.so
00621000      4K rwx--  /u01/app/oracle/product/10.2.0/db/lib/libocr10.so
00622000   1032K rwx--    [ anon ]
00839000      4K rwx--    [ anon ]
0089f000    880K r-x--  /u01/app/oracle/product/10.2.0/db/lib/libhasgen10.so
0097b000     20K rwx--  /u01/app/oracle/product/10.2.0/db/lib/libhasgen10.so
00980000     12K rwx--    [ anon ]
0098f000      4K r-x--  /u01/app/oracle/product/10.2.0/db/lib/libskgxn2.so
00990000      4K rwx--  /u01/app/oracle/product/10.2.0/db/lib/libskgxn2.so
009fd000     36K rwx--    [ anon ]
00a5a000      4K rwx--    [ anon ]
00ab7000    128K r-x--  /u01/app/oracle/product/10.2.0/db/lib/libskgxp10.so
00ad7000      8K rwx--  /u01/app/oracle/product/10.2.0/db/lib/libskgxp10.so
00b01000     76K r-x--  /lib/libnsl-2.5.so
00b14000      4K r-x--  /lib/libnsl-2.5.so
00b15000      4K rwx--  /lib/libnsl-2.5.so
00b16000      8K rwx--    [ anon ]
00b36000     88K r-x--  /u01/app/oracle/product/10.2.0/db/lib/libdbcfg10.so
00b4c000      8K rwx--  /u01/app/oracle/product/10.2.0/db/lib/libdbcfg10.so
00bfd000    104K r-x--  /lib/ld-2.5.so
00c17000      4K r-x--  /lib/ld-2.5.so
00c18000      4K rwx--  /lib/ld-2.5.so
00c1b000      4K r-x--  /usr/lib/libaio.so.1.0.1
00c1c000      4K rwx--  /usr/lib/libaio.so.1.0.1
00cbc000     32K r-x--  /u01/app/oracle/product/10.2.0/db/lib/libclsra10.so
00cc4000      4K rwx--  /u01/app/oracle/product/10.2.0/db/lib/libclsra10.so
00d67000    148K r-x--  /lib/libm-2.5.so
00d8c000      4K r-x--  /lib/libm-2.5.so
00d8d000      4K rwx--  /lib/libm-2.5.so
00d90000      8K r-x--  /lib/libdl-2.5.so
00d92000      4K r-x--  /lib/libdl-2.5.so
00d93000      4K rwx--  /lib/libdl-2.5.so
00d96000     76K r-x--  /lib/libpthread-2.5.so
00da9000      4K r-x--  /lib/libpthread-2.5.so
00daa000      4K rwx--  /lib/libpthread-2.5.so
00dab000      8K rwx--    [ anon ]
00e33000    268K r-x--  /u01/app/oracle/product/10.2.0/db/lib/libocrb10.so
00e76000      4K rwx--  /u01/app/oracle/product/10.2.0/db/lib/libocrb10.so
00ee0000      4K rwx--    [ anon ]
00f07000      4K rwxs-  /u01/app/oracle/product/10.2.0/db/dbs/hc_jingyong.dat
00f76000      4K rwx--    [ anon ]
00f77000   7028K r-x--  /u01/app/oracle/product/10.2.0/db/lib/libjox10.so
01654000    260K rwx--  /u01/app/oracle/product/10.2.0/db/lib/libjox10.so
01695000      4K rwx--    [ anon ]
048b1000   1556K rwx--    [ anon ]
08048000  77032K r-x--  /u01/app/oracle/product/10.2.0/db/bin/oracle
0cb82000    324K rwx--  /u01/app/oracle/product/10.2.0/db/bin/oracle
0cbd3000    120K rwx--    [ anon ]
0e505000    456K rwx--    [ anon ]
20000000 167936K rwxs-    [ shmid=0x8000 ]
b7e6a000     64K rwx--  /dev/zero
b7e7a000     64K rwx--  /dev/zero
b7e8a000     64K rwx--  /dev/zero
b7e9a000     64K rwx--  /dev/zero
b7eaa000     64K rwx--  /dev/zero
b7eba000     64K rwx--  /dev/zero
b7eca000     64K rwx--  /dev/zero
b7eda000     64K --x--  /dev/zero
b7eea000     24K rwx--  /dev/zero
b7ef0000     64K rwx--  /dev/zero
b7f00000     64K rwx--  /dev/zero
b7f10000     64K rwx--  /dev/zero
b7f20000    128K rwx--  /dev/zero
b7f40000     64K rwx--  /dev/zero
b7f50000     64K rwx--  /dev/zero
b7f60000     40K rwx--  /dev/zero
bffb7000     84K rwx--    [ stack ]
 total   263492K

为了避免给sga分配多个共享内存段,可以修改shmmax内核参数,使用sga存在于一个
共享内存段中,可以通过修改/proc/sys/kernel/shmmax参数可以达到此目的

[root@jingyong ~]# echo 536870912> /proc/sys/kernel/shmmax
[root@jingyong ~]# more /proc/sys/kernel/shmmax
536870912

我修改了512M了,这里对于shmmax参数的修改不会永久的生效,在系统重新启动后
会失效.可以通过修改/etc/sysctl.conf文件来进行永久修改.
在/ect/sysctl.conf文件中增加以下一行这个更改在系统重启后仍然生效
kernel.shmmax=536870912

在修改shmmax参数后,重启数据库使更改生效
在重新启动数据库后我们再来查看共享内存的分配情况:

[root@jingyong ~]# ipcs -sa

------ Shared Memory Segments --------
key        shmid      owner      perms      bytes      nattch     status
0x289516a4 32768      oracle    640        171966464  18
0x00000000 65537      gdm       600        393216     2          dest

------ Semaphore Arrays --------
key        semid      owner      perms      nsems
0x0bbc1610 98304      oracle    640        154

------ Message Queues --------
key        msqid      owner      perms      used-bytes   messages

可以看到共享内存分配只分配了一个共享内存段

如果没有修改shmmax参数,oracle在启动过程中就会在alert_.log文件中记录警告:

Starting ORACLE instance(normal)
Thu Nov 21 16:02;02 2012
WARNING:EINVAL creating segment of size 0x0000000033400000
fix shm parameter in /etc/system or equivalent

这是一个警告提示,是建议修改shmmax参数不是强制

修改参数时scope参数值的作用

修改参数

可以通过alter system或者导入导出来更改spfile的内容
从oracle9i开始,alter system命令增加了一个新的选项scope,scope参数有3个可选值:memory,spfile和both
memory:只改变当前实例运行,重新启动数据库后失效
spfile:只改变spfile的设置,不改变当前实例运行,重新启动数据库后生效
both:同时改变实例及spfile,当前更改立即生效,重新启动数据库后仍然有效.
针对rac环境,alter system还可以指定sid参数,对不同实例进行不同设置.
所以通过spfile修改参数的完整命令如下:

alter system set = scope=memory|spfile|both [sid=]

带有scope=both参数的语句与不带scope参数的效果是一样的

当scope=memory时
修改当前实例的db_cache_advice参数为OFF;

SQL> show parameter db_cache_advice

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
db_cache_advice                      string      ON

SQL>alter system set db_cache_advice=off scope=memory;
System altered

SQL> show parameter db_cache_advice

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
db_cache_advice                      string      OFF

如果观察alert_.log文件,可以发现其中记录了如下一行

alter system set db_cache_advice=off scope=memory;

如果重新启动数据库,这个更改将会丢失

重置spfile参数
当想恢复参数为缺省值是可以使用以下命令:

alter system reset parameter  sid='sid|*';

在oracle服务器上如何去何从查询ora错误信息的描述

当遇到ora错误时需要查询错误信息是其实可以在数据库服务器上来查看

[root@jingyong ~]# su - oracle

[oracle@jingyong ~]$ cd $ORACLE_BASE

[oracle@jingyong oracle]$ oerr ora 30012
30012, 00000, "undo tablespace '%s' does not exist or of wrong type"
// *Cause:   the specified undo tablespace does not exist or of the
//           wrong type.
// *Action:  Correct the tablespace name and reissue the statement.

oracle启动实例时使用参数文件的顺序

oracle先会使用spfile.ora文件作为启动参数文件

如果该文件不存在就使用查找spfile.ora文件
如果spfile.ora与spfile.ora文件都不存在的话
就会使用init
.ora文件
如果上述三个文件都没有就没有办法启动oracle实例
先通过spfile文件来创建pfile文件

SQL> create pfile from spfile;

File created.

SQL> show parameter spfile

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
spfile                               string      /u01/app/oracle/product/10.2.0
                                                 /db/dbs/spfilejingyong.ora
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.

删除spfilejingyong.ora文件

[oracle@jingyong dbs]$ mv spfilejingyong.ora spfilejingyong.ora.bak

SQL> startup nomount
ORACLE instance started.

Total System Global Area  167772160 bytes
Fixed Size                  1218316 bytes
Variable Size              75499764 bytes
Database Buffers           88080384 bytes
Redo Buffers                2973696 bytes
SQL> show parameter spfile

NAME                                 TYPE
------------------------------------ ----------------------
VALUE
------------------------------
spfile                               string

从上面可以看去现在使用的是initjingyong.ora参数文件

ora_rowscn伪列的用途

ora_rowscn

对于每一行数据,ora_rowscn返回每一行最近被修改的大概时间.这对于判断一行数据
大概是在什么时间被修改的还是有用的.因oracle是通过事务提交对行所在数据块来
进行scn的跟踪的所以说它不精确.可以通过在创建表时使用行级别的依赖跟踪来获得
一个更加精确的scn.create table … norowdependencies|rowdependencies

在对视图进行查询时不能使用ora_rowscn.但对于视图的基表是可以使用ora_rowscn.
也能在update或delete语句中的where子句中使用ora_rowscn

ora_rowscn不能用于回闪查询,但是可以用回闪版本查询来代替ora_rowscn.

ora_rowscn也不能用于外部表

获取行被修改的大根的scn

SQL> select ora_rowscn,t.test_id from test_jy t;

ORA_ROWSCN               TEST_ID
---------- ---------------------
    625591                     3

通过scn来获取修改行记录大概的时间

SQL> select scn_to_timestamp(ora_rowscn),t.test_id from test_jy t;

SCN_TO_TIMESTAMP(ORA_ROWSCN)                                                                   TEST_ID
-------------------------------------------------------------------------------- ---------------------
20-12月-12 06.23.22.000000000 下午                                                                   3

也可以通过时间来获得scn

SQL> select timestamp_to_scn(scn_to_timestamp(ora_rowscn)),t.test_id from test_jy t;

TIMESTAMP_TO_SCN(SCN_TO_TIMEST               TEST_ID
------------------------------ ---------------------
                        625590                     3

oracle中sequence使用的限制

在使用序列的currval和nextval时的限制 创建一个序列

create sequence test_seq minvalue 1 maxvalue 10000000 start with 1 increment by 1 cache 20;

在delete,select,update语句的子查询中不能使用sequence的值

SQL> delete from test_jy where test_id < (select test_seq.currval from dual); 
delete from test_jy where test_id <(select test_seq.currval from dual) 
ORA-02287: 此处不允许序号 

SQL> select * from test_jy where test_id < (select test_seq.currval from dual); 
select * from test_jy where test_id <(select test_seq.currval from dual) 
ORA-02287: 此处不允许序号 

SQL> update test_jy set test_id=0 where test_id < (select test_seq.currval from dual); 
update test_jy set test_id=0 where test_id <(select test_seq.currval from dual) 
ORA-02287: 此处不允许序号 

在查询视图或物化视图时

SQL> select a.* from test_v a where a.userid

带有distinct操作符的select语句不能使用

SQL> select distinct a.*,test_seq.currval from test_v a ; 
select distinct a.*,test_seq.currval from test_v a 
ORA-02287: 此处不允许序号 

有group by,order by操作的select语句不能使用

SQL> select test_jy.*,test_seq.currval from test_jy group by test_jy.test_id; 
select test_jy.*,test_seq.currval from test_jy group by test_jy.test_id 
ORA-02287: 此处不允许序号 

SQL> select test_jy.*,test_seq.currval from test_jy order by test_jy.test_id; 
select test_jy.*,test_seq.currval from test_jy order by test_jy.test_id 
ORA-02287: 此处不允许序号 

有UNION, INTERSECT, MINUS操作符的语句不能使用

 
SQL> select test_jy.*,test_seq.currval from test_jy where test_id=1 
2 union 
3 select test_jy.*,test_seq.currval from test_jy where test_id=2;
select test_jy.*,test_seq.currval from test_jy where test_id=1 
union 
select test_jy.*,test_seq.currval from test_jy where test_id=2 
ORA-02287: 此处不允许序号 

SQL> select test_jy.*,test_seq.currval from test_jy where test_id=1 
2 intersect 
3 select test_jy.*,test_seq.currval from test_jy where test_id=2; 
select test_jy.*,test_seq.currval from test_jy where test_id=1 
intersect 
select test_jy.*,test_seq.currval from test_jy where test_id=2 
ORA-02287: 此处不允许序号 

SQL> select test_jy.*,test_seq.currval from test_jy where test_id=1 
2 minus 
3 select test_jy.*,test_seq.currval from test_jy where test_id=2; 
select test_jy.*,test_seq.currval from test_jy where test_id=1 
minus 
select test_jy.*,test_seq.currval from test_jy where test_id=2 
ORA-02287: 此处不允许序号 

在select语句中的where子句中

SQL> select test_jy.* from test_jy where test_id

在create table或alter table语句的中default值是不能使用sequence

SQL> alter table test_jy modify test_id number(20) default test_seq.currval; 
alter table test_jy modify test_id number(20) default test_seq.currval 
ORA-00984: 列在此处不允许 

还有就在check约束中不能使用

查看表空间及文件大小的语句

 SELECT d.tablespace_name TS_Name,

       d.contents TS_Type,
       d.status TS_Status,
       d.extent_management TS_ExtentManagement,
       trunc(NVL(a.bytes / 1024 / 1024, 0)) TS_Size,
       trunc(NVL(a.bytes - NVL(f.bytes, 0), 0) / 1024 / 1024) TS_UsedSize,
       trunc(NVL((a.bytes - NVL(f.bytes, 0)) / a.bytes * 100, 0)) TS_Used
  FROM sys.dba_tablespaces d,
       (select tablespace_name, sum(bytes) bytes
          from dba_data_files
         group by tablespace_name) a,
       (select tablespace_name, sum(bytes) bytes
          from dba_free_space
         group by tablespace_name) f
 WHERE d.tablespace_name = a.tablespace_name(+)
   AND d.tablespace_name = f.tablespace_name(+)
   AND NOT
        (d.extent_management like 'LOCAL' AND d.contents like 'TEMPORARY')
UNION ALL
SELECT d.tablespace_name TS_Name,
       d.contents TS_Type,
       d.status TS_Status,
       d.extent_management TS_ExtentManagement,
       trunc(NVL(a.bytes / 1024 / 1024, 0)) TS_Size,
       trunc(NVL(t.bytes, 0) / 1024 / 1024) TS_UsedSize,
       trunc(NVL(t.bytes / a.bytes * 100, 0)) TS_Used
  FROM sys.dba_tablespaces d,
       (select tablespace_name, sum(bytes) bytes
          from dba_temp_files
         group by tablespace_name) a,
       (select tablespace_name, sum(bytes_cached) bytes
          from v$temp_extent_pool
         group by tablespace_name) t
 WHERE d.tablespace_name = a.tablespace_name(+)
   AND d.tablespace_name = t.tablespace_name(+)
   AND d.extent_management like 'LOCAL'
   AND d.contents like 'TEMPORARY'
 order by TS_Name;
TS_NAME                        TS_TYPE   TS_STATUS TS_EXTENTMANAGEMENT TS_SIZE_MB TS_USEDSIZE_MB    TS_USED
------------------------------ --------- --------- ------------------- ---------- -------------- ----------
EXAMPLE                        PERMANENT ONLINE    LOCAL                      100             77         77
SYSAUX                         PERMANENT ONLINE    LOCAL                      240            238         99
SYSTEM                         PERMANENT ONLINE    LOCAL                      480            475         99
TEMP                           TEMPORARY ONLINE    LOCAL                       20             18         90
UNDOTBS1                       UNDO      ONLINE    LOCAL                       35             28         81
USERS                          PERMANENT ONLINE    LOCAL                        5              3         65



--查看数据文件大小
select a.tablespace_name,a.file_name,a.bytes/1024/1024 total_mb, (a.bytes - nvl(c.bytes, 0))/1024/1024 use_mb
  from (select a.*,
               d.STATUS file_status,
               a.increment_by * b.block_size extendbytes
          from dba_data_files a, dba_tablespaces b, v$datafile d
         where a.tablespace_name = b.tablespace_name
           and a.file_id = d.FILE#
           /*and a.file_id = :file_id*/) a
  left join (select file_id, sum(bytes) bytes
               from dba_free_space
              /*where file_id = :file_id*/
              group by file_id) c on a.file_id = c.file_id
union all
select a.tablespace_name,a.file_name,a.bytes/1024/1024 total_mb, c.bytes/1024/1024 use_mb
  from (select a.*,
               d.STATUS file_status,
               a.increment_by * b.block_size extendbytes
          from dba_temp_files a, dba_tablespaces b, v$tempfile d
         where a.tablespace_name= b.tablespace_name
           and a.file_id = d.FILE#
           /*and a.file_id = :file_id*/) a
  left join (select file_id, sum(bytes_cached) bytes
               from v$temp_extent_pool
              /*where file_id = :file_id*/
              group by file_id) c on a.file_id = c.file_id
TABLESPACE_NAME                FILE_NAME                                                                          TOTAL_MB     USE_MB
------------------------------ -------------------------------------------------------------------------------- ---------- ----------
SYSTEM                         D:\ORACLE\PRODUCT\10.2.0\ORADATA\JINGYONG\SYSTEM01.DBF                                  480      475.5
UNDOTBS1                       D:\ORACLE\PRODUCT\10.2.0\ORADATA\JINGYONG\UNDOTBS01.DBF                                  35    28.4375
USERS                          D:\ORACLE\PRODUCT\10.2.0\ORADATA\JINGYONG\USERS01.DBF                                     5       3.25
EXAMPLE                        D:\ORACLE\PRODUCT\10.2.0\ORADATA\JINGYONG\EXAMPLE01.DBF                                 100    77.6875
SYSAUX                         D:\ORACLE\PRODUCT\10.2.0\ORADATA\JINGYONG\SYSAUX01.DBF                                  240   238.8125
TEMP                           D:\ORACLE\PRODUCT\10.2.0\ORADATA\JINGYONG\TEMP01.DBF                                     20         18

查询中让优化器使用复合索引


create table MT_BIZ
(
  HOSPITAL_ID      VARCHAR2(20) not null,
  SERIAL_NO        VARCHAR2(16) not null,
  FEE_BATCH        NUMBER(5) default 1 not null,
  CASE_ID          NUMBER(12),
  BIZ_TYPE         VARCHAR2(2) not null,
  ORDINAL_NO       NUMBER(2) default 0 not null,
  DISTRICT_CODE    VARCHAR2(6),
  INDI_ID          NUMBER(12) not null,
  NAME             VARCHAR2(20) not null,
  SEX              CHAR(1) not null,
  PERS_TYPE        VARCHAR2(3) not null,
  OFFICE_GRADE     VARCHAR2(3) default '000' not null,
  IDCARD           VARCHAR2(25),
  IC_NO            VARCHAR2(25),
  BIRTHDAY         DATE,
  TELEPHONE        VARCHAR2(30),
  CORP_ID          NUMBER(10) not null,
  CORP_NAME        VARCHAR2(70) not null,
  TREATMENT_TYPE   VARCHAR2(3) not null,
  BIZ_TIMES        NUMBER(3),
  RELA_HOSP_ID     VARCHAR2(20),
  RELA_SERIAL_NO   VARCHAR2(16),
  SERIAL_APPLY     NUMBER(12),
  REG_DATE         DATE not null,
  REG_STAFF        VARCHAR2(8) not null,
  REG_MAN          VARCHAR2(20),
  REG_FLAG         CHAR(1) default '0' not null,
  BEGIN_DATE       DATE not null,
  REG_INFO         VARCHAR2(10),
  IN_DEPT          VARCHAR2(10),
  IN_DEPT_NAME     VARCHAR2(20),
  IN_AREA          VARCHAR2(10),
  IN_AREA_NAME     VARCHAR2(20),
  IN_BED           VARCHAR2(10),
  BED_TYPE         CHAR(1),
  PATIENT_ID       VARCHAR2(20),
  IN_DISEASE       VARCHAR2(20) not null,
  FOREGIFT         NUMBER(10,2),
  DIAGNOSE_DATE    DATE,
  DIAGNOSE         VARCHAR2(20),
  IN_DAYS          NUMBER(4),
  FIN_DISEASE      VARCHAR2(20),
  END_DATE         DATE,
  END_STAFF        VARCHAR2(8),
  END_MAN          VARCHAR2(30),
  FIN_INFO         VARCHAR2(10),
  IC_FLAG          CHAR(1) default '0' not null,
  REIMBURSE_FLAG   CHAR(1) default '0' not null,
  BIZ_END_DATE     DATE,
  FINISH_FLAG      CHAR(1) default '0' not null,
  POS_CODE         VARCHAR2(10),
  LOCK_FLAG        CHAR(1) default '0' not null,
  INJURY_BORTH_SN  NUMBER(12),
  REMARK           VARCHAR2(500),
  TRANS_FLAG       CHAR(1) default '0' not null,
  CENTER_ID        VARCHAR2(100) default 0 not null,
  PERS_TYPE_DETAIL VARCHAR2(3),
  CORP_TYPE_CODE   VARCHAR2(3),
  SPECIAL_CODE     VARCHAR2(3),
  DOCTOR_NO        VARCHAR2(20),
  DOCTOR_NAME      VARCHAR2(20),
  FIN_DISEASE1     VARCHAR2(20),
  FIN_DISEASE2     VARCHAR2(20),
  CASE_INFO        VARCHAR2(100),
  BILL_NO          VARCHAR2(20),
  HOS_SERIAL       VARCHAR2(30),
  DISEASE_TYPE     CHAR(1),
  DISEASE_FLAG     CHAR(1) default '0',
  RECUR_FLAG       CHAR(1) default '0',
  INJURY_TYPE      VARCHAR2(2) default '01'
);
-- Add comments to the table
comment on table MT_BIZ
  is '医疗业务表';
-- Add comments to the columns
comment on column MT_BIZ.HOSPITAL_ID
  is '医疗机构编号';
comment on column MT_BIZ.SERIAL_NO
  is '业务序列号';
comment on column MT_BIZ.FEE_BATCH
  is '费用批次';
comment on column MT_BIZ.CASE_ID
  is '病例分型序号';
comment on column MT_BIZ.BIZ_TYPE
  is '业务类别编号';
comment on column MT_BIZ.ORDINAL_NO
  is '内部序数';
comment on column MT_BIZ.DISTRICT_CODE
  is '社区编码(指个人所属行政区编码)';
comment on column MT_BIZ.INDI_ID
  is '个人编号';
comment on column MT_BIZ.NAME
  is '姓名';
comment on column MT_BIZ.SEX
  is '性别';
comment on column MT_BIZ.PERS_TYPE
  is '人员类别待遇代码';
comment on column MT_BIZ.OFFICE_GRADE
  is '公务员级别';
comment on column MT_BIZ.IDCARD
  is '公民身份号码';
comment on column MT_BIZ.IC_NO
  is 'IC卡号';
comment on column MT_BIZ.BIRTHDAY
  is '出生日期';
comment on column MT_BIZ.TELEPHONE
  is '联系电话';
comment on column MT_BIZ.CORP_ID
  is '单位编码';
comment on column MT_BIZ.CORP_NAME
  is '单位名称';
comment on column MT_BIZ.TREATMENT_TYPE
  is '待遇类别(用于区分同一业务类型的不同情况,比如生育门诊的三个月以上和三个月以上流产,不区分时为0)';
comment on column MT_BIZ.BIZ_TIMES
  is '本年业务次数';
comment on column MT_BIZ.RELA_HOSP_ID
  is '关联医疗机构编码';
comment on column MT_BIZ.RELA_SERIAL_NO
  is '关联业务序列号';
comment on column MT_BIZ.SERIAL_APPLY
  is '申请序列号';
comment on column MT_BIZ.REG_DATE
  is '业务登记日期';
comment on column MT_BIZ.REG_STAFF
  is '登记人工号';
comment on column MT_BIZ.REG_MAN
  is '登记人';
comment on column MT_BIZ.REG_FLAG
  is '登记标志(0:正常 1:转院 2:二次返院(审批通过后RELA_SERIAL_NO为空) 3:急诊留观转住院 4:90天或180天结算(处理后RELA_HOSP_ID为空,RELA_SERIAL_NO不为空))';
comment on column MT_BIZ.BEGIN_DATE
  is '业务开始时间';
comment on column MT_BIZ.REG_INFO
  is '业务开始情况(FR:提取冻结费用的零报业务  MW:医疗转工伤的零报业务)';
comment on column MT_BIZ.IN_DEPT
  is '入院科室';
comment on column MT_BIZ.IN_DEPT_NAME
  is '入院科室名称';
comment on column MT_BIZ.IN_AREA
  is '入院病区';
comment on column MT_BIZ.IN_AREA_NAME
  is '入院病区名称';
comment on column MT_BIZ.IN_BED
  is '入院床位号';
comment on column MT_BIZ.BED_TYPE
  is '床位类型';
comment on column MT_BIZ.PATIENT_ID
  is '医院业务号';
comment on column MT_BIZ.IN_DISEASE
  is '入院疾病诊断';
comment on column MT_BIZ.FOREGIFT
  is '预付款总额';
comment on column MT_BIZ.DIAGNOSE_DATE
  is '确诊日期';
comment on column MT_BIZ.DIAGNOSE
  is '确诊疾病诊断';
comment on column MT_BIZ.IN_DAYS
  is '住院天数';
comment on column MT_BIZ.FIN_DISEASE
  is '出院疾病诊断';
comment on column MT_BIZ.END_DATE
  is '业务终结日期';
comment on column MT_BIZ.END_STAFF
  is '终结人工号';
comment on column MT_BIZ.END_MAN
  is '终结人';
comment on column MT_BIZ.FIN_INFO
  is '业务终结情况';
comment on column MT_BIZ.IC_FLAG
  is '用卡标志';
comment on column MT_BIZ.REIMBURSE_FLAG
  is '中心报帐标志';
comment on column MT_BIZ.BIZ_END_DATE
  is '诊次结束时间';
comment on column MT_BIZ.FINISH_FLAG
  is '完成标志';
comment on column MT_BIZ.POS_CODE
  is 'POS机编号';
comment on column MT_BIZ.LOCK_FLAG
  is '锁定标志';
comment on column MT_BIZ.INJURY_BORTH_SN
  is '对应的工伤生育业务号';
comment on column MT_BIZ.REMARK
  is '备注';
comment on column MT_BIZ.TRANS_FLAG
  is '传输标志(0:未传输 1:已成功传输 2:未成功传输)';
comment on column MT_BIZ.CENTER_ID
  is '医保中心编码';
comment on column MT_BIZ.PERS_TYPE_DETAIL
  is '人员类别详细代码(bs_person.PERS_TYPE)';
comment on column MT_BIZ.CORP_TYPE_CODE
  is '单位类型';
comment on column MT_BIZ.SPECIAL_CODE
  is '特殊人群编码';
comment on column MT_BIZ.DOCTOR_NO
  is '医生编号';
comment on column MT_BIZ.DOCTOR_NAME
  is '医生姓名';
comment on column MT_BIZ.FIN_DISEASE1
  is '第一副诊断';
comment on column MT_BIZ.FIN_DISEASE2
  is '第二副诊断';
comment on column MT_BIZ.CASE_INFO
  is '病历信息';
comment on column MT_BIZ.BILL_NO
  is '单据号';
comment on column MT_BIZ.HOS_SERIAL
  is '医院交易流水号';
comment on column MT_BIZ.DISEASE_TYPE
  is '病种分型(A:病种单纯 B:严重 C:严重并发 D:危重)';
comment on column MT_BIZ.DISEASE_FLAG
  is '职业病标志(0 不是职业病,1 是职业病)';
comment on column MT_BIZ.RECUR_FLAG
  is '工伤复发标志(0 不是工伤复发,1 是工伤复发)';
comment on column MT_BIZ.INJURY_TYPE
  is '工伤类别(01:新工伤,02老工伤,对应新增wi_injury_type码表)';
-- Create/Recreate primary, unique and foreign key constraints
alter table MT_BIZ
  add constraint PK_MT_BIZ primary key (HOSPITAL_ID, SERIAL_NO)
  using index;
-- Create/Recreate indexes
create index IDX_MT_BIZ_BEGIN_DATE on MT_BIZ (BEGIN_DATE);
create index IDX_MT_BIZ_BIZ_END_DATE on MT_BIZ (BIZ_END_DATE);
create index IDX_MT_BIZ_CORP_ID on MT_BIZ (CORP_ID);
create index IDX_MT_BIZ_IB on MT_BIZ (INJURY_BORTH_SN);
create index IDX_MT_BIZ_INDI_ID on MT_BIZ (INDI_ID);
create index IDX_MT_BIZ_RELA on MT_BIZ (RELA_HOSP_ID, RELA_SERIAL_NO);

在mt_biz表中有一个复合主键是由hospital_id与serial_no组成的
其中serial_no是唯一值是由序列生成的,所以在查询数据时有些语句只使用serial_no
造成了使用不上索引的问题
因为如果索引是建立在多个列上, 只有在它的第一个列也叫前导列(leading
column)被where子句引用时,优化器才会选择使用该索引.
例如,不使用合主键的唯一索引中的前导列hospital_id时的语句执行计划如下

SQL> set autotrace traceonly;
SQL> select * from mt_biz a where a.serial_no='15485197';


执行计划
----------------------------------------------------------
Plan hash value: 3513793642

----------------------------------------------------------------------------
| Id  | Operation         | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |        |     1 |   244 |    42   (0)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| MT_BIZ |     1 |   244 |    42   (0)| 00:00:01 |
----------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("A"."SERIAL_NO"='15485197')


统计信息
----------------------------------------------------------
          1  recursive calls
          0  db block gets
        190  consistent gets
          0  physical reads
          0  redo size
       2852  bytes sent via SQL*Net to client
        239  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed

从显示的执行计划可以看出当没有使用复合索引中的前导列hospital_id时
是执行的全表扫描

当使用复合索引中的前导列hospital_id时

SQL> select * from mt_biz a where a.hospital_id='4307000009';

已选择348行。


执行计划
----------------------------------------------------------
Plan hash value: 3033165289

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

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

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

|   0 | SELECT STATEMENT            |           |    18 |  4392 |     8   (0)| 00:00:01 |

|   1 |  TABLE ACCESS BY INDEX ROWID| MT_BIZ    |    18 |  4392 |     8   (0)| 00:00:01 |

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

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


Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("A"."HOSPITAL_ID"='4307000009')


统计信息
----------------------------------------------------------
          1  recursive calls
          0  db block gets
        345  consistent gets
          0  physical reads
          0  redo size
     102775  bytes sent via SQL*Net to client
        400  bytes received via SQL*Net from client
         25  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
        348  rows processed

给出的执行计划是通过INDEX RANGE SCAN来执行查询

当使用复合索引中所有列时

SQL> select * from mt_biz a where a.hospital_id='4307000009' and a.serial_no='15485197';


执行计划
----------------------------------------------------------
Plan hash value: 2316229530

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

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

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

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

|   1 |  TABLE ACCESS BY INDEX ROWID| MT_BIZ    |     1 |   244 |     1   (0)| 00:00:01 |

|*  2 |   INDEX UNIQUE SCAN         | PK_MT_BIZ |     1 |       |     1   (0)| 00:00:01 |

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


Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("A"."HOSPITAL_ID"='4307000009' AND "A"."SERIAL_NO"='15485197')


统计信息
----------------------------------------------------------
          1  recursive calls
          0  db block gets
          3  consistent gets
          0  physical reads
          0  redo size
       2818  bytes sent via SQL*Net to client
        232  bytes received via SQL*Net from client
          1  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed

给出的执行计划是使用INDEX UNIQUE SCAN索引扫描来查询
所以当创建复合索引后如果查询要想使用这个复合索引就必须在查询条件中
使用复合索引的前导列才会让优化器使用这个复合索引

回闪查询查询删除的数据

oracle回闪技术:代替按时间点恢复
oracle回闪技术提供了一组功能它有效的代替了查看数据过去的状态和数据修改前后的状态而不用要你从备份中还原大部分数据或执行按时间点恢复.回闪技术与介质恢复相比更有效和更小的损坏数据.
大多数据回闪技术功能是在逻辑级别的操作,象查看和维护数据库对象
回闪查询:让你指定一个目标时间然后运行查询,来查看在那个时间出现的查询结查.为了从一个不想要发生的改变象用户错误的更新一个,用户可以选择在出错前的某个时间点运行查询来检索丢失或改变的记录.

回闪版本查询:让你可以查询在一个指定时间间隔内一个表或多个表永久存的所有记录的所有版本.象在表上执行的所有更新.也可能检索行记录不同版本的元数据象开始时间,结束时间,进行的操作和这个事务的事务ID创建的版本.这个功能也用来恢复丢失的数据和查询审计表的改变.

回闪事务查询:让你可能查看由单个事务发生的改变或在一个时间周期内所有事务发生的改变.

回闪表:能让你将表还原到过去某一个时间点.你能在数据库联机的情况下还原表数据,而仅仅回滚你指定的表的数据.

回闪删除:能拆消drop table语句的影响

回闪表,回闪查询,回闪事务查询和回闪版本查询都是要依赖重做数据.重做数据是用来记录数据库中每一个更新操作的数据的.使用它的主要目的是用来给查询提供一至性读取和回滚事务。

回闪删除是建交在一种叫做回收站机制之上的,oracle使用它来管理删除的数据库对象,直到回收站空间不足以要给新对象分配空间时才会清除.
查询一个表的过去状态可以使用select as of子句来完成.

select *
 from lv_insr_topay
    as of timestamp (sysdate - 40/1440)
    where corp_id=777;--删除前

    select *
 from lv_insr_topay
    as of timestamp (sysdate - 10/1440)
    where corp_id=777;--删除后


select *
 from lv_insr_topay
    as of timestamp to_timestamp('2011-12-26 15:00:00','yyyy-mm-dd hh24:mi:ss')
    where corp_id=777;--删除前

    select *
 from lv_insr_topay
    as of timestamp to_timestamp('2011-12-26 15:09:00','yyyy-mm-dd hh24:mi:ss')
    where corp_id=777;--删除后


select count(*) from zw2004.gl_pznr versions
  between  timestamp to_timestamp('2012-01-15 10:40:01','yyyy-mm-dd hh24:mi:ss') and
 to_timestamp('2012-01-17 09:50:00','yyyy-mm-dd hh24:mi:ss');

回闪版本查询

select versions_starttime,versions_endtime, versions_xid,versions_operation,POLICY_CODE,POLICY_VALUE,CENTER_OR_HOSP from fc_biz_policy  versions  between  timestamp to_timestamp('2012-04-19 10:40:01','yyyy-mm-dd hh24:mi:ss') and
 to_timestamp('2012-04-22 09:50:00','yyyy-mm-dd hh24:mi:ss') where policy_code='use_IC'

回闪事务查询

select * from flashback_transaction_query a where a.table_owner='ZW2004' and a.table_name='GL_PZNR'

回闪删除掉的表

flashback table jhk_map_center to before drop