Archive

Archive for the ‘oracle’ Category

常用sql

February 9th, 2010 No comments
1、查看表空间的名称及大小

select t.tablespace_name, round(sum(bytes/(1024*1024)),0) ts_size
from dba_tablespaces t, dba_data_files d
where t.tablespace_name = d.tablespace_name
group by t.tablespace_name;
Read more…

Categories: 脚本 Tags:

用Oracle的 DBMS_XPLAN函数轻松格式化EXPLAIN PLAN结果

September 13th, 2009 No comments

EXPLAIN PLAN命令的输出结果是解决SQL效率低下问题的最重要的诊断工具之一。此命令加载一个工作表格(默认名称为PLAN_TABLE),表格中带有Oracle优化器为SQL语句计算的执行计划步骤。例如:

EXPLAIN PLAN FOR

SELECT empno, ename

FROM emp

WHERE ename LIKE ‘S%’;

由于PLAN_TABLE中的行形成了一个等级,对其进行查询需要应用SELECT语句中的两个复杂子语句——START WITH与CONNECT BY。第一次在Oracle 9i中出现并在10g中得到扩充的DBMS_XPLAN使格式化及显示执行计划更为容易。

DBMS_XPLAN.DISPLAY是一个表格标值函数,它显示PLAN_TABLE的内容。与常规的纯量函数SUBSTR或总计函数SUM不同的是,表格函数返回一个完整的行集合(rowset)。它们用在SELECT语句的FROM子语句中,前面必须加上TABLE这个词。如:

SELECT *

FROM TABLE(DBMS_XPLAN.DISPLAY);

列表A显示的是上述EXPLAIN PLAN语句的结果,并由DBMS_XPLAN.DISPLAY格式化。

值得注意的是,DBMS_XPLAN还能从存储在系统全局区(System Global Area, SGA)内的指针显示“实时”执行计划。列表B显示的是由用户SCOTT执行的同一SQL语句。通过查看V$SESSION视图,你可以找到执行SCOTT任务的最后一个SQL ID。这反过来又可输入到DBMS_XPLAN.DISPLAY_CURSOR中,获得指针所使用的执行计划。

最后,DBMS_XPLAN.DISPLAY_AWR函数可用来查寻Oracle 10g的自动负荷信息库(Automatic Workload Repository, AWR)获得的历史SQL语句,并显示它的执行计划。它显示一个你可以访问的含七天历史记录的滚动窗口。

所有这三个函数都是协调SQL语句的强大工具。

Categories: oracle, SQL优化, 执行计划 Tags:

Oracle分区表(Partition Table)的数量限制

September 10th, 2009 No comments
oracle分区表是不是有最大分区个数限制 ,我有一张大约20G的表,有好多分区(按时间),结果根据这个时间条件查询就报ORA-03113 end-of-file on communication channel错误,然后断开链接,硬件与网络都没问题. 后来我删了几个分区就一切OK了,这是咋回事呢? 系统环境 win2003

oracle分区表是不是有最大分区个数限制,我有一张大约20G的表,有好多分区(按时间),结果根据这个时间条件查询就报ORA-03113 end-of-file on communication channel错误,然后断开链接,硬件与网络都没问题. 后来我删了几个分区就一切OK了,这是咋回事呢? 系统环境 win2003 server + oracle9201

我猜测他这个问题并非遇到了分区个数的限制,因为这个限制很难达到,至少到目前为止我尚未听说有人达到这个分区限制。

那么Oracle最大允许存在多少个分区呢?

我们可以从Oracle的Concepts手册上找到这个信息,对于Oracle9iR2:

Tables can be partitioned into up to 64,000 separate partitions.

对于Oracle10gR2,Oracle增强了分区特性:

Tables can be partitioned into up to 1024K-1 separate partitions.

真的会有人尝试来突破这个分区极限吗? 你的数据库最大的分区表有多少个分区呢?

关于何时应该进行分区,Oracle有如下建议:

■ Tables greater than 2GB should always be considered for partitioning.
■ Tables containing historical data, in which new data is added into the newest partition. A typical example is a historical table where only the current month’s data is updatable and the other 11 months are read only.

 

Categories: oracle Tags:

oracle日期

July 30th, 2009 No comments

1)ww的算法为每年1月1日为第一周开始,date+6为每一周结尾   例如20090101为第一周的第一天,而第一周的最后一天为20050101+6=20050107 2)iw的算法为星期一至星期日算一周,且每年的第一个星期一为第一周,   例如20090101为星期四,所以用iw的算法是前年的53周,而20090106之后才是第一周的开始。

1》以12小时制显示
SQL>select to_char(sysdate,’YYYY-MM-DD HH12:MI:SS AM’)from dual;

TO_CHAR(SYSDATE,’YYYY-MM-DDHH1
——————————
2007-06-29 02:50:06 下午
Read more…

Categories: 编程, oracle Tags:

insert,append,nologging生成redo数量的测试

April 13th, 2009 No comments

SQL> select * from v$version;

BANNER
—————————————————————-
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 – Prod
PL/SQL Release 10.2.0.3.0 – Production
CORE    10.2.0.3.0      Production
TNS for 32-bit Windows: Version 10.2.0.3.0 – Production
NLSRTL Version 10.2.0.3.0 – Production

SQL> archive log list;
数据库日志模式             非存档模式
自动存档             禁用
存档终点            USE_DB_RECOVERY_FILE_DEST
最早的联机日志序列     163
当前日志序列           165
Read more…

Categories: oracle, tuning Tags: , , ,

在线重定义表的测试和问题

April 13th, 2009 No comments

在一个高可用系统中,如果需要改变一个表的定义是一件比较棘手的问题,尤其是对于7×24 系统。Oracle提供的基本语法基本可以满足一般性修改,但是对于把普通堆表改为分区表,把索引组织表修改为堆表等操作就无法完成了。而且,对于被大量 DML语句访问的表,幸运的是,Oracle从9i版本开始提供了在线重定义表功能,通过调用DBMS_REDEFINITION包,可以在修改表结构的同时允许DML操作。

1、在线重定义表具有以下功能:

(1)修改表的存储参数;

(2)可以将表转移到其他表空间;

(3)增加并行查询选项;

(4)增加或删除分区;

(5)重建表以减少碎片;

(6)将堆表改为索引组织表或相反的操作;

(7)增加或删除一个列。

Read more…

10g的一个新花样:skip_unusable_indexes

March 17th, 2009 No comments


关于skip_unusable_indexes这个参数,大家从它的名称就基本上可以理解它是做什么的,但在使用时需要注意一个小问题,非唯一索引在失效(unusable)状态是允许继续对表进行DML操作,但唯一索引在失效状态是不允许DML操作.

这个富有人性化的改进可以避免因为dba的失误而导致很多事务回退,当dba对表进行move操作时,如果表上没有唯一索引,那我们在move时,就不必担心事务回退,所以如果有些表如果insert/delete非重频繁的话,最好不要创建唯一索引.

10g中加强了对unusable索引的监控,如果你move了表,这时将会在alert日志中记录如下信息:Some indexes or index [sub]partitions of table SCOTT.DEPT have been marked unusable

Categories: index Tags:

关于Oracle几种锁管理的汇总

March 8th, 2009 No comments

Oracle里锁有以下几种模式:
    0:none
    1:null 空
    2:Row-S 行共享(RS):共享表锁,sub share
    3:Row-X 行独占(RX):用于行的修改,sub exclusive
    4:Share 共享锁(S):阻止其他DML操作,share
    5:S/Row-X 共享行独占(SRX):阻止其他事务操作,share/sub exclusive
    6:exclusive 独占(X):独立访问使用,exclusive
   
    数字越大锁级别越高, 影响的操作越多。
   
    1级锁有:Select,有时会在v$locked_object出现。
    2级锁有:Select for update,Lock For Update,Lock Row Share
    select for update当对话使用for update子串打开一个游标时,所有返回集中的数据行都将处于行级(Row-X)独占式锁定,其他对象只能查询这些数据行,不能进行update、delete或select for update操作。
    3级锁有:Insert, Update, Delete, Lock Row Exclusive
    没有commit之前插入同样的一条记录会没有反应, 因为后一个3的锁会一直等待上一个3的锁, 我们必须释放掉上一个才能继续工作。
    4级锁有:Create Index, Lock Share
    locked_mode为2,3,4不影响DML(insert,delete,update,select)操作, 但DDL(alter,drop等)操作会提示ora-00054错误。
    00054, 00000, “resource busy and acquire with NOWAIT specified”
    // *Cause: Resource interested is busy.
    // *Action: Retry if necessary.
    5级锁有:Lock Share Row Exclusive
    具体来讲有主外键约束时update / delete … ; 可能会产生4,5的锁。
    6级锁有:Alter table, Drop table, Drop Index, Truncate table, Lock Exclusive
   
    以DBA角色, 查看当前数据库里锁的情况可以用如下SQL语句:

col owner for a12
    col object_name for a16
    select b.owner,b.object_name,l.session_id,l.locked_mode
    from v$locked_object l, dba_objects b
    where b.object_id=l.object_id

    select t2.username,t2.sid,t2.serial#,t2.logon_time
    from v$locked_object t1,v$session t2
    where t1.session_id=t2.sid order by t2.logon_time
    如果有长期出现的一列,可能是没有释放的锁。我们可以用下面SQL语句杀掉长期没有释放非正常的锁:
   
    alter system kill session ‘sid,serial#’;
   
    如果出现了锁的问题, 某个DML操作可能等待很久没有反应。
   
    当你采用的是直接连接数据库的方式,也不要用OS系统命令 $kill process_num 或者 $kill -9 process_num来终止用户连接,因为一个用户进程可能产生一个以上的锁, 杀OS进程并不能彻底清除锁的问题。

Categories: lock Tags:

sql有绑定变量但是却不能共享的原因及如何诊断…About version_count

March 8th, 2009 No comments
2007-12-13 13:51
library cache 相关的latch 等待并都是因为sql语句没有绑定变量造成的!!!

也有很多种情况会造成相同的sql_id,相同的hash_value而不能share。
这通常都会造成很多的child cursor.

      
查询v$sqlarea中版本数大于10的所有sql的语句:
select sql_id,version_count, sql_text
from   v$sqlarea
where version_count > 10
order by version_count, hash_value;

通常有问题的时候某些sql_id 的 version_count 可能会大于1000,
如果对比的话,sql_text都是完全相同的,那么到底是什么造成如此多的version呢?

例如我们上面的查询找到了sql_id:10xj8nynmpqtq

则执行下面的查询:

select SQL_ID,CHILD_NUMBER,
UNBOUND_CURSOR,
SQL_TYPE_MISMATCH,
OPTIMIZER_MISMATCH,
OUTLINE_MISMATCH,
STATS_ROW_MISMATCH,
LITERAL_MISMATCH,
SEC_DEPTH_MISMATCH,
EXPLAIN_PLAN_CURSOR,
BUFFERED_DML_MISMATCH,
PDML_ENV_MISMATCH,
INST_DRTLD_MISMATCH,
SLAVE_QC_MISMATCH,
TYPECHECK_MISMATCH,
AUTH_CHECK_MISMATCH,
BIND_MISMATCH,
DESCRIBE_MISMATCH,
LANGUAGE_MISMATCH,
TRANSLATION_MISMATCH,
ROW_LEVEL_SEC_MISMATCH,
INSUFF_PRIVS,
INSUFF_PRIVS_REM,
REMOTE_TRANS_MISMATCH,
LOGMINER_SESSION_MISMATCH,
INCOMP_LTRL_MISMATCH
from v$sql_shared_cursor
where sql_id=’10xj8nynmpqtq’;

SQL_ID        CHILD_NUMBER U S O O S L S E B P I S T A B D L T R I I R L I
————- ———— – - – - – - – - – - – - – - – - – - – - – - – -
10xj8nynmpqtq            0 N N N N N N N N N N N N N N N N N N N N N N N N
10xj8nynmpqtq            1 N N N N N N N N N N N N N N N N N N N N N N N N
10xj8nynmpqtq            2 N N N N N N N N N N N N N N Y N N N N N N N N N
10xj8nynmpqtq            3 N N N N N N N N N N N N N N N N N N N N N N N N
10xj8nynmpqtq            4 N N N N N N N N N N N N N N N N N N N N N N N N
10xj8nynmpqtq            5 N N N N N N N N N N N N N N Y N N N N N N N N N
10xj8nynmpqtq            6 N N N N N N N N N N N N N N N N N N N N N N N N
10xj8nynmpqtq            7 N N N N N N N N N N N N N N Y N N N N N N N N N
10xj8nynmpqtq            8 N N N N N N N N N N N N N N N N N N N N N N N N
10xj8nynmpqtq            9 N N N N N N N N N N N N N N Y N N N N N N N N N
10xj8nynmpqtq           10 N N N N N N N N N N N N N N N N N N N N N N N N

SQL_ID        CHILD_NUMBER U S O O S L S E B P I S T A B D L T R I I R L I
————- ———— – - – - – - – - – - – - – - – - – - – - – - – -
10xj8nynmpqtq           11 N N N N N N N N N N N N N N N N N N N N N N N N
10xj8nynmpqtq           12 N N N N N N N N N N N N N N Y N N N N N N N N N
10xj8nynmpqtq           13 N N N N N N N N N N N N N N N N N N N N N N N N
10xj8nynmpqtq           14 N N N N N N N N N N N N N N Y N N N N N N N N N
10xj8nynmpqtq           15 N N N N N N N N N N N N N N N N N N N N N N N N

相应的 Y 列说明了该cursor不能共享的原因:

BIND_MISMATCH – The bind metadata does not match the existing child cursor

除了这些能够查询到的原因之外,还有可能所有的列都是 N ,但是仍然还是有很多version_count .

那么恭喜你中奖了,这种情况可能的BUG:

Bug 4458226 High version count with cursor_sharing=force

Affects:
Product (Component) Oracle Server (Rdbms)
Range of versions believed to be affected Versions < 11
Versions confirmed as being affected 10.1.0.4 ,10.2.0.1

Platforms affected Generic (all / most platforms affected)

Fixed:
This issue is fixed in 9.2.0.8 (Server Patch Set) ,10.1.0.5 (Server Patch Set)
10.2.0.2 (Server Patch Set) ,11g (Future version)

Bug 4701527 Cursors not shared when executing procedures over a dblink

Affects:
Product (Component) Oracle Server (Rdbms)
Range of versions believed to be affected Versions < 11
Versions confirmed as being affected 9.2.0.5 ,9.2.0.7 ,10.1.0.5

Platforms affected Generic (all / most platforms affected)

Fixed:
This issue is fixed in 9.2.0.8 (Server Patch Set) ,10.2.0.4 (Server Patch Set) ,11g (Future version)

Description
A cursor sharing problem exists for INSERT statements that insert into a remote table and are executed via a PLSQL module which itself is being executed remotely.

To be seeing this problem the following must be true:
1. Multiple child cursors are seen for an INSERT statement
2. V$SQL_SHARED_CURSOR will have the INST_DRTLD_MISMATCH column set to ‘Y’
   for the cursor
3. The insert statement is executed remotely via a PLSQL module that is
   executed remotely

Bug 3461251 V$SQL_SHARED_CURSOR shows all N with CURSOR_SHARING=FORCE

Affects:
Product (Component) Oracle Server (Rdbms)
Range of versions believed to be affected Versions < 11.2
Versions confirmed as being affected 9.2.0.4

Platforms affected Generic (all / most platforms affected)

Fixed:
This issue is fixed in 11.2 (Future Release)

Description
New child cursors may be created but V$SQL_SHARED_CURSOR does not have a ‘Y’ in any of the columns that denote the reason why the cursor could not be shared for the cursor in question. This is common when literal replacement is enabled and histograms exist on the columns that are predicates of the SQL statement and have undergone literal replacement.

This fix adds a new column to V$SQL_SHARED_CURSOR called HASH_MATCH_FAILED which is set to “Y” if sharing fails due to a hash mismatch, such as the case with mismatched histogram data.

Workaround:
Check if the unshared child cursors have histogram data on key columns.

Bug 5705795 Win/Linux: Many child cursors possible for SQL using BINDS

This bug is alerted in Note 403616.1
Affects:

    Product (Component)    Oracle Server (Rdbms)
    Range of versions believed to be affected    Versions >= 10.2.0.3 but < 10.2.0.4
    Versions confirmed as being affected   

        * 10.2.0.3

    Platforms affected   

        * Linux 32bit
        * Windows/NT/XP

     It is believed to be a regression in default behaviour thus:
       Regression introduced in 10.2.0.3

Fixed:

    This issue is fixed in   

        * 10.2.0.4 (Server Patch Set)

Symptoms:
   

Description

    This problem is introduced in 10.2.0.3 on Windows 32bit and Linux 32bit only by the fix for bug 4752541.

    10.2.0.3 on other platforms includes the fix for this bug# and so are not affected.

    SQL using bind variables with different bind sizes can lead to a large number of child cursors being
    created leading to excess shared pool usage and latch contention.

Categories: 排错 Tags: , , ,

碰到个Bug: GES: Potential blocker (pid=536584) on resource CI-0000001E-00000002

March 8th, 2009 No comments
2008-08-04 18:16

今天碰到的:

Rac其中一个节点sqlplus / as sysdba都会被挂起,另一个Node没有问题。

alertlog中有这样的信息:
Thread 2 advanced to log sequence 5327
Current log# 3 seq# 5327 mem# 0: /dev/rlvredo21
Mon Aug 4 15:39:00 2008
Thread 2 advanced to log sequence 5328
Current log# 4 seq# 5328 mem# 0: /dev/rlvredo22
Mon Aug 4 15:59:55 2008
GES: Potential blocker (pid=536584) on resource CI-0000001E-00000002;
enqueue info in file /oracle/app/oracle/admin/qasmdm/bdump/qasmdm2_lmd0_438324.trc and DIAG trace file

看trace文件:

[qasmdm2:oracle] /> more /oracle/app/oracle/admin/qasmdm/bdump/qasmdm2_lmd0_438324.trc
/oracle/app/oracle/admin/qasmdm/bdump/qasmdm2_lmd0_438324.trc
Oracle Database 10g Enterprise Edition Release 10.2.0.2.0 – 64bit Production
With the Partitioning, Real Application Clusters, OLAP and Data Mining options
ORACLE_HOME = /oracle/app/oracle/product/10.2.0
System name:    AIX
Node name:      PEKAX132
Release:        3
Version:        5
Machine:        00002A27D100
Instance name: qasmdm2
Redo thread mounted by this instance: 2
Oracle process number: 6
Unix process pid: 438324, image:
oracle@PEKAX132 (LMD0)

*** 2008-07-30 00:27:17.430
*** SERVICE NAME:() 2008-07-30 00:27:17.429
*** SESSION ID:(551.1) 2008-07-30 00:27:17.429
* kjdrchkdrm: found an RM request in the request queue
Transfer pkey 4294950913 to node 1
*** 2008-07-30 00:27:17.515
Single node DRM(1) – transfer pkey 4294950913 to 1 done
*** 2008-07-30 00:27:17.515
Single node DRM(1) – transfer pkey 4294950914 to 1 done
*** 2008-07-30 00:27:17.515
Single node DRM(1) – transfer pkey 4294950915 to 1 done
*** 2008-07-30 00:27:17.515
Single node DRM(1) – transfer pkey 4294950916 to 1 done
。。。
。。。
Single node DRM(7) – transfer pkey 4294950947 to 1 done
* kjdrchkdrm: found an RM request in the request queue
Transfer pkey 4294950948 to node 1
*** 2008-07-30 00:27:43.465
Single node DRM(9) – transfer pkey 4294950948 to 1 done
* kjdrchkdrm: found an RM request in the request queue
Transfer pkey 4294950956 to node 1
*** 2008-07-30 00:27:43.476
Single node DRM(11) – transfer pkey 4294950956 to 1 done
*** 2008-07-30 00:27:43.476
。。。
。。。
synca inc 4 lvl 1 from 0 rcvd (my inc,lvl: 0,0) (4/5.0.0)
m2s domain info received from 0 (4.6)
* m2s: domain 0 valid = 1 according to instance 0
received master 1 (4.6)
ftd received from node 0 (4/7.0.0)
all ftds received
synca inc 4 lvl 2 from 0 rcvd (my inc,lvl: 4,1) (4/7.0.0)
ftd received from node 0 (4/9.0.0)
all ftds received
synca inc 4 lvl 3 from 0 rcvd (my inc,lvl: 4,2) (4/13.0.0)
ftd received from node 0 (4/14.0.0)
all ftds received

PEKAX132:/>ps -ef|grep 536584
oracle 536584       1   0   Jul 30      – 7:11 ora_lck0_qasmdm2
    root 2064566 2101288   0 16:18:43 pts/6 0:00 grep 536584

来看看lck进程是干什么的:
我们知道:LCKn是一个可选进程,当在并行服务器模式可出现多个锁定进程以利于数据库通信,作用就是锁定当前Instan要使用的资源而避免冲突。

再看trace文件名对应的进程:qasmdm2_lmd0_438324.trc

Lock Manager Daemon ?LMDn
In parallel server environments Oracle synchronizes access to resources that are shared between instances using instance locks. Each instance manages a subset of the instance locks. The LMDn processes service requests for locally managed locks directly, and forward requests for remotely managed locks to the remote node. The LMDn processes are also responsible for the detection of distributed deadlocks. Multiple lock manager processes can be configured for each instance, but one is normally sufficient because the messages are passed asynchronously.

显然是由于Rac资源的锁造成的


因为时间比较紧,没有去细查究竟什么原因造成的这个锁,只是简单的Kill了进程.

可知的相关的bug可以参考:BUG 6271080

Categories: oracle, 排错 Tags: ,

Videos, Slideshows and Podcasts by Cincopa Wordpress Plugin