选择显示字体大小

oracle sql性能优化系列 (十二)

oracle sql性能优化系列 (十二) 

39. 总是使用索引的第一个列

如果索引是建立在多个列上, 只有在它的第一个列(leading column)被where子句引用时,优化器才会选择使用该索引.

 

译者按:

这也是一条简单而重要的规则. 见以下实例.

 

sql> create table multiindexusage ( inda number , indb number , descr varchar2(10));

table created.

sql> create index multindex on multiindexusage(inda,indb);

index created.

sql> set autotrace traceonly

 

sql> select * from multiindexusage where inda = 1;

execution plan

----------------------------------------------------------

0 select statement optimizer=choose

1 0 table access (by index rowid) of 'multiindexusage'

2 1 index (range scan) of 'multindex' (non-unique)

 

sql> select * from multiindexusage where indb = 1;

execution plan

----------------------------------------------------------

0 select statement optimizer=choose

1 0 table access (full) of 'multiindexusage'

 

很明显, 当仅引用索引的第二个列时,优化器使用了全表扫描而忽略了索引

 

 

40. oracle内部操作

当执行查询时,oracle采用了内部的操作. 下表显示了几种重要的内部操作.

oracle clause
内部操作

order by
sort order by

union
union-all

minus
minus

intersect
intersect

distinct,minus,intersect,union
sort unique

min,max,count
sort aggregate

group by
sort group by

rownum
count or count stopkey

queries involving joins
sort join,merge join,nested loops

connect by
connect by

 

 


41. 用union-all 替换union ( 如果有可能的话)

 

当sql语句需要union两个查询结果集合时,这两个结果集合会以union-all的方式被合并, 然后在输出最终结果前进行排序.

如果用union all替代union, 这样排序就不是必要了. 效率就会因此得到提高.

 

举例:

低效:

    select acct_num, balance_amt

from debit_transactions

where tran_date = ’31-dec-95’

union

select acct_num, balance_amt

from debit_transactions

where tran_date = ’31-dec-95’

高效:

select acct_num, balance_amt

from debit_transactions

where tran_date = ’31-dec-95’

union all

select acct_num, balance_amt

from debit_transactions

where tran_date = ’31-dec-95’

 

译者按:

需要注意的是,union all 将重复输出两个结果集合中相同记录. 因此各位还是

要从业务需求分析使用union all的可行性.

union 将对结果集合排序,这个操作会使用到sort_area_size这块内存. 对于这

块内存的优化也是相当重要的. 下面的sql可以用来查询排序的消耗量

 

select substr(name,1,25) "sort area name",

substr(value,1,15) "value"

from v$sysstat

where name like 'sort%'

 

42. 使用提示(hints)

对于表的访问,可以使用两种hints.

full 和 rowid

 

full hint 告诉oracle使用全表扫描的方式访问指定表.

例如:

select /*+ full(emp) */ *

from emp

where empno = 7893;

 

rowid hint 告诉oracle使用table access by rowid的操作访问表.

 

通常, 你需要采用table access by rowid的方式特别是当访问大表的时候, 使用这种方式, 你需要知道roiwd的值或者使用索引.

如果一个大表没有被设定为缓存(cached)表而你希望它的数据在查询结束是仍然停留

在sga中,你就可以使用cache hint 来告诉优化器把数据保留在sga中. 通常cache hint 和 full hint 一起使用.

例如:

select /*+ full(worker) cache(worker)*/ *

from work;

 

索引hint 告诉oracle使用基于索引的扫描方式. 你不必说明具体的索引名称

例如:

select /*+ index(lodging) */ lodging

from lodging

where manager = ‘bill gates’;

 

在不使用hint的情况下, 以上的查询应该也会使用索引,然而,如果该索引的重复值过多而你的优化器是cbo, 优化器就可能忽略索引. 在这种情况下, 你可以用index hint强制oracle使用该索引.

 

oracle hints 还包括all_rows, first_rows, rule,use_nl, use_merge, use_hash 等等.

 

译者按:

使用hint , 表示我们对oracle优化器缺省的执行路径不满意,需要手工修改.

这是一个很有技巧性的工作. 我建议只针对特定的,少数的sql进行hint的优化.

oracle的优化器还是要有信心(特别是cbo)
 
 


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons