【SQL 提示 之二】index_ss Index Skip Hint

简介:

Index Skip Hint  在如下情况下使用:当在一个联合索引中,某些谓词条件不在联合索引的第一列时比如 id,object_name 在where条件中使用了object_name 时,可以通过使用Index Skip Hint 来访问数据。

SQL> create table t as select 1 id ,object_name from dba_objects;
表已创建。
SQL> insert into t select 2 id ,object_name from dba_objects;
已创建68301行。
SQL> insert into t select 3 id ,object_name from dba_objects;
已创建68301行。
SQL> insert into t select 4 id ,object_name from dba_objects;
已创建68301行。
SQL> commit;
提交完成。
SQL> create index t_idx on t(id,object_name);
索引已创建。
SQL> exec dbms_stats.gather_table_stats(user,'T',cascade => true);
PL/SQL 过程已成功完成。
SQL> set autot trace exp stat
SQL> select * from t where object_name='T';
执行计划
----------------------------------------------------------
Plan hash value: 3670166625    
--------------------------------------------------------------------------
| Id  | Operation        | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT |       |     7 |   189 |     6   (0)| 00:00:01 |
|*  1 |  INDEX SKIP SCAN | T_IDX |     7 |   189 |     6   (0)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - access("OBJECT_NAME"='T') 
       filter("OBJECT_NAME"='T')
统计信息
----------------------------------------------------------
        129  recursive calls  
          0  db block gets 
         37  consistent gets
          5  physical reads 
          0  redo size   
        531  bytes sent via SQL*Net to client
        416  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          4  sorts (memory)  
          0  sorts (disk) 
          4  rows processed

SQL> select /*+ full(t) */ * from t where object_name='T';
执行计划
----------------------------------------------------------                                     
Plan hash value: 1601196873   
-------------------------------------------------------------------------- 
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |     7 |   189 |   337   (1)| 00:00:05 |
|*  1 |  TABLE ACCESS FULL| T    |     7 |   189 |   337   (1)| 00:00:05 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):    
---------------------------------------------------
   1 - filter("OBJECT_NAME"='T')  
统计信息
----------------------------------------------------------
          1  recursive calls
          0  db block gets 
       1232  consistent gets
        295  physical reads 
          0  redo size  
        531  bytes sent via SQL*Net to client
        416  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)    
          0  sorts (disk)  
          4  rows processed 



SQL> select  * from t where object_name='T';
执行计划
----------------------------------------------------------  
Plan hash value: 3670166625      
--------------------------------------------------------------------------
| Id  | Operation        | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT |       |     7 |   189 |     6   (0)| 00:00:01 |
|*  1 |  INDEX SKIP SCAN | T_IDX |     7 |   189 |     6   (0)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------                                                                     
   1 - access("OBJECT_NAME"='T') 
       filter("OBJECT_NAME"='T')                                                                                       

统计信息
----------------------------------------------------------  
          1  recursive calls  
          0  db block gets 
         19  consistent gets 
          0  physical reads 
          0  redo size 
        531  bytes sent via SQL*Net to client  
        416  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client 
          0  sorts (memory) 
          0  sorts (disk)  
          4  rows processed
 
SQL> create table t2 as select object_id id,object_name from dba_objects;
表已创建。
SQL> create index idx_t2 on t2 (id,object_name);
索引已创建。
SQL> exec dbms_stats.gather_table_stats(user,'T2',cascade => true);
PL/SQL 过程已成功完成。
SQL> select * from t2 where object_name ='T';
执行计划
----------------------------------------------------------   
Plan hash value: 1513984157
-------------------------------------------------------------------------- 
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     | 
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |     2 |    58 |    91   (2)| 00:00:02 |
|*  1 |  TABLE ACCESS FULL| T2   |     2 |    58 |    91   (2)| 00:00:02 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):   
---------------------------------------------------  
   1 - filter("OBJECT_NAME"='T')  
统计信息
----------------------------------------------------------  
        129  recursive calls         
          0  db block gets      
        348  consistent gets  
          0  physical reads 
          0  redo size     
        478  bytes sent via SQL*Net to client  
        416  bytes received via SQL*Net from client 
          2  SQL*Net roundtrips to/from client   
          4  sorts (memory)   
          0  sorts (disk)  
          1  rows processed  
 有以上两个执行计划可以看出t 表的id字段有4个不同的值,t2表上则有6万多个不同的值,对于前者,当谓词中没有联合索引的第一个字段是,cbo会选择index_ss。而对于第一个索引字段重复率很低的情况,选择index_ss 反而比fts 消耗跟多的资源。
SQL> select /*+ index_ss(t2 idx_t2) */ * from t2 where object_name='T';
执行计划
----------------------------------------------------------
Plan hash value: 2401255812   
---------------------------------------------------------------------------
| Id  | Operation        | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT |        |     2 |    58 | 68326   (1)| 00:13:40 |
|*  1 |  INDEX SKIP SCAN | IDX_T2 |     2 |    58 | 68326   (1)| 00:13:40 |
---------------------------------------------------------------------------
Predicate Information (identified by operation id):    
---------------------------------------------------
   1 - access("OBJECT_NAME"='T')  
       filter("OBJECT_NAME"='T')  
统计信息
----------------------------------------------------------  
          1  recursive calls   
          0  db block gets   
        387  consistent gets 
          0  physical reads  
          0  redo size     
        478  bytes sent via SQL*Net to client 
        416  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)    
          0  sorts (disk)   
          1  rows processed  

相关文章
|
8月前
|
SQL 关系型数据库 MySQL
OBCP第四章 SQL调优-Hint
OBCP第四章 SQL调优-Hint
209 0
|
3天前
|
SQL Oracle 关系型数据库
SQL CREATE INDEX 语句- 提高数据库检索效率的关键步骤
SQL CREATE INDEX 语句用于在表中创建索引。 索引用于比其他方式更快地从数据库中检索数据。用户无法看到索引,它们只是用于加速搜索/查询。 注意: 使用索引更新表比不使用索引更新表需要更多的时间(因为索引也需要更新)。因此,只在经常进行搜索的列上创建索引。
53 5
|
10月前
|
SQL 存储 分布式计算
ODPS(MaxCompute)不支持解析和执行SQL语句中的Hint语句
ODPS(MaxCompute)不支持解析和执行SQL语句中的Hint语句
181 2
java.sql.SQLException: Parameter index out of range (6 >
java.sql.SQLException: Parameter index out of range (6 >
109 0
|
SQL Oracle 关系型数据库
Python 技术篇-操作oracle数据库执行SQL语句报错,提示ORA-00911: 无效字符解决方法
Python 技术篇-操作oracle数据库执行SQL语句报错,提示ORA-00911: 无效字符解决方法
503 0
Python 技术篇-操作oracle数据库执行SQL语句报错,提示ORA-00911: 无效字符解决方法
|
SQL 弹性计算
服务器内自建SQL server 服务无法启动,提示评估期已过
服务器内自建SQL server 服务无法启动,提示评估期已过
|
SQL API Apache
Flink SQL代码补全提示(源码分析)
使用过Navicat的童鞋都知道,当我们写SQL的时候,工具会根据我们输入的内容弹出提示,这样可以很方便我们去写SQL
381 0
Flink SQL代码补全提示(源码分析)
|
SQL Windows
SQL Server安装提示【需要microsoft.NET Framework 3.5 Service Pack 1】
SQL Server安装提示【需要microsoft.NET Framework 3.5 Service Pack 1】
549 0
SQL Server安装提示【需要microsoft.NET Framework 3.5 Service Pack 1】
|
SQL Java 关系型数据库
spring boot集成mybatis只剩两个sql 并提示 Cannot obtain primary key information from the database, generated objects may be incomplete
spring boot集成mybatis只剩两个sql 并提示 Cannot obtain primary key information from the database, generated objects may be incomplete
123 0
spring boot集成mybatis只剩两个sql 并提示 Cannot obtain primary key information from the database, generated objects may be incomplete