?? oracle sql性能優(yōu)化系列 (二)--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性能優(yōu)化系列 (二)</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>網(wǎng)絡(luò)</FONT></TD>
</TR>
<TR>
<TD height=15></TD>
</TR>
<TR>
<TD class=l17><FONT class=f14 id=zoom><BR>4. 選擇最有效率的表名順序(只在基于規(guī)則的優(yōu)化器中有效)<BR><BR>ORACLE的解析器按照從右到左的順序處理FROM子句中的表名,因此FROM子句中寫在最后的表(基礎(chǔ)表 driving table)將被最先處理. 在FROM子句中包含多個(gè)表的情況下,你必須選擇記錄條數(shù)最少的表作為基礎(chǔ)表.當(dāng)ORACLE處理多個(gè)表時(shí), 會運(yùn)用排序及合并的方式連接它們.首先,掃描第一個(gè)表(FROM子句中最后的那個(gè)表)并對記錄進(jìn)行派序,然后掃描第二個(gè)表(FROM子句中最后第二個(gè)表),最后將所有從第二個(gè)表中檢索出的記錄與第一個(gè)表中合適記錄進(jìn)行合并.<BR><BR> <BR><BR>例如:<BR><BR> 表 TAB1 16,384 條記錄<BR><BR> 表 TAB2 1 條記錄<BR><BR> <BR><BR> 選擇TAB2作為基礎(chǔ)表 (最好的方法)<BR><BR> select count(*) from tab1,tab2 執(zhí)行時(shí)間0.96秒<BR><BR> <BR><BR> 選擇TAB2作為基礎(chǔ)表 (不佳的方法)<BR><BR> select count(*) from tab2,tab1 執(zhí)行時(shí)間26.09秒<BR><BR> <BR><BR>如果有3個(gè)以上的表連接查詢, 那就需要選擇交叉表(intersection table)作為基礎(chǔ)表, 交叉表是指那個(gè)被其他表所引用的表.<BR><BR> <BR><BR>例如:<BR><BR> <BR><BR> EMP表描述了LOCATION表和CATEGORY表的交集.<BR><BR> <BR><BR> SELECT * <BR><BR>FROM LOCATION L , <BR><BR> CATEGORY C,<BR><BR> EMP E <BR><BR>WHERE E.EMP_NO BETWEEN 1000 AND 2000<BR><BR>AND E.CAT_NO = C.CAT_NO<BR><BR>AND E.LOCN = L.LOCN<BR><BR> <BR><BR>將比下列SQL更有效率<BR><BR> <BR><BR>SELECT * <BR><BR>FROM EMP E ,<BR><BR>LOCATION L , <BR><BR> CATEGORY C<BR><BR>WHERE E.CAT_NO = C.CAT_NO<BR><BR>AND E.LOCN = L.LOCN<BR><BR>AND E.EMP_NO BETWEEN 1000 AND 2000<BR><BR> <BR><BR> <BR><BR>5. WHERE子句中的連接順序.<BR><BR> <BR><BR>ORACLE采用自下而上的順序解析WHERE子句,根據(jù)這個(gè)原理,表之間的連接必須寫在其他WHERE條件之前, 那些可以過濾掉最大數(shù)量記錄的條件必須寫在WHERE子句的末尾.<BR><BR> <BR><BR>例如:<BR><BR> <BR><BR>(低效,執(zhí)行時(shí)間156.3秒)<BR><BR>SELECT … <BR><BR>FROM EMP E<BR><BR>WHERE SAL > 50000<BR><BR>AND JOB = ‘MANAGER’<BR><BR>AND 25 < (SELECT COUNT(*) FROM EMP<BR><BR> WHERE MGR=E.EMPNO);<BR><BR> <BR><BR>(高效,執(zhí)行時(shí)間10.6秒)<BR><BR>SELECT … <BR><BR>FROM EMP E<BR><BR>WHERE 25 < (SELECT COUNT(*) FROM EMP<BR><BR> WHERE MGR=E.EMPNO)<BR><BR>AND SAL > 50000<BR><BR>AND JOB = ‘MANAGER’;<BR><BR> <BR><BR> <BR><BR>6. SELECT子句中避免使用 ‘ * ‘<BR><BR>當(dāng)你想在SELECT子句中列出所有的COLUMN時(shí),使用動態(tài)SQL列引用 ‘*’ 是一個(gè)方便的方法.不幸的是,這是一個(gè)非常低效的方法. 實(shí)際上,ORACLE在解析的過程中, 會將’*’ 依次轉(zhuǎn)換成所有的列名, 這個(gè)工作是通過查詢數(shù)據(jù)字典完成的, 這意味著將耗費(fèi)更多的時(shí)間. <BR><BR> <BR><BR> <BR><BR>7. 減少訪問<a href="/Html/DataBase/" target="_blank">數(shù)據(jù)庫</a>的次數(shù)<BR><BR>當(dāng)執(zhí)行每條SQL語句時(shí), ORACLE在內(nèi)部執(zhí)行了許多工作: 解析SQL語句, 估算索引的利用率, 綁定變量 , 讀數(shù)據(jù)塊等等. 由此可見, 減少訪問數(shù)據(jù)庫的次數(shù) , 就能實(shí)際上減少ORACLE的工作量.<BR><BR> <BR><BR>例如,<BR><BR> 以下有三種方法可以檢索出雇員號等于0342或0291的職員.<BR><BR> <BR><BR>方法1 (最低效)<BR><BR> SELECT EMP_NAME , SALARY , GRADE<BR><BR> FROM EMP <BR><BR> WHERE EMP_NO = 342;<BR><BR> <BR><BR> SELECT EMP_NAME , SALARY , GRADE<p><div align="right">本新聞共<font color=red>2</font>頁,當(dāng)前在第<font color=red>1</font>頁 <font color="red">1</font> <a href="164558059816455862139_2.shtml">2</a> </div></p>
<P clear=all></P>
</FONT></TD>
</TR>
</TBODY>
</TABLE>
</DIV>
<BR>
上一篇:<a href='../../2005-4/29/164558059816455811533.shtml' title ='在DB2中如何實(shí)現(xiàn)Oracle的相關(guān)功能(三)'>在DB2中如何實(shí)現(xiàn)Oracle的相關(guān)功能(三)</a> 下一篇:<a href='../../2005-4/29/164558059816455883761.shtml' title ='ORACLE9I中外部表的使用'>ORACLE9I中外部表的使用</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=059816455862139\" target=\"_blank\">發(fā)送給好友</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=8957\" 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()">關(guān)閉</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/164558059816455850688.shtml title="ORACLE SQL性能優(yōu)化系列 (六)">ORACLE SQL性能優(yōu)化系列 (六)</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/164558059816455833067.shtml title="ORACLE SQL性能優(yōu)化系列 (七)">ORACLE SQL性能優(yōu)化系列 (七)</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性能優(yōu)化系列 (四)">ORACLE SQL性能優(yōu)化系列 (四)</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/164559059816455944859.shtml title="ORACLE SQL性能優(yōu)化系列 (十)">ORACLE SQL性能優(yōu)化系列 (十)</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性能優(yōu)化系列 (十一)">ORACLE SQL性能優(yōu)化系列 (十一)</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/164558059816455832430.shtml title="ORACLE SQL性能優(yōu)化系列 (九)">ORACLE SQL性能優(yōu)化系列 (九)</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性能優(yōu)化系列 (五)">ORACLE SQL性能優(yōu)化系列 (五)</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性能優(yōu)化系列 (十四) 完結(jié)篇">ORACLE SQL性能優(yōu)化系列 (十四) 完結(jié)篇</a></td></tr>
<tr>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -