Oracle执行计划

  建立与oracle的web程序,经常性出现sql性能不高导致的问题,比如程序好好的突然数据库查询变得很慢,几乎加载不了,这时候就有可能是oracle查询计划出错的原因。

 <sql id="queryCalendarBySeqnos">

        SELECT * FROM(
select /*+ leading(ii) index(ii CALENDAR_INVITE_IDX1) */ ii.RECMYSMS,ii.RECMYEMAIL,ii.RECMOBILE,ii.RECEMAIL,ii.enable,ii.status,
case(ii.inviteauth) when -1 then 0 else 1 end as isInvitedCalendar,
0 as isSharedCalendar,
0 as isSubCalendar,
nvl(l.labelname,sl.labelname) as labelname,
nvl(l.color,sl.color) as color,
nvl(l.gid,sl.gid) as lableGid,
i.createtime,
<include refid="calendarInfoColumnNamesForSelf"/>
FROM calendar_info i, calendar_invite_info ii,
calendar_label l,calendar_sys_label sl
WHERE
i.seqno = ii.calseqno
and i.labelid = l.seqno(+)
and i.labelid = sl.seqno(+)
AND ii.inviteruin = #uin#
and i.isdelflag = 0
and ii.isdelflag = 0
<![CDATA[
AND ii.status <> 2
]]>
AND i.seqno in
<iterate property="gids" open="(" close=")" conjunction=",">
#gids[]#
</iterate> UNION ALL select /*+ leading(ls) index(ls CALENDAR_LABEL_SHARE_IDX2) */ 0 as RECMYSMS,0 as RECMYEMAIL,'' as RECMOBILE,'' as RECEMAIL,0 as enable,1,
0 as isInvitedCalendar,
1 as isSharedCalendar,
0 as isSubCalendar,
l.labelname,ls.color,
l.gid as lableGid,
i.createtime,
<include refid="calendarInfoColumnNames"/>
from calendar_info i, calendar_label_share_info ls,
calendar_label l
<![CDATA[
where
i.labelid = ls.labelid
and i.labelid = l.seqno
and ls.sharetype <> -1
and ls.shareuin = #uin#
and ls.status = 1
and i.isdelflag = 0 ]]>
AND i.seqno in
<iterate property="gids" open="(" close=")" conjunction=",">
#gids[]#
</iterate> UNION ALL select /*+ leading(cs) index(cs UNI_CALENDAR_SUBSCRIPTION) */ 0 as RECMYSMS,0 as RECMYEMAIL,'' as RECMOBILE,'' as RECEMAIL,0 as enable,1,
0 as isInvitedCalendar,
0 as isSharedCalendar,m
1 as isSubCalendar,
l.labelname,cs.color,
l.gid as lableGid,
i.createtime,
<include refid="calendarInfoColumnNames"/>
from calendar_info i, calendar_subscription cs,
calendar_label l
<![CDATA[
where
i.labelid = cs.labelid
and i.labelid = l.seqno
and cs.uin=#uin#
and l.isPublic=1
and i.isdelflag = 0
]]>
AND i.seqno in
<iterate property="gids" open="(" close=")" conjunction=",">
#gids[]#
</iterate>
)
</sql>

如上图的代码,就出现了问题。

所以学一下oracle的查询计划吧。

1、设置oracle查询计划,如下步骤:

Oracle执行计划

Oracle执行计划

2、在sql-plus查询sql的执行计划,如图:

Oracle执行计划

SQL> explain plan for select count(*) from   calendar01.calendar_label@to_calendar.LOCALDOMAIN  where    istodoemail =1  and color <>'#f2b73a';

Explained

SQL>
SQL>
SQL>
SQL> SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY); PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 4083529702
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| T
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT REMOTE| | 1 | 21 | 6 (0)| 0
| 1 | SORT AGGREGATE | | 1 | 21 | |
|* 2 | TABLE ACCESS FULL | CALENDAR_LABEL | 12 | 252 | 6 (0)| 0
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter("A1"."ISTODOEMAIL"=1 AND "A1"."COLOR"<>'#f2b73a')
Note
-----
- fully remote statement 18 rows selected SQL>

我们需要掌握它的执行先后顺序和每个字段代表的含义。

 执行计划的执行顺序为: 
先从计划开头一直往右看,直到最右边并列的代码部分,如果见到并列的,就从上往下看,对于并列的步骤,靠上的先执行,对于不并列的步骤,靠右的先执行
 

中文参照:http://www.cnblogs.com/kerrycode/archive/2012/05/24/2517210.html

英文参照:http://perumal.org/how-to-read-an-oracle-sql-execution-plan/

上一篇:飞漫魏永明:从MiniGUI看嵌入式十年得与失


下一篇:C# → 数据库