---
title: "[转]Oracle HowTo:如何使用Leading提示改变表连接方式"
author: "lin5061"
date: "2010-11-04T14:19:59Z"
tags: ["Oracle", "SQL"]
categories: ["数据库"]
slug: "oracle-howto-leading"
draft: false
lastmod: "2010-11-04T14:19:59Z"
id: 802751
url: "https://lin5061.iteye.com/blog/802751"
---

1、使用leading和use\_nl来设置表的查询顺序，来加快查询速度，一般把小表设为第一个表。  
/\*+LEADING(TABLE)\*/  
　　将指定的表作为连接次序中的首表.  
/\*+USE\_NL(TABLE)\*/  
　　将指定表与嵌套的连接的行源进行连接,并把指定表作为内部表.

成本计算方法：  
设小表100行，大表100000行。

两表均有索引：  
如果小表在内，大表在外(驱动表)的话，则扫描次数为：  
100000+100000\*2 (其中2表示IO次数，一次索引，一次数据)  
如果大表在内，小表在外(驱动表)的话，则扫描次数为：  
100+100\*2.

两表均无索引：  
如果小表在内，大表在外的话，则扫描次数为：  
100000+100\*100000  
如果大表在内，小表在外的话，则扫描次数为：  
100+100000\*100

注意：如果一个表有索引，一个表没有索引，ORACLE会将没有索引的表作驱动表。如果两个表都有索引，则外表作驱动表。如果两个都没索引的话，则也是外表作驱动表。

2、使用index直接匹配索引来查询数据提高查询速度  
/\*+INDEX(TABLE INDEX\_NAME)\*/  
　　表明对表选择索引的扫描方法.  
3、当判断某几个表中是否存在某种关系的行时可使用rownum=1来作为条件而加快速度，如果必须有多行才满足条件时，可设置rownum &lt;= n。  
例如：获得一个v\_count值判断是否大于0  
equipment表 几万条，controledpnsnrange 几千条  
select  
count(sc.pmnum)  
into  
v\_count  
from  
equipment e,  
sal\_controledpnsnrange sc  
where  
(sc.new\_min\_item\_no &lt;= e.itemnum)  
and (sc.new\_max\_item\_no &gt;= e.itemnum)  
and (sc.new\_min\_serial\_no &lt;= e.serialnum)  
and (sc.new\_max\_serial\_no &gt;= e.serialnum)  
and e.itemnum = a\_item\_no;  
执行时间大约为3分钟。  
代码修改后:  
select /\*+ leading(e) use\_nl(sc) \*/  
count(sc.pmnum)  
into  
v\_count  
from  
equipment e,  
sal\_controledpnsnrange sc  
where  
(sc.new\_min\_item\_no &lt;= e.itemnum)  
and (sc.new\_max\_item\_no &gt;= e.itemnum)  
and (sc.new\_min\_serial\_no &lt;= e.serialnum)  
and (sc.new\_max\_serial\_no &gt;= e.serialnum)  
and e.itemnum = a\_item\_no;  
执行时间大约为45秒。  
代码再次修改后：  
select /\*+ leading(e) use\_nl(sc) \*/  
count(sc.pmnum)  
into  
v\_count  
from  
equipment e,  
sal\_controledpnsnrange sc  
where  
(sc.new\_min\_item\_no &lt;= e.itemnum)  
and (sc.new\_max\_item\_no &gt;= e.itemnum)  
and (sc.new\_min\_serial\_no &lt;= e.serialnum)  
and (sc.new\_max\_serial\_no &gt;= e.serialnum)  
and e.itemnum = a\_item\_no  
and rownum = 1;  
执行时间大约为4秒。

 

转自：[http://www.eygle.com/archives/2006/02/use\_leading\_hints\_change\_sql\_explain.html](http://www.eygle.com/archives/2006/02/use_leading_hints_change_sql_explain.html)