首页 > 代码库 > oracle_执行计划_谓词信息和数据获取(access and filter区别) (转)
oracle_执行计划_谓词信息和数据获取(access and filter区别) (转)
These two terms in the Predicate Information section indicate when the data source is reduced. Simply, access means only retrieve those records meeting the condition and ignore others. Filter means *after* you already got the data, go through them all and keep those meeting the condition and throw away the others.
access: 直接获取那些满足条件的数据,抛弃其他不满足的数据
filter: 你已经有了一些数据,对这些已经有的数据应用filter,得到满足filter的数据。
http://www.itpub.net/forum.php?mod=viewthread&tid=1766289
一:简要说明
在查看执行计划的信息中,经常会看到两个谓词filter和access,它们的区别是什么,理解了这两个词对我们解读Oracle的执行计划信息会有所帮助。
简单说,执行计划如果显示是access,就表示这个谓词条件的值将会影响数据的访问路径(表还是索引),而filter表示谓词条件的值并不会影响数据访问路径,只起到过滤的作用。
二:举例说明
SQL> create table zhou_t (x int , y int );
表已创建。
SQL> set autotrace trace exp;
SQL> select /*+rule*/ * from zhou_t where x=5;
执行计划
----------------------------------------------------------
Plan hash value: 1395150869
------------------------------------
| Id | Operation | Name |
------------------------------------
| 0 | SELECT STATEMENT | |
|* 1 | TABLE ACCESS FULL| ZHOU_T |
------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("X"=5)
Note
-----
- rule based optimizer used (consider using cbo)
因为表zhou_t没有创建索引,执行计划没有选择数据访问路径的余地,谓词条件在这里只是起到数据过滤的作用,所以使用了filter
如果在表上创建了索引呢?
SQL> create index zhou_t_idx on zhou_t(x,y);
索引已创建。
SQL> select /*+rule*/ * from zhou_t where x=5;
执行计划
----------------------------------------------------------
Plan hash value: 42197324
---------------------------------------
| Id | Operation | Name |
---------------------------------------
| 0 | SELECT STATEMENT | |
|* 1 | INDEX RANGE SCAN| ZHOU_T_IDX |
---------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("X"=5)
Note
-----
- rule based optimizer used (consider using cbo)
从上面可以看到,谓词条件影响到数据访问的路径------选择了索引,所以用access
http://zuoren110.blog.163.com/blog/static/617563201201331427675/
SQL> create table t
2 as select rownum r,object_name
3 from dba_objects
4 /
Table created.
SQL> create index t_idx on t(r);
Index created.
SQL> execute dbms_stats.gather_table_stats(user,‘t‘,cascade=>true)
PL/SQL procedure successfully completed.
SQL> set autotrace traceonly explain
SQL> select * from t
2 where r = 10000;
Execution Plan
----------------------------------------------------------
Plan hash value: 470836197
-------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 30 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| T | 1 | 30 | 2 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | T_IDX | 1 | | 1 (0)| 00:00:01 |
-------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("R"=10000)
使用的谓词是access ,访问的是索引,然后通过rowid 直接取出select结果。
SQL> select * from t
2 where r > 10000 and r < 50000
3 /
Execution Plan
----------------------------------------------------------
Plan hash value: 1601196873
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 40001 | 1171K| 88 (2)| 00:00:02 |
|* 1 | TABLE ACCESS FULL| T | 40001 | 1171K| 88 (2)| 00:00:02 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("R"<50000 AND "R">10000)
使用的谓词是filter 使用的是全表扫描,过滤掉不需要的行。
SQL> select r from t
2 where r > 10000
3 /
Execution Plan
----------------------------------------------------------
Plan hash value: 3163761342
------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 55631 | 271K| 42 (3)| 00:00:01 |
|* 1 | INDEX FAST FULL SCAN| T_IDX | 55631 | 271K| 42 (3)| 00:00:01 |
------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("R">10000)
这里的执行计划,就有点意思了,使用的是索引扫描(index fast full scan),
但是没有通过access 指出。可见oracle 决定使用索引扫描,并不一定要通过
access 来告诉我们。在这里r 可以完全通过读取索引来获得所需要的列值,并且
需要检索索引中的大部分key,所以oracle 决定使用index fast full scan,这种
访问索引的方式会通过multiblocks read 方式读取索引的 bocks,返回的结果集
是未经排序的,并且因为读取了所以的index blocks ,所以需要对index blocks
中的index keys 进行过滤。
SQL> create table emp
2 as select employee_id,first_name,last_name
3 from hr.employees;
Table created.
SQL> create index emp_idx on emp(employee_id,last_name);
Index created.
SQL> exec dbms_stats.gather_table_stats(user,‘emp‘,cascade=>true)
PL/SQL procedure successfully completed.
SQL> select employee_id,last_name
2 from emp
3 where employee_id < 200 and last_name = ‘King‘
4 /
Execution Plan
----------------------------------------------------------
Plan hash value: 3087982339
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2 | 24 | 1 (0)| 00:00:01 |
|* 1 | INDEX RANGE SCAN| EMP_IDX | 2 | 24 | 1 (0)| 00:00:01 |
----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("LAST_NAME"=‘King‘ AND "EMPLOYEE_ID"<200)
filter("LAST_NAME"=‘King‘)
我上面这个例子也比较有意思,我们在前面创建了一个复合索引,并且在where 子句中
使用了索引中的列。oracle 会根据where 条件通过访问复合索引中的列是否满足employee_id < 200
如果满足再根据条件filter 过滤出last_name = ‘King‘ 的index Key。
小结:通过上面的列子,虽然例子不是很经典,但是我觉得已经可以说明。
1、如果oracle 决定使用 index 来获得结果集,不需要使用access 谓词告诉我们,我(oracle)使用了index.
2、通过index 访问数据,也有可能需要用到filter 的。
http://blog.csdn.net/kkdelta/article/details/7938653
oracle_执行计划_谓词信息和数据获取(access and filter区别) (转)