解析索引

解析索引
参考《oracle DBA工作笔记》
在oracle中对于索引的存储是采用btree索引来实现。btree是树形结构,索引分为根节点,分支节点和叶子节点。最后叶子节点的数据和表的数据行就会有一个最终的映射。

1、关于索引的内部信息
我们创建一个表。
create table index_test as select *from user_objects;
然后创建一个索引。
create index inx_test on index_test(object_id);
使用analyze来分析索引的信息,尽管在新版本中我们建议使用dbms_stats来替代analyze,但是analyze validate structure这个功能时analyze独有的,dbms_stats在这方面还不能完全替代analyze.
analyze index inx_test validate structure;
select height,blocks,br_blks,lf_blks,lf_rows,del_lf_rows from index_stats; --高度  索引总块数   枝干块数    叶子块数  叶子内行数    叶子中被删除的行数  

2、关于索引的访问方式
针对不同的使用场景使用不认同的访问方式。
2.1、索引唯一扫描
如下使用了INDEX UNIQUE SCAN,这是最快的索引访问方式,适用于'where object_id=258'。
SQL> create table a as select object_id,object_name,object_type from dba_objects;

Table created.

SQL>  analyze table a compute statistics;

Table analyzed.

SQL> create unique index ind_a on a(object_id);  

Index created.

SQL> set autotrace on
SQL> set linesize 150
SQL> select * from a where object_id=258;

 OBJECT_ID OBJECT_NAME
---------- --------------------------------------------------------------------------------------------------------------------------------
OBJECT_TYPE
-------------------
       258 PLSCOPE_IDENTIFIER$
TABLE



Execution Plan
----------------------------------------------------------
Plan hash value: 2087649606

-------------------------------------------------------------------------------------
| Id  | Operation                   | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |       |     1 |    36 |     2   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| A     |     1 |    36 |     2   (0)| 00:00:01 |
|*  2 |   INDEX UNIQUE SCAN         | IND_A |     1 |       |     1   (0)| 00:00:01 |
-------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("OBJECT_ID"=258)


Statistics
----------------------------------------------------------
          0  recursive calls
          0  db block gets
          3  consistent gets
          0  physical reads
          0  redo size
        568  bytes sent via SQL*Net to client
        512  bytes received via SQL*Net from client
          1  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed

2.2、快速索引全扫描
因为不涉及排序,所以快一些。
select object_id from a;

Execution Plan
----------------------------------------------------------
Plan hash value: 2248738933

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      | 86264 |   336K|   142   (0)| 00:00:02 |
|   1 |  TABLE ACCESS FULL| A    | 86264 |   336K|   142   (0)| 00:00:02 |
--------------------------------------------------------------------------
没有走索引,排除null干扰
SQL> alter table a modify(object_id not null);

Table altered.

SQL> set autot traceonly exp
SQL> select object_id from a;

Execution Plan
----------------------------------------------------------
Plan hash value: 672397539

------------------------------------------------------------------------------
| Id  | Operation            | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------
|   0 | SELECT STATEMENT     |       | 86264 |   336K|    50   (0)| 00:00:01 |
|   1 |  INDEX FAST FULL SCAN| IND_A | 86264 |   336K|    50   (0)| 00:00:01 |
------------------------------------------------------------------------------
2.3、索引全扫描
如果要对索引做排序,可以使用索引全扫描,适用于'order by'。
可以做个对比
SQL> select object_id from a order by object_id;

Execution Plan
----------------------------------------------------------
Plan hash value: 1197305525

--------------------------------------------------------------------------
| Id  | Operation        | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT |       | 86264 |   336K|   181   (1)| 00:00:03 |
|   1 |  INDEX FULL SCAN | IND_A | 86264 |   336K|   181   (1)| 00:00:03 |
--------------------------------------------------------------------------

SQL> select object_id from a;

Execution Plan
----------------------------------------------------------
Plan hash value: 672397539

------------------------------------------------------------------------------
| Id  | Operation            | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------
|   0 | SELECT STATEMENT     |       | 86264 |   336K|    50   (0)| 00:00:01 |
|   1 |  INDEX FAST FULL SCAN| IND_A | 86264 |   336K|    50   (0)| 00:00:01 |
------------------------------------------------------------------------------
2.4、区间扫描
如果涉及索引列的区间值,可以用区间,适用于扫描between。
SQL> select * from a where object_id between 2000 and 3000;

Execution Plan
----------------------------------------------------------
Plan hash value: 1070634250

-------------------------------------------------------------------------------------
| Id  | Operation                   | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |       |   989 | 35604 |    11   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| A     |   989 | 35604 |    11   (0)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN          | IND_A |   989 |       |     4   (0)| 00:00:01 |
-------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("OBJECT_ID">=2000 AND "OBJECT_ID"<=3000)
2.5、跳跃索引扫描
索引有好几列,语句只用一列。
SQL> create index ind_a on a(object_type,object_id,object_name);

Index created.

SQL> analyze table a compute statistics for all indexed columns;

Table analyzed.

SQL> select * from a where object_id=258;

Execution Plan
----------------------------------------------------------
Plan hash value: 4117514

--------------------------------------------------------------------------
| Id  | Operation        | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT |       |     1 |    36 |    46   (0)| 00:00:01 |
|*  1 |  INDEX SKIP SCAN | IND_A |     1 |    36 |    46   (0)| 00:00:01 |
--------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - access("OBJECT_ID"=258)
       filter("OBJECT_ID"=258)

3、索引和空值
创建索引会因为空值出现偏差;
空值不在索引中,查询会因为空值走不了索引。
如果需要输出非空的数据,加入is NOT NULL的过滤条件,索引就能正常启用。

4、约束和索引
用alter table test add constraint con_test_id_uq unique(id);添加约束,不要用modify。
约束和索引要尽量分开,因为约束disable的情况下,索引会连带删除,如果是分区表的全局索引那么影响也是全局性的。
索引和约束分离,索引列顺序不当会导致不走索引。

5、虚拟索引
使用虚拟索引,通过查看执行计划来比较前后的变化,如果提升幅度大,再来考虑创建对应的索引。
SQL>  create table t as select *from dba_objects where object_id is not null and rownum<100000;

Table created.
正常情况
SQL> select *from t where object_id=8040;

Execution Plan
----------------------------------------------------------
Plan hash value: 1601196873

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |    14 |  2898 |   336   (1)| 00:00:05 |
|*  1 |  TABLE ACCESS FULL| T    |    14 |  2898 |   336   (1)| 00:00:05 |
--------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("OBJECT_ID"=8040)

Note
-----
   - dynamic sampling used for this statement (level=2)
创建虚拟索引
SQL> create unique index inx_t on t(object_id) nosegment;

Index created.
发现没有使用虚拟索引
SQL> select *from t where object_id=8040;

Execution Plan
----------------------------------------------------------
Plan hash value: 1601196873

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |     1 |   207 |   336   (1)| 00:00:05 |
|*  1 |  TABLE ACCESS FULL| T    |     1 |   207 |   336   (1)| 00:00:05 |
--------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("OBJECT_ID"=8040)

Note
-----
   - dynamic sampling used for this statement (level=2)
在会话级别启用虚拟索引
SQL> alter session set "_USE_NOSEGMENT_INDEXES" = true;

Session altered.
虚拟索引生效
SQL> select *from t where object_id=8040;

Execution Plan
----------------------------------------------------------
Plan hash value: 1855406669

-------------------------------------------------------------------------------------
| Id  | Operation                   | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |       |     1 |   207 |     2   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| T     |     1 |   207 |     2   (0)| 00:00:01 |
|*  2 |   INDEX UNIQUE SCAN         | INX_T |     1 |       |     1   (0)| 00:00:01 |
-------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("OBJECT_ID"=8040)

Note
-----
   - dynamic sampling used for this statement (level=2)

6、不可见索引
创建不可见索引会锁表,对业务有影响,对比虚拟索引,不可见索引有索引段,也有数据字典信息,这些虚拟索引没有。
SQL> create table t as select *from dba_objects where object_id is not null and rownum<100000;

Table created.

SQL> set autot trace exp stat
正常状态
SQL> select *from t where object_id=9445 ;


Execution Plan
----------------------------------------------------------
Plan hash value: 1601196873

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |    14 |  2898 |   336   (1)| 00:00:05 |
|*  1 |  TABLE ACCESS FULL| T    |    14 |  2898 |   336   (1)| 00:00:05 |
--------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("OBJECT_ID"=9445)

Note
-----
   - dynamic sampling used for this statement (level=2)


Statistics
----------------------------------------------------------
         24  recursive calls
          0  db block gets
       1321  consistent gets
       1464  physical reads
          0  redo size
       1628  bytes sent via SQL*Net to client
        523  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed
创建不可见索引
SQL> create unique index inx_t on t(object_id) invisible;

Index created.
不可见索引没有生效
SQL> select *from t where object_id=9445 ;


Execution Plan
----------------------------------------------------------
Plan hash value: 1601196873

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |  1005 |   203K|   336   (1)| 00:00:05 |
|*  1 |  TABLE ACCESS FULL| T    |  1005 |   203K|   336   (1)| 00:00:05 |
--------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("OBJECT_ID"=9445)


Statistics
----------------------------------------------------------
          1  recursive calls
          0  db block gets
       1235  consistent gets
       1231  physical reads
          0  redo size
       1628  bytes sent via SQL*Net to client
        523  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed
使不可见索引生效
SQL> alter session set "optimizer_use_invisible_indexes"=true;

Session altered.

SQL> select *from t where object_id=9445 ;


Execution Plan
----------------------------------------------------------
Plan hash value: 1855406669

-------------------------------------------------------------------------------------
| Id  | Operation                   | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |       |     1 |   207 |     2   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| T     |     1 |   207 |     2   (0)| 00:00:01 |
|*  2 |   INDEX UNIQUE SCAN         | INX_T |     1 |       |     1   (0)| 00:00:01 |
-------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("OBJECT_ID"=9445)


Statistics
----------------------------------------------------------
          1  recursive calls
          0  db block gets
          3  consistent gets
          2  physical reads
          0  redo size
       1495  bytes sent via SQL*Net to client
        512  bytes received via SQL*Net from client
          1  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          1  rows processed
使不可见索引无效
SQL> alter session set "optimizer_use_invisible_indexes"=false;

Session altered.
将不可见索引变为正常索引
SQL> alter index inx_t visible;

Index altered.

SQL> select *from t where object_id=9445 ;


Execution Plan
----------------------------------------------------------
Plan hash value: 1855406669

-------------------------------------------------------------------------------------
| Id  | Operation                   | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |       |     1 |   207 |     2   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| T     |     1 |   207 |     2   (0)| 00:00:01 |
|*  2 |   INDEX UNIQUE SCAN         | INX_T |     1 |       |     1   (0)| 00:00:01 |
-------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("OBJECT_ID"=9445)


Statistics
----------------------------------------------------------
         34  recursive calls
          0  db block gets
         20  consistent gets
          0  physical reads
          0  redo size
       1495  bytes sent via SQL*Net to client
        512  bytes received via SQL*Net from client
          1  SQL*Net roundtrips to/from client
          4  sorts (memory)
          0  sorts (disk)
          1  rows processed

### 回答1: c 解析索引文件shx是指对shx文件进行解析和读取操作。shx文件是ESRI Shapefile文件格式中的一种索引文件,用于加快对shp文件中几何对象的访问速度。 解析索引文件shx的过程包括以下几个步骤: 1. 打开shx文件:首先需要打开待解析的shx文件,可以使用相应的文件读取函数打开文件流。 2. 读取文件头:shx文件的开头包含了一些元数据,如记录总数和文件版本等信息。解析过程需要读取这些信息,以便后续的处理。 3. 读取记录索引:shx文件的主要内容是记录索引,每个索引对应shp文件中的一个几何对象。解析过程需要读取每个记录索引的偏移量和长度等信息。 4. 解析记录索引:根据记录索引的偏移量和长度,可以通过读取shp文件对应位置的数据,获取到相应的几何对象。解析过程需要根据索引文件的信息,定位到shp文件中对应的位置,并读取相应的数据。 5. 关闭shx文件:解析完成后,需要关闭打开的shx文件流,释放系统资源。 解析索引文件shx的主要目的是为了提高对shp文件中几何对象的读取速度,通过索引文件确定shp文件中每个几何对象的位置,以便快速定位和读取对应的数据。在GIS领域中,shx文件扮演着重要的角色,能够使空间数据的管理和处理更加高效。 ### 回答2: c是一种文件格式,用于解析索引文件shx。shx文件是由ArcGIS软件生成的空间索引文件,其目的是提高地理数据的查询和检索效率。 要解析shx文件,首先需要了解shx文件的结构。shx文件包含了地理数据的索引信息,它通常与.shp文件一起使用。shx文件采用二进制格式存储,它由一系列记录组成。 每个shx记录包含两个部分:记录头和偏移量。记录头包含记录号和记录长度,用于标识和定位记录。偏移量表示该记录在.shp文件中的起始位置。 解析shx文件的方法是遍历每个记录,读取其中的记录头和偏移量。通过记录头可以确定记录的类型和长度,通过偏移量可以定位到.shp文件中的对应记录。 为了解析shx文件,我们可以使用编程语言,如Python或C++。首先,我们需要打开shx文件,读取其中的二进制数据。然后,我们可以按照记录的数据结构,依次读取每个记录的记录头和偏移量。 通过对记录头和偏移量的解析,我们可以获取到地理数据的索引信息。例如,我们可以获取每个几何对象的类型(如点、线或面)和坐标范围。 解析shx文件可以帮助我们理解地理数据的空间关系,并且可以为后续的地理数据查询和分析提供支持。通过读取shx文件,我们可以快速定位和访问需要的地理数据,从而提高数据的处理效率和准确性。 总而言之,解析索引文件shx是一种获取地理数据索引信息的方法。通过对shx文件的解析,我们可以理解地理数据的空间关系,并且可以为后续的地理数据查询和分析提供支持。 ### 回答3: c解析索引文件shx是指对shx文件进行解析和处理的过程。 shx文件是Shapefile文件格式中的一种索引文件,用于加快对空间数据的查询和检索速度。它记录了shapefile文件中每个几何对象的位置和大小信息。 要解析shx文件,首先需要了解shx文件的结构。shx文件由多个记录组成,每个记录包含两个32位整数值,分别表示shapefile文件中对应几何对象的偏移量和长度。 解析shx文件的步骤如下: 1. 打开shx文件,并读取文件头信息,确定记录的数量。 2. 对于每个记录,读取偏移量和长度值。 3. 根据偏移量和长度值,定位到shapefile文件中相应几何对象的位置。 4. 解析几何对象的数据,根据Shapefile文件格式进行解析。 5. 根据需要,可以进一步解析几何对象的属性信息。 解析shx文件可以得到shp文件中所有几何对象的位置和大小信息,从而可以快速地检索和查询特定区域的几何对象。这对于空间数据分析、地理信息系统等应用领域非常重要。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值