?? 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>43. 用WHERE替代ORDER BY<BR><BR>ORDER BY 子句只在兩種嚴格的條件下使用索引.<BR><BR> <BR><BR>ORDER BY中所有的列必須包含在相同的索引中并保持在索引中的排列順序.<BR><BR>ORDER BY中所有的列必須定義為非空.<BR><BR> <BR><BR>WHERE子句使用的索引和ORDER BY子句中所使用的索引不能并列.<BR><BR> <BR><BR>例如:<BR><BR> 表DEPT包含以下列:<BR><BR> <BR><BR> DEPT_CODE PK NOT NULL<BR><BR> DEPT_DESC NOT NULL<BR><BR> DEPT_TYPE NULL<BR><BR> <BR><BR> 非唯一性的索引(DEPT_TYPE)<BR><BR> <BR><BR> 低效: (索引不被使用)<BR><BR> SELECT DEPT_CODE<BR><BR> FROM DEPT<BR><BR> ORDER BY DEPT_TYPE<BR><BR> <BR><BR> EXPLAIN PLAN:<BR><BR> SORT ORDER BY <BR><BR> TABLE ACCESS FULL<BR><BR> <BR><BR> 高效: (使用索引)<BR><BR> <BR><BR> SELECT DEPT_CODE<BR><BR> FROM DEPT<BR><BR> WHERE DEPT_TYPE > 0 <BR><BR> <BR><BR> EXPLAIN PLAN:<BR><BR> TABLE ACCESS BY ROWID ON EMP<BR><BR> INDEX RANGE SCAN ON DEPT_IDX<BR><BR>譯者按:<BR><BR> ORDER BY 也能使用索引! 這的確是個容易被忽視的知識點. 我們來驗證一下:<BR><BR>SQL> select * from emp order by empno;<BR><BR>Execution Plan<BR><BR>----------------------------------------------------------<BR><BR> 0 SELECT STATEMENT Optimizer=CHOOSE<BR><BR> 1 0 TABLE ACCESS (BY INDEX ROWID) OF 'EMP'<BR><BR> 2 1 INDEX (FULL SCAN) OF 'EMPNO' (UNIQUE)<BR><BR> <BR><BR>44. 避免改變索引列的類型.<BR><BR>當比較不同數據類型的數據時, ORACLE自動對列進行簡單的類型轉換.<BR><BR> <BR><BR>假設 EMPNO是一個數值類型的索引列.<BR><BR> <BR><BR>SELECT …<BR><BR>FROM EMP<BR><BR>WHERE EMPNO = ‘123’<BR><BR> <BR><BR>實際上,經過ORACLE類型轉換, 語句轉化為:<BR><BR>SELECT …<BR><BR>FROM EMP<BR><BR>WHERE EMPNO = TO_NUMBER(‘123’)<BR><BR> <BR><BR>幸運的是,類型轉換沒有發生在索引列上,索引的用途沒有被改變.<BR><BR> <BR><BR>現在,假設EMP_TYPE是一個字符類型的索引列.<BR><BR>SELECT …<BR><BR>FROM EMP<BR><BR>WHERE EMP_TYPE = 123<BR><BR> <BR><BR>這個語句被ORACLE轉換為:<BR><BR>SELECT …<BR><BR>FROM EMP<BR><BR>WHERE TO_NUMBER(EMP_TYPE)=123<BR><BR> <BR><BR>因為內部發生的類型轉換, 這個索引將不會被用到! <BR><BR>譯者按:<BR><BR>為了避免ORACLE對你的SQL進行隱式的類型轉換, 最好把類型轉換用顯式表現出來. 注意當字符和數值比較時, ORACLE會優先轉換數值類型到字符類型.<BR><BR> <BR><BR>45. 需要當心的WHERE子句<p><div align="right">本新聞共<font color=red>2</font>頁,當前在第<font color=red>1</font>頁 <font color="red">1</font> <a href="16455905981645592205_2.shtml">2</a> </div></p>
<P clear=all></P>
</FONT></TD>
</TR>
</TBODY>
</TABLE>
</DIV>
<BR>
上一篇:<a href='../../2005-4/29/164559059816455980543.shtml' title ='ORACLE SQL性能優化系列 (十二)'>ORACLE SQL性能優化系列 (十二)</a> 下一篇:<a href='../../2005-4/29/164559059816455950271.shtml' title ='ORACLE大數據類型的操作之一CLOB'>ORACLE大數據類型的操作之一CLOB</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=05981645592205\" 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=8996\" 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/164558059816455832430.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/164558059816455850688.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/164559059816455975203.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/164558059816455831878.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/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/SoftWare/SoftwareEducate/OracleAuthentication/2006-5/29/150826355.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/164558059816455810544.shtml title="ORACLE SQL性能優化系列 (一)">ORACLE SQL性能優化系列 (一)</a></td></tr>
<tr>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -