📄 oracle sql 性能优化调整 -- 互联网博客 -- [北方博客].htm
字号:
<html>
<head><meta name="description" content="优化,性能,oracle,sql,Oracle sql 性能优化调整 ,互联网博客,internet,IT,java,web,jsp,struts,internet,互联网,firefox,北方网,博客,blog,网志"><meta name="keywords" content="优化,性能,oracle,sql,Oracle sql 性能优化调整 ,北方网,博客,blog,网志">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Oracle sql 性能优化调整 -- 互联网博客 -- [北方博客]</title>
<style type="text/css">
<!--
body {
margin-left: 1px;
margin-top: 1px;
margin-right: 1px;
margin-bottom: 1px;
background-color: #336699;
}
body,td,th {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 14px;
line-height: 22px;
}
.myurl {
font-size: 14px;
color: #666666;
}
a:link {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 14px;
color: #000000;
}
a:visited {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 14px;
color: #000000;
}
a:hover {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 14px;
color: #0000FF;
}
a:active {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 14px;
color: #000000;
}
.blogname {font-size: 20px;}
input {
border: 1px dotted #CCCCCC;
}
hr {
border: 1px dotted #CCCCCC;
}
-->
</style>
</head>
<body onload="javascript:if(document._gsearch!=undefined){document._gsearch.q.value='优化,性能,oracle,sql';}">
<table width="100%" border="0" align="center" cellpadding="3" cellspacing="5" bgcolor="#003366">
<tr bgcolor="#99CC66">
<td colspan="2"><table width="100%" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td width="52%"><table width="100%" border="0" cellpadding="0" cellspacing="2" bgcolor="#669900">
<tr>
<td height="60" valign="middle" bgcolor="#FFFFFF"><table height="20">
<tr>
<td>
<div class="blogname"><strong>互联网博客</strong></div></td>
</tr>
<tr>
<td>
<div class="myurl">http://internet.blog.enorth.com.cn ><a href="javascript:;" onclick="javascript:window.clipboardData.setData('Text','http://internet.blog.enorth.com.cn');alert('本站网址已经复制到您的剪贴板中.');">复制</a> ><a href="javascript:;" onclick="javascript:window.external.addFavorite
('http://internet.blog.enorth.com.cn','互联网博客');">收藏本站</a></div></td>
</tr>
</table></td>
</tr>
</table></td>
<td width="48%"><div align="right"><a href="http://blog.enorth.com.cn/blog/randomSite.b" target="_blank">左邻右里</a> | <a href="/topic/index.shtml">日志列表</a> | <a href="/rss/rss.xml" target="_blank"><img src="http://system.blog.enorth.com.cn/img/xml.gif" border="0"></a> |
<script type="text/javascript"><!--
google_ad_client = "pub-4353784307974611";
google_ad_width = 120;
google_ad_height = 60;
google_ad_format = "120x60_as_rimg";
google_cpa_choice = "CAAQyKmU_AEaCEgmBX1Smfx8KODy9IMB";
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div></td>
</tr>
</table></td>
</tr>
<tr bgcolor="#FFFFFF">
<td colspan="2"><div align="center"><a href="/index.shtml">首页</a> <a href=/topic/391.shtml>IT技术(74)</a> <a href=/topic/62139.shtml>音视频(6)</a> <a href=/topic/49570.shtml>汽车(3)</a> <a href=/topic/26389.shtml>程序人生(1)</a> <a href=/topic/1929.shtml>搜索引擎(2)</a> <a href=/topic/163.shtml>我的日志(27)</a> <a href=/topic/3571.shtml>歌词(10)</a> <a href=/topic/44580.shtml>成功案例(1)</a> <a href=/topic/22130.shtml>James(19)</a> <a href=/topic/2276.shtml>笑话(10)</a> <a href=/topic/4765.shtml>Blog技术(4)</a> <a href=/topic/2441.shtml>诗词歌赋(7)</a> </div></td>
</tr>
<tr bgcolor="#FFFFFF">
<td width="76%" valign="top" bgcolor="#FFFFFF" id="col_main">
<br>
<table width="95%" border="0" align="center" cellpadding="2" cellspacing="1">
<tr>
<td><a href="http://blog.enorth.com.cn" target="_blank" title="北方博客">北方博客</a> > <a href="/">首页</a> > Oracle sql 性能优化调整 <br>
<center>
<script type="text/javascript"><!--
google_ad_client = "pub-4353784307974611";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
google_ad_channel ="";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "333333";
google_color_text = "000000";
google_color_url = "666666";
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</center>
<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td align="left"><a href="http://internet.blog.enorth.com.cn/article/27381.shtml"><span style="font-family: Webdings">3</span>上一篇: Apache服务器配置全攻略(八) </a></td><td align="right"><a href="http://internet.blog.enorth.com.cn/article/27438.shtml">下一篇: Tomcat 配置集锦<span style="font-family: Webdings">4</span></a></td></tr></table><strong>[<a href="/topic/391.shtml">IT技术</a>]Oracle sql 性能优化调整 </strong>
<hr width="100%" size="1" noshade>
<table><tr><td><strong>Tags</strong>: <a href="http://blog.enorth.com.cn/blog/tags/%E4%BC%98%E5%8C%96" target="_blank">优化</a> <a href="http://blog.enorth.com.cn/blog/tags/%E6%80%A7%E8%83%BD" target="_blank">性能</a> <a href="http://blog.enorth.com.cn/blog/tags/oracle" target="_blank">oracle</a> <a href="http://blog.enorth.com.cn/blog/tags/sql" target="_blank">sql</a> </td></tr></table><br><span id="zoom"><P> 一、问题的提出 </P>
<P><BR> 在应用系统开发初期,由于开发数据库数据比较少,对于查询SQL语句,复杂视图的的编写等体会不出SQL语句各种写法的性能优劣,但是如果将应用系统提交实际应用后,随着数据库中数据的增加,系统的响应速度就成为目前系统需要解决的最主要的问题之一。系统优化中一个很重要的方面就是SQL语句的优化。对于海量数据,劣质SQL语句和优质SQL语句之间的速度差别可以达到上百倍,可见对于一个系统不 <script language=javascript src="/CMS/JS/newsad.js"></script> 是简单地能实现其功能就可,而是要写出高质量的SQL语句,提高系统的可用性。 </P>
<P> 在多数情况下,Oracle使用索引来更快地遍历表,优化器主要根据定义的索引来提高性能。但是,如果在SQL语句的where子句中写的SQL代码不合理,就会造成优化器删去索引而使用全表扫描,一般就这种SQL语句就是所谓的劣质SQL语句。在编写SQL语句时我们应清楚优化器根据何种原则来删除索引,这有助于写出高性能的SQL语句。 </P>
<P> 二、SQL语句编写注意问题 </P>
<P><BR> 下面就某些SQL语句的where子句编写中需要注意的问题作详细介绍。在这些where子句中,即使某些列存在索引,但是由于编写了劣质的SQL,系统在运行该SQL语句时也不能使用该索引,而同样使用全表扫描,这就造成了响应速度的极大降低。 </P>
<P> 1. IS NULL 与 IS NOT NULL </P>
<P> 不能用null作索引,任何包含null值的列都将不会被包含在索引中。即使索引有多列这样的情况下,只要这些列中有一列含有null,该列就会从索引中排除。也就是说如果某列存在空值,即使对该列建索引也不会提高性能。 </P>
<P> 任何在where子句中使用is null或is not null的语句优化器是不允许使用索引的。 </P>
<P>2. 联接列 </P>
<P> 对于有联接的列,即使最后的联接值为一个静态值,优化器是不会使用索引的。我们一起来看一个例子,假定有一个职工表(employee),对于一个职工的姓和名分成两列存放(FIRST_NAME和LAST_NAME),现在要查询一个叫比尔.克林顿(Bill Cliton)的职工。 </P>
<P> 下面是一个采用联接查询的SQL语句, </P>
<P> select * from employss <BR> where <BR> first_name||''||last_name ='Beill Cliton'; </P>
<P> 上面这条语句完全可以查询出是否有Bill Cliton这个员工,但是这里需要注意,系统优化器对基于last_name创建的索引没有使用。 </P>
<P> 当采用下面这种SQL语句的编写,Oracle系统就可以采用基于last_name创建的索引。 </P>
<P> Select * from employee <BR> where <BR>first_name ='Beill' and last_name ='Cliton'; </P>
<P> 遇到下面这种情况又如何处理呢?如果一个变量(name)中存放着Bill Cliton这个员工的姓名,对于这种情况我们又如何避免全程遍历,使用索引呢?可以使用一个函数,将变量name中的姓和名分开就可以了,但是有一点需要注意,这个函数是不能作用在索引列上。下面是SQL查询脚本: </P>
<P> select * from employee <BR> where <BR> first_name = SUBSTR('&&name',1,INSTR('&&name',' ')-1) <BR> and <BR> last_name = SUBSTR('&&name',INSTR('&&name',' ')+1) </P>
<P> 3. 带通配符(%)的like语句 </P>
<P> 同样以上面的例子来看这种情况。目前的需求是这样的,要求在职工表中查询名字中包含 cliton的人。可以采用如下的查询SQL语句: </P>
<P> select * from employee where last_name like '%cliton%'; </P>
<P> 这里由于通配符(%)在搜寻词首出现,所以Oracle系统不使用last_name的索引。在很多情况下可能无法避免这种情况,但是一定要心中有底,通配符如此使用会降低查询速度。然而当通配符出现在字符串其他位置时,优化器就能利用索引。在下面的查询中索引得到了使用: </P>
<P> select * from employee where last_name like 'c%'; </P>
<P> 4. Order by语句 </P>
<P> ORDER BY语句决定了Oracle如何将返回的查询结果排序。Order by语句对要排序的列没有什么特别的限制,也可以将函数加入列中(象联接或者附加等)。任何在Order by语句的非索引项或者有计算表达式都将降低查询速度。 </P>
<P> 仔细检查order by语句以找出非索引项或者表达式,它们会降低性能。解决这个问题的办法就是重写order by语句以使用索引,也可以为所使用的列建立另外一个索引,同时应绝对避免在order by子句中使用表达式。</P>
<P>5. NOT </P>
<P> 我们在查询时经常在where子句使用一些逻辑表达式,如大于、小于、等于以及不等于等等,也可以使用and(与)、or(或)以及not(非)。NOT可用来对任何逻辑运算符号取反。下面是一个NOT子句的例子: </P>
<P> ... where not (status ='VALID') </P>
<P> 如果要使用NOT,则应在取反的短语前面加上括号,并在短语前面加上NOT运算符。NOT运算符包含在另外一个逻辑运算符中,这就是不等于(<>)运算符。换句话说,即使不在查询where子句中显式地加入NOT词,NOT仍在运算符中,见下例: </P>
<P> ... where status <>'INVALID'; </P>
<P> 再看下面这个例子: </P>
<P> select * from employee where salary<>3000; </P>
<P> 对这个查询,可以改写为不使用NOT: </P>
<P> select * from employee where salary<3000 or salary>3000; </P>
<P> 虽然这两种查询的结果一样,但是第二种查询方案会比第一种查询方案更快些。第二种查询允许Oracle对salary列使用索引,而第一种查询则不能使用索引。 </P>
<P> 6. IN和EXISTS </P>
<P> 有时候会将一列和一系列值相比较。最简单的办法就是在where子句中使用子查询。在where子句中可以使用两种格式的子查询。 </P>
<P> 第一种格式是使用IN操作符: </P>
<P> ... where column in(select * from ... where ...); </P>
<P> 第二种格式是使用EXIST操作符: </P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -