局部范围扫描的概念:摘自《海量数据库的解决方案》
能够实现局部范围扫描方式的SQL具有一个非常显著的特征,就是不论处理的数据量有多大,始终都能够确保具有较快的执行速度。也许这句具有魅力的话有些不符合常理,但是对于我们而言,这难道不是我们所追求的目标吗?
在实现了局部范围扫描的瞬间,SQL开始从大量的限制中获得了自由,但是很明显在变形之前的SQL中有可能会存在着一些不符合局部范围扫描准则的要素,之所以实现了局部范围扫描,是因为我们对SQL做了适当变形并使其遵循了局部范围扫描的准则。
无法使用局部扫描的情况:
Group 聚合函数,如sum,avg,count,min,max等。
Order by。
Sort。
Union。
Minux。
一、用读取路径实现对排序的替代操作。
表test01索引建在object_name和object_id上。
整体范围扫描:
select t.owner,t.object_name,t.object_type
from test01 t
where t.object_type like 'TABLE%'
order by t.object_name。
局部范围扫描:
select /*+ index_desc(t IDX_OBJECTNAME)*/t.owner,t.object_name,t.object_type --表有别名的必须使用别名,否则hint不会生效的。
from test01 t
where t.object_type like 'TABLE%'
二、max,min的局部扫描:
全局扫描:
select max(object_id) from test01 where object_type = 'TABLE';
and t.object_name > ' ';--用该条件才能使用hint提示生效。
局部扫描:
select /*+ index_desc(test01 IDX_OBJECTID)*/
object_id
from test01
where object_type = 'TABLE'
and object_id > 0 --用该条件才能使用hint提示生效。
and rownum = 1;
三、FILTER型局部范围扫描:
select count(*) into cnt from test01 t where t.object_id > 100;
-----------
if cnt > 0
-------------------
如果只是确认存在与否,没必要对全部数据进行count。
select 1 into cnt from dual
where exists(
select 'X' from test01 t where t.object_id > 100
)
if cnt > 0
只要满足条件就立即返回。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/20577218/viewspace-701401/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/20577218/viewspace-701401/