【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  

相关文章
|
SQL 关系型数据库 MySQL
OBCP第四章 SQL调优-Hint
OBCP第四章 SQL调优-Hint
467 0
|
10月前
|
SQL
【YashanDB知识库】使用leading hint调整SQL执行计划后报错YAS-04522 invalid hint leading
【YashanDB知识库】使用leading hint调整SQL执行计划后报错YAS-04522 invalid hint leading
【YashanDB知识库】使用leading hint调整SQL执行计划后报错YAS-04522 invalid hint leading
|
11月前
|
SQL
【YashanDB 知识库】使用 leading hint 调整 SQL 执行计划后报错 YAS-04522 invalid hint leading
在 YashanDB 的所有版本中,使用 leading hint 调整 SQL 执行计划时可能出现“YAS-04522 invalid hint leading”错误,导致 SQL 无法正常执行。原因是 YashanDB 优化器的 Bug。解决方法为避免使用 leading hint。可通过创建测试表 a、b、c 并执行特定 SQL 语句来验证问题是否存在。
|
SQL Oracle 关系型数据库
|
SQL 数据库 索引
SQL CREATE INDEX 语句
【7月更文挑战第20天】SQL CREATE INDEX 语句。
142 1
|
SQL 运维 安全
数据管理DMS产品使用合集之执行SQL时,如何添加Hint来改变查询的执行计划
阿里云数据管理DMS提供了全面的数据管理、数据库运维、数据安全、数据迁移与同步等功能,助力企业高效、安全地进行数据库管理和运维工作。以下是DMS产品使用合集的详细介绍。
270 1
|
SQL 数据库 索引
SQL CREATE INDEX 语句
SQL CREATE INDEX 语句
154 4
|
SQL 存储 流计算
Flink SQL 在快手实践问题之表示 Mini-Batch hint如何解决
Flink SQL 在快手实践问题之表示 Mini-Batch hint如何解决
146 0
|
SQL 弹性计算 分布式计算
实时数仓 Hologres操作报错合集之在执行SQL查询时遇到了问题,报错原因是“Invalid index column id: 2”,该怎么处理
在使用阿里云实时数仓Hologres时,可能会遇到不同类型的错误。例如:1.内存超限错误、2.字符串缓冲区扩大错误、3.分区导入错误、4.外部表访问错误、5.服务未开通或权限问题、6.数据类型范围错误,下面是一些常见错误案例及可能的原因与解决策略的概览。
|
SQL Oracle 关系型数据库
SQL CREATE INDEX 语句- 提高数据库检索效率的关键步骤
SQL CREATE INDEX 语句用于在表中创建索引。 索引用于比其他方式更快地从数据库中检索数据。用户无法看到索引,它们只是用于加速搜索/查询。 注意: 使用索引更新表比不使用索引更新表需要更多的时间(因为索引也需要更新)。因此,只在经常进行搜索的列上创建索引。
208 5