ITPub博客

首页 > 数据库 > Oracle > [20160121]调式PL SQL.txt

[20160121]调式PL SQL.txt

原创 Oracle 作者:lfree 时间:2016-01-21 09:50:13 0 删除 编辑

[20160121]调式PL SQL.txt

--一上班,看到一个帖子,里面提到一些技巧,才想起来许多东西视乎在忘记。
--链接 :https://connormcdonald.wordpress.com/2016/01/20/problematic-sql-plsql-is-your-friend/

--我自己重复测试看看。

1.环境:
SCOTT@book> @&r/ver1
PORT_STRING                    VERSION        BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx            11.2.0.4.0     Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

create or replace procedure P is
  x int;
begin
--
-- this is my proc, and it has one very poor SQL
--
  select count(*)
  into   x
  from   dba_views;
                                                
  select count(*)
  into   x
  from   dba_tables;
                                                
  select count(*)
  into   x
  from   dba_objects;
                                                
  select count(*)
  into   x
  from   dba_objects, dba_objects;
                                                
end;
/

--要以上脚本正常执行,必须显式授权select权限。

SYS@book>  GRANT SELECT ON SYS.dba_views TO SCOTT;
Grant succeeded.

SYS@book>  GRANT SELECT ON SYS.dba_tables TO SCOTT;
Grant succeeded.

SYS@book>  GRANT SELECT ON SYS.dba_objects TO SCOTT;
Grant succeeded.

2.测试:
SCOTT@book> @ &r/spid

       SID    SERIAL# SPID       PID  P_SERIAL# C50
---------- ---------- ------ ------- ---------- --------------------------------------------------
        90        589 51753       32        216 alter system kill session '90,589' immediate;

SCOTT@book> exec p
--很慢....

--打开另外的会话。
SCOTT@book> select sid,username, sql_id from   v$session where  status = 'ACTIVE' and    last_call_et > 10 and    username is not null;
       SID USERNAME             SQL_ID
---------- -------------------- -------------
        90 SCOTT                ff35fbgz27513

SCOTT@book> @ &r/wait
P1RAW            P2RAW            P3RAW                    P1         P2         P3        SID    SERIAL#       SEQ# EVENT                                    STATE               WAIT_TIME_MICRO SECONDS_IN_WAIT
---------------- ---------------- ---------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------------------------------------- ------------------- --------------- ---------------
0000000062657100 0000000000000001 00               1650815232          1          0        104       1031         53 SQL*Net message to client                WAITED SHORT TIME                 3               0

SCOTT@book> select p1raw,p2raw,p3raw,p1,p2,p3,sid,serial#,seq#,event,state,wait_time_micro,seconds_in_wait,status from v$session where sid=90 ;
P1RAW            P2RAW            P3RAW                    P1         P2         P3        SID    SERIAL#       SEQ# EVENT                                    STATE               WAIT_TIME_MICRO SECONDS_IN_WAIT STATUS
---------------- ---------------- ---------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------------------------------------- ------------------- --------------- --------------- --------
0000000062657100 0000000000000001 00               1650815232          1          0         90        589         68 SQL*Net message from client              WAITED KNOWN TIME          97590648             153 ACTIVE

--从这里看仅仅SQL*Net message from client,实际上后台执行那条sql语句。具有一定的欺骗性,自己以后要注意。

SCOTT@book> @ &r/sqlid ff35fbgz27513
old   1: select sql_id,to_char(replace(sql_fulltext,chr(13),'')) sqltext  from v$sql where  sql_id = '&&1' and rownum<=1
new   1: select sql_id,to_char(replace(sql_fulltext,chr(13),'')) sqltext  from v$sql where  sql_id = 'ff35fbgz27513' and rownum<=1
old   3: select sql_id,to_char(replace(sql_text,chr(13),'')) sqltext  from dba_hist_sqltext where  sql_id = '&&1' and rownum<=1
new   3: select sql_id,to_char(replace(sql_text,chr(13),'')) sqltext  from dba_hist_sqltext where  sql_id = 'ff35fbgz27513' and rownum<=1
SQL_ID        SQLTEXT
------------- ----------------------------------------------
ff35fbgz27513 SELECT COUNT(*) FROM DBA_OBJECTS, DBA_OBJECTS

--可以找到这条sql语句。

SCOTT@book> select PROGRAM_ID, PROGRAM_LINE# from v$sql where  sql_id = 'ff35fbgz27513';
PROGRAM_ID PROGRAM_LINE#
---------- -------------
     89889            19

--通过这里可以知道在那个存储过程以及哪一行.

SCOTT@book> select * from dba_objects where object_id=89889;
OWNER  OBJECT_NAME SUBOBJECT_  OBJECT_ID DATA_OBJECT_ID OBJECT_TYPE         CREATED             LAST_DDL_TIME       TIMESTAMP           STATUS  T G S  NAMESPACE EDITION_NAME
------ ----------- ---------- ---------- -------------- ------------------- ------------------- ------------------- ------------------- ------- - - - ---------- ------------
SCOTT  P                           89889                PROCEDURE           2016-01-21 08:46:24 2016-01-21 08:51:56 2016-01-21:08:51:56 VALID   N N N          1

SCOTT@book> select * from dba_source where owner=user and name='P';
OWNER  NAME  TYPE       LINE TEXT
------ ----- ---------- ---- --------------------------------------------------
SCOTT  P     PROCEDURE     1 procedure P is
SCOTT  P     PROCEDURE     2   x int;
SCOTT  P     PROCEDURE     3 begin
SCOTT  P     PROCEDURE     4 --
SCOTT  P     PROCEDURE     5 -- this is my proc, and it has one very poor SQL
SCOTT  P     PROCEDURE     6 --
SCOTT  P     PROCEDURE     7   select count(*)
SCOTT  P     PROCEDURE     8   into   x
SCOTT  P     PROCEDURE     9   from   dba_views;
SCOTT  P     PROCEDURE    10
SCOTT  P     PROCEDURE    11   select count(*)
SCOTT  P     PROCEDURE    12   into   x
SCOTT  P     PROCEDURE    13   from   dba_tables;
SCOTT  P     PROCEDURE    14
SCOTT  P     PROCEDURE    15   select count(*)
SCOTT  P     PROCEDURE    16   into   x
SCOTT  P     PROCEDURE    17   from   dba_objects;
SCOTT  P     PROCEDURE    18
SCOTT  P     PROCEDURE    19   select count(*)
SCOTT  P     PROCEDURE    20   into   x
SCOTT  P     PROCEDURE    21   from   dba_objects, dba_objects;
SCOTT  P     PROCEDURE    22
SCOTT  P     PROCEDURE    23 end;
23 rows selected.

--从以上的测试,都能对上。

3.如何优化那条sql语句呢?

--当然这种笛卡尔积本来不是优化的可能。不过我想起以前好像可以通过no_merge不融合来获得好的效果。上面的笛卡尔结果太大。效果不明显。
--换一个人视图看看。

SCOTT@book> set timing on
SCOTT@book> SELECT    COUNT (*)  FROM dba_tables a , dba_tables b;
  COUNT(*)
----------
   8294400
Elapsed: 00:00:26.30

SCOTT@book> SELECT  /*+ NO_MERGE(A) NO_MERGE(B) */  COUNT (*)  FROM dba_tables a , dba_tables b;
  COUNT(*)
----------
   8294400
Elapsed: 00:00:00.48

--如果没有使用别名a,b。

SCOTT@book> SELECT  /*+ NO_MERGE(DBA_TABLES_0002@SEL$1) NO_MERGE(DBA_TABLES_0001@SEL$1) */  COUNT (*)  FROM dba_tables  , dba_tables ;
  COUNT(*)
----------
   8294400
Elapsed: 00:00:00.68

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/267265/viewspace-1979990/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
熟悉oracle相关技术,擅长sql优化,rman备份与恢复,熟悉linux shell编程。

注册时间:2008-01-03

  • 博文量
    2488
  • 访问量
    6294850