⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 oracle sql性能优化系列 (十三)--acnow_net.htm

📁 这是在网上收集的关于oracle的sql语句优化的文章
💻 HTM
📖 第 1 页 / 共 3 页
字号:
        <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.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 用WHERE替代ORDER BY<BR><BR>ORDER BY 子句只在两种严格的条件下使用索引.<BR><BR>&nbsp;<BR><BR>ORDER BY中所有的列必须包含在相同的索引中并保持在索引中的排列顺序.<BR><BR>ORDER BY中所有的列必须定义为非空.<BR><BR>&nbsp;<BR><BR>WHERE子句使用的索引和ORDER BY子句中所使用的索引不能并列.<BR><BR>&nbsp;<BR><BR>例如:<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 表DEPT包含以下列:<BR><BR>&nbsp;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DEPT_CODE&nbsp;&nbsp;&nbsp; PK&nbsp;&nbsp;&nbsp; NOT NULL<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DEPT_DESC&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NOT NULL<BR><BR>&nbsp;&nbsp;&nbs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nbsp;&nbsp; WHERE DEPT_TYPE &gt; 0 <BR><BR>&nbsp;&nbsp; <BR><BR>&nbsp;EXPLAIN PLAN:<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TABLE ACCESS BY ROWID ON EMP<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; INDEX RANGE SCAN ON DEPT_IDX<BR><BR>译者按:<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ORDER BY 也能使用索引! 这的确是个容易被忽视的知识点. 我们来验证一下:<BR><BR>SQL&gt;&nbsp; select * from emp order by empno;<BR><BR>Execution Plan<BR><BR>----------------------------------------------------------<BR><BR>&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SELECT STATEMENT Optimizer=CHOOSE<BR><BR>&nbsp;&nbsp; 1&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp; TABLE ACCESS (BY INDEX ROWID) OF 'EMP'<BR><BR>&nbsp;&nbsp; 2&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp;&nbsp;&nbsp; INDEX (FULL SCAN) OF 'EMPNO' (UNIQUE)<BR><BR>&nbsp;<BR><BR>44.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 避免改变索引列的类型.<BR><BR>当比较不同数据类型的数据时, ORACLE自动对列进行简单的类型转换.<BR><BR>&nbsp;<BR><BR>假设 EMPNO是一个数值类型的索引列.<BR><BR>&nbsp;<BR><BR>SELECT …<BR><BR>FROM EMP<BR><BR>WHERE EMPNO = ‘123’<BR><BR>&nbsp;<BR><BR>实际上,经过ORACLE类型转换, 语句转化为:<BR><BR>SELECT …<BR><BR>FROM EMP<BR><BR>WHERE EMPNO = TO_NUMBER(‘123’)<BR><BR>&nbsp;<BR><BR>幸运的是,类型转换没有发生在索引列上,索引的用途没有被改变.<BR><BR>&nbsp;<BR><BR>现在,假设EMP_TYPE是一个字符类型的索引列.<BR><BR>SELECT …<BR><BR>FROM EMP<BR><BR>WHERE EMP_TYPE = 123<BR><BR>&nbsp;<BR><BR>这个语句被ORACLE转换为:<BR><BR>SELECT …<BR><BR>FROM EMP<BR><BR>WHERE TO_NUMBER(EMP_TYPE)=123<BR><BR>&nbsp;<BR><BR>因为内部发生的类型转换, 这个索引将不会被用到! <BR><BR>译者按:<BR><BR>为了避免ORACLE对你的SQL进行隐式的类型转换, 最好把类型转换用显式表现出来. 注意当字符和数值比较时, ORACLE会优先转换数值类型到字符类型.<BR><BR>&nbsp;<BR><BR>45.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 需要当心的WHERE子句<p><div align="right">本新闻共<font color=red>2</font>页,当前在第<font color=red>1</font>页&nbsp;&nbsp;<font color="red">1</font>&nbsp;&nbsp;<a href="16455905981645592205_2.shtml">2</a>&nbsp;&nbsp;</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&nbsp;SQL性能优化系列&nbsp;(九)</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&nbsp;SQL性能优化系列&nbsp;(六)</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&nbsp;SQL性能优化系列&nbsp;(十一)</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&nbsp;SQL性能优化系列&nbsp;(五)</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&nbsp;SQL性能优化系列&nbsp;(十二)</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&nbsp;SQL性能优化系列&nbsp;(二)</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&nbsp;SQL性能优化系列&nbsp;(一)</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&nbsp;SQL性能优化系列&nbsp;(一)</a></td></tr>
<tr>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -