看最佳化文檔的時候發現,指定索引的HINT還可以通過列方式。
在9i和以前的版本,索引提示的格式為/*+ index(table_alias) */或/*+ index(table_alias index_name) */,但是在10g中不僅可以通過索引名稱來確定HINT的索引,還可以通過指定列名的方式。
SQL> select * from v$version;
BANNER
----------------------------------------------------------------
Oracle Database10gEnterpriseEdition Release10.2.0.3.0 - 64bi
PL/SQL Release 10.2.0.3.0 - Production
CORE 10.2.0.3.0 Production
TNS for Linux: Version 10.2.0.3.0 - Production
NLSRTL Version 10.2.0.3.0 - Production
SQL> create table t
2 as select *
3 from dba_objects;
Table created.
SQL> create index ind_t_owner_type
2 on t (owner, object_type);
Index created.
SQL> exec dbms_stats.gather_table_stats(user, 'T', method_opt => 'for all indexed columns size 100')
PL/SQL procedure successfully completed.
SQL> set autot trace exp
SQL> select *
2 from t
3 where wner = 'SYS';
Execution Plan
----------------------------------------------------------
Plan hash value: 1601196873
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 23299 | 2138K| 180 (1)| 00:00:03 |
|* 1 | TABLE ACCESS FULL| T | 23299 | 2138K| 180 (1)| 00:00:03 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("OWNER"='SYS')
SQL> select /*+ index(t ind_t_owner_type) */ *
2 from t
3 where wner = 'SYS';
Execution Plan
----------------------------------------------------------
Plan hash value: 393469706
--------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows |Bytes| Cost (%CPU)| Time |
--------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 23299 |2138K| 721 (1)| 00:00:11 |
| 1 | TABLE ACCESS BY INDEX ROWID| T | 23299 |2138K| 721 (1)| 00:00:11 |
|* 2 | INDEX RANGE SCAN | IND_T_OWNER_TYPE | 23299 | | 44 (0)| 00:00:01 |
--------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("OWNER"='SYS')
除了常規的索引名稱方式,還可以通過owner或owner、object_type列的方式指定: