📄 oracle sql性能优化系列 (十)--acnow_net.htm
字号:
<DIV id=article>
<TABLE cellSpacing=0 cellPadding=0 width=540 border=0 style="TABLE-LAYOUT: fixed">
<TBODY>
<TR>
<TH class=f24><FONT color=#05006c>
<H1>ORACLE SQL性能优化系列 (十)</H1>
</FONT></TH>
</TR>
<TR>
<TD> <HR SIZE=1 bgcolor="#d9d9d9"> </TD>
</TR>
<TR>
<TD align=middle height=20>http://Tech.acnow.net 2005-4-29 <FONT color=#a20010>网络</FONT></TD>
</TR>
<TR>
<TD height=15></TD>
</TR>
<TR>
<TD class=l17><FONT class=f14 id=zoom><BR>31. 强制索引失效<BR><BR> <BR><BR> <BR><BR>如果两个或以上索引具有相同的等级,你可以强制命令ORACLE优化器使用其中的一个(通过它,检索出的记录数量少) .<BR><BR> <BR><BR>举例:<BR><BR> <BR><BR>SELECT ENAME<BR><BR>FROM EMP<BR><BR>WHERE EMPNO = 7935 <BR><BR>AND DEPTNO + 0 = 10 /*DEPTNO上的索引将失效*/<BR><BR>AND EMP_TYPE || ‘’ = ‘A’ /*EMP_TYPE上的索引将失效*/<BR><BR> <BR><BR>这是一种相当直接的提高查询效率的办法. 但是你必须谨慎考虑这种策略,一般来说,只有在你希望单独优化几个SQL时才能采用它.<BR><BR> <BR><BR>这里有一个例子关于何时采用这种策略, <BR><BR> <BR><BR>假设在EMP表的EMP_TYPE列上有一个非唯一性的索引而EMP_CLASS上没有索引. <BR><BR> <BR><BR>SELECT ENAME<BR><BR>FROM EMP<BR><BR>WHERE EMP_TYPE = ‘A’ <BR><BR>AND EMP_CLASS = ‘X’;<BR><BR> <BR><BR>优化器会注意到EMP_TYPE上的索引并使用它. 这是目前唯一的选择. 如果,一段时间以后, 另一个非唯一性建立在EMP_CLASS上,优化器必须对两个索引进行选择,在通常情况下,优化器将使用两个索引并在他们的结果集合上执行排序及合并. 然而,如果其中一个索引(EMP_TYPE)接近于唯一性而另一个索引(EMP_CLASS)上有几千个重复的值. 排序及合并就会成为一种不必要的负担. 在这种情况下,你希望使优化器屏蔽掉EMP_CLASS索引.<BR><BR>用下面的方案就可以解决问题.<BR><BR>SELECT ENAME<BR><BR>FROM EMP<BR><BR>WHERE EMP_TYPE = ‘A’ <BR><BR>AND EMP_CLASS||’’ = ‘X’;<BR><BR> <BR><BR>32. 避免在索引列上使用计算.<BR><BR>WHERE子句中,如果索引列是函数的一部分.优化器将不使用索引而使用全表扫描.<BR><BR> <BR><BR>举例:<BR><BR> <BR><BR>低效:<BR><BR>SELECT …<BR><BR>FROM DEPT<BR><BR>WHERE SAL * 12 > 25000;<BR><BR> <BR><BR>高效:<BR><BR>SELECT …<BR><BR>FROM DEPT<BR><BR>WHERE SAL > 25000/12;<BR><BR> <BR><BR>译者按:<BR><BR>这是一个非常实用的规则,请务必牢记<BR><BR> <BR><BR>33. 自动选择索引<BR><BR>如果表中有两个以上(包括两个)索引,其中有一个唯一性索引,而其他是非唯一性.<BR><BR>在这种情况下,ORACLE将使用唯一性索引而完全忽略非唯一性索引.<BR><BR> <BR><BR>举例:<BR><BR>SELECT ENAME<BR><BR>FROM EMP<BR><BR>WHERE EMPNO = 2326 <BR><BR>AND DEPTNO = 20 ;<BR><BR> <BR><BR>这里,只有EMPNO上的索引是唯一性的,所以EMPNO索引将用来检索记录.<BR><BR>TABLE ACCESS BY ROWID ON EMP<BR><BR> INDEX UNIQUE SCAN ON EMP_NO_IDX<BR><BR> <BR><BR>34. 避免在索引列上使用NOT<BR><BR>通常, 我们要避免在索引列上使用NOT, NOT会产生在和在索引列上使用函数相同的<BR><BR>影响. 当ORACLE”遇到”NOT,他就会停止使用索引转而执行全表扫描.<BR><BR> 举例:<BR><BR> <BR><BR> 低效: (这里,不使用索引)<BR><BR> <BR><BR> SELECT …<BR><BR> FROM DEPT<BR><BR> WHERE DEPT_CODE NOT = 0;<BR><BR> <BR><BR> 高效: (这里,使用了索引)<BR><BR> <BR><BR> SELECT …<BR><BR> FROM DEPT<BR><BR> WHERE DEPT_CODE > 0;<BR><BR> <BR><BR> 需要注意的是,在某些时候, ORACLE优化器会自动将NOT转化成相对应的关系操作符.<BR><BR> NOT > to <=<BR><BR> NOT >= to <<BR><BR> NOT < to >=<BR><BR> NOT <= to ><BR><BR> <BR><BR> <BR><BR>译者按:<BR><BR> 在这个例子中,作者犯了一些错误. 例子中的低效率SQL是不能被执行的.<BR><BR>我做了一些测试:<BR><BR> <BR><BR>SQL> select * from emp where NOT empno > 1;<BR><BR>no rows selected<BR><BR>Execution Plan<BR><BR>----------------------------------------------------------<BR><BR> 0 SELECT STATEMENT Optimizer=CHOOSE<p><div align="right">本新闻共<font color=red>2</font>页,当前在第<font color=red>1</font>页 <font color="red">1</font> <a href="164559059816455944859_2.shtml">2</a> </div></p>
<P clear=all></P>
</FONT></TD>
</TR>
</TBODY>
</TABLE>
</DIV>
<BR>
上一篇:<a href='../../2005-4/29/164559059816455982497.shtml' title ='Oracle 10G Beta版在Win2000上安装的Bug'>Oracle 10G Beta版在Win2000上安装的Bug</a> 下一篇:<a href='../../2005-4/29/164559059816455943386.shtml' title ='Oracle中如何快速删除数据字典管理的表空间'>Oracle中如何快速删除数据字典管理的表空间</a>
<BR>
<BR>
<TABLE class="tf" cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD style="PADDING-RIGHT: 20px" align=right>
【<script type="text/javascript">
<!--
document.write("<a href=\"http:\/\/tech.acnow.net\/Sendmail.asp?NewsID=059816455944859\" target=\"_blank\">发送给好友</a>");
-->
</script>】
【<A href="http://bbs.acnow.net/Index.asp?boardid=103">论坛</A>】
【<script type="text/javascript">
<!--
document.write("<a target=\"_blank\" Href=\"http:\/\/tech.acnow.net\/Users\/AddFavorite.asp?NewsID=8991\" style=\"CURSOR:hand\" onClick=\"window.external.AddFavorite(document.location.href,document.title)\" onMouseMove=\"status='收藏本页';\" onMouseOut=\"status='';\">添加到收藏夹</a>");
-->
</script>】
【<A target="_self" href="javascript:doZoom(16)">大</A>
<A target="_self" href="javascript:doZoom(14)">中</A>
<A target="_self" href="javascript:doZoom(12)">小</A>】
【<A target="_self" href="javascript:doPrint()">打印</A>】
【<A target="_self" href="javascript:window.close()">关闭</A>】</TD>
</TR>
</TBODY>
</TABLE>
<TABLE cellSpacing=0 cellPadding=0 width=545 border=0>
<TBODY>
<TR>
<TD height=19></TD>
</TR>
<TR>
<TD bgColor=#c6c9d1 height=1></TD>
</TR>
<TR>
<TD height=10></TD>
</TR>
</TBODY>
</TABLE>
<BR>
<DIV id=links>
<TABLE cellSpacing=0 cellPadding=0 width=542 border=0>
<TBODY>
<TR>
<TD><table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
<td><img src="http://tech.acnow.net/Files/Img/list_nav_spacer.gif"><a Class="a2" href= http://tech.acnow.net/Html/DataBase/Oracle/2005-4/29/164558059816455810544.shtml title="ORACLE SQL性能优化系列 (一)">ORACLE SQL性能优化系列 (一)</a></td></tr>
<tr>
<td Height=1 colspan="1">
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td Height=1 background="http://tech.acnow.net/Files/Img/title_line.gif">
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td><img src="http://tech.acnow.net/Files/Img/list_nav_spacer.gif"><a Class="a2" href= http://tech.acnow.net/Html/DataBase/Oracle/2005-4/29/164559059816455980543.shtml title="ORACLE SQL性能优化系列 (十二)">ORACLE SQL性能优化系列 (十二)</a></td></tr>
<tr>
<td Height=1 colspan="1">
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td Height=1 background="http://tech.acnow.net/Files/Img/title_line.gif">
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td><img src="http://tech.acnow.net/Files/Img/list_nav_spacer.gif"><a Class="a2" href= http://tech.acnow.net/Html/DataBase/Oracle/2005-4/29/16455905981645592205.shtml title="ORACLE SQL性能优化系列 (十三)">ORACLE SQL性能优化系列 (十三)</a></td></tr>
<tr>
<td Height=1 colspan="1">
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td Height=1 background="http://tech.acnow.net/Files/Img/title_line.gif">
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td><img src="http://tech.acnow.net/Files/Img/list_nav_spacer.gif"><a Class="a2" href= http://tech.acnow.net/Html/DataBase/Oracle/2005-4/29/164558059816455862139.shtml title="ORACLE SQL性能优化系列 (二)">ORACLE SQL性能优化系列 (二)</a></td></tr>
<tr>
<td Height=1 colspan="1">
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td Height=1 background="http://tech.acnow.net/Files/Img/title_line.gif">
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td><img src="http://tech.acnow.net/Files/Img/list_nav_spacer.gif"><a Class="a2" href= http://tech.acnow.net/Html/DataBase/Oracle/2005-4/29/164558059816455832253.shtml title="ORACLE SQL性能优化系列 (八)">ORACLE SQL性能优化系列 (八)</a></td></tr>
<tr>
<td Height=1 colspan="1">
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td Height=1 background="http://tech.acnow.net/Files/Img/title_line.gif">
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td><img src="http://tech.acnow.net/Files/Img/list_nav_spacer.gif"><a Class="a2" href= http://tech.acnow.net/Html/DataBase/Oracle/2005-4/29/164558059816455832996.shtml title="ORACLE SQL性能优化系列 (四)">ORACLE SQL性能优化系列 (四)</a></td></tr>
<tr>
<td Height=1 colspan="1">
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td Height=1 background="http://tech.acnow.net/Files/Img/title_line.gif">
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td><img src="http://tech.acnow.net/Files/Img/list_nav_spacer.gif"><a Class="a2" href= http://tech.acnow.net/Html/DataBase/Oracle/2005-4/29/164559059816455960820.shtml title="ORACLE SQL性能优化系列 (十四) 完结篇">ORACLE SQL性能优化系列 (十四) 完结篇</a></td></tr>
<tr>
<td Height=1 colspan="1">
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td Height=1 background="http://tech.acnow.net/Files/Img/title_line.gif">
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td><img src="http://tech.acnow.net/Files/Img/list_nav_spacer.gif"><a Class="a2" href= http://tech.acnow.net/html/SoftWare/SoftwareEducate/OracleAuthentication/2006-5/29/150826355.shtml title="ORACLE SQL性能优化系列 (一)">ORACLE SQL性能优化系列 (一)</a></td></tr>
<tr>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -