TimesTen索引的概念与日常操作

TimesTen中的索引和其它数据库一样,都是为了加速查询

Indexes are auxiliary data structures that greatly improve the performance of table searches.
Indexes are used automatically by the query optimizer to speed up the execution of a query.

演示环境准备

create table test(a int, b timestamp, primary key(a));

declare a number := 1;
begin for i in 1..100000 loop a := a+1;
    insert into test values(a, sysdate);
    commit;
end loop;
end;
/

索引类型和查看

TimesTen支持三种索引类型,这比Oracle要简单得多,内存数据库的性能使其没有必要支持那么多类型的索引。

  • Range Index(T-tree Index)
    适合于范围匹配,如where a between c and d, 或where a > value

    Range indexes are useful for finding rows with column values within a certain range.
    但没有指定Index类型时,Range Index为缺省类型。

  • Hash Index
    适合于完全匹配, 如where a=value

    are useful for equality searches

  • Bitmap Index

    Bitmap indexes are useful when searching and retrieving data from columns with low cardinality
    low cardinality指列具有较少的唯一值,如性别。bitmap中的每一个bit表示一行。
    Bitmap Index占用空间较少。

三者的比较如下:

Hash indexes are faster than range indexes for exact match lookups, but they require more space than range indexes. Hash indexes cannot be used for lookups involving ranges.
Range indexes are optimized for in-memory data management and provide efficient sorting by column value.
TimesTen may create temporary hash and range indexes automatically during query processing to speed up query execution.

查看索引类型可以使用indexes命令或查询系统表:
indexes命令

Command> indexes

Indexes on table ORACLE.A:
  No indexes found.

Indexes on table ORACLE.TEST:
  TEST: unique range index on columns:
    A
  HASHIDX: non-unique hash index on columns: 
    A
  2 indexes found.

Indexes on table ORACLE.TEST1:
  TEST1: unique range index on columns:
    A
  1 index found.

3 indexes found on 3 tables.
Command> indexes test

Indexes on table ORACLE.TEST:
  TEST: unique range index on columns:
    A
  HASHIDX: non-unique hash index on columns: 
    A
  2 indexes found.

2 indexes found on 1 table.

查询系统表

select t.tblname, i.ixname, i.ixtype,
(case i.ixtype when 0 then 'HASH'
when 1 then 'T-TREE'
when 2 then 'BITMAP'
when 3 then 'B+TREE'
else 'WeirdTypeFound' end)
from tables t, indexes i
where t.tblowner = current_user
and t.tblid = i.tblid
order by t.tblname, i.ixtype;

< TEST                           , HASHIDX                        , 0, HASH >
< TEST                           , TEST                           , 1, T-TREE >
< TEST1                          , TEST1                          , 1, T-TREE >
3 rows found.

创建索引

每一个index都有属主,即创建表的属主
LOB列不能有索引
例:

CREATE HASH INDEX custname_idx ON customer(cust_name);

HASH索引的特殊考虑

为避免hash冲突,创建hash索引时需指定hash index pages参数,算法是表中估计的行数除以256,如果表的行数变化很大,就建议用range index。例如:

Command> select count(*) from test;
< 100000 >
1 row found.
Command> create hash index hashidx on test(a) pages = 390;

其中390=100000/256

修改索引

唯一可改的是使用range index还是hash index。

You can use the ALTER TABLE statement to add (or change) a primary key constraint to use either a range or hash index.

Command> alter table test1 add constraint test1 primary key(a);
Command> indexes test1

Indexes on table ORACLE.TEST1:
  TEST1: unique range index on columns:
    A
  1 index found.

1 index found on 1 table.
Command> alter table test1 use hash index pages = current;

Command> alter table test1 use range index;
Command> indexes test1

Indexes on table ORACLE.TEST1:
  TEST1: unique range index on columns:
    A
  1 index found.

1 index found on 1 table.

删除索引

语法:DROP INDEX [Owner.]IndexName [FROM [Owner.]TableName]
以下演示Drop index无法删除primary key,Primary key不允许删除。

Command> indexes

Indexes on table ORACLE.A:
  IDX1: non-unique range index on columns: 
    A
  1 index found.

Indexes on table ORACLE.TEST:
  TEST: unique range index on columns:
    A
  1 index found.

Indexes on table ORACLE.TEST1:
  No indexes found.

2 indexes found on 3 tables.
Command> drop index idx1;
Command> drop index test;
 2215: Attempt to drop a primary key index
The command failed.

以下演示index重名时需指定表名

Command> create index idx1 on a(a);
Command> create index idx1 on test1(a);
Command> indexes

Indexes on table ORACLE.A:
  IDX1: non-unique range index on columns: 
    A
  1 index found.

Indexes on table ORACLE.TEST:
  TEST: unique range index on columns:
    A
  1 index found.

Indexes on table ORACLE.TEST1:
  IDX1: non-unique range index on columns: 
    A
  1 index found.

3 indexes found on 3 tables.
Command> drop index idx1;
 2222: Index name is not unique. More than one table has an index with this name.
The command failed.
Command> drop index idx1 from test1;
Command> drop index idx1 from a;

估算/计算索引空间

使用ttSize,如下:

[oracle@tt12c ~]$ ttsize -tbl test sampledb_1122

Rows = 100000

Total in-line row bytes = 6040494

Indexes:
  Range index ORACLE.TEST adds 2035434 bytes
  Total index bytes = 2035434

Total = 8075928

[oracle@tt12c ~]$ ttsize -tbl test -rows 11111111 sampledb_1122

Rows = 11111111

Total in-line row bytes = 669500798

Indexes:
  Range index ORACLE.TEST adds 225752554 bytes
  Total index bytes = 225752554

Total = 895253352

dsmap不能估算,但可以监控当前索引占用空间:

“`
[oracle@tt12c support]$ ./dsmap -dsname /home/oracle/TimesTen/tt1122/info/DemoDataStore/sampledb_1122.ds0 -tblsize oracle.test
2016-05-03 18:58:55.799
/home/oracle/TimesTen/tt1122/info/DemoDataStore/sampledb_1122.ds0
TimesTen Release 11.2.2.8.11

* WARNING - Checkpoint file is not for a sync checkpoint!

DSN = sampledb_1122
filesize = 61048560 (58.22m)
maxid_file = 41941112
maxid_ds = 41943040

Size information for ORACLE.TEST

Total logical pages = 391
Logical page space used = 1807984

In-line information

Total physical pages = 391
Used rows = 100000
Free rows = 96
Total in-line size = 4210288

Space usage information for index ORACLE.TEST

Index type is T-Tree

Hdr size = 1440
Node size = 672
Nodes = 3076

Total index usage = 2068512

System usage = 18416

Total usage for ORACLE.TEST= 8105200
“`

使用Index Advisor作出索引建议

选择正确的索引对性能影响很大,Index Advisor可以针对特定的SQL工作负载提出索引建议。一般用于读密集型工作负载,对于写密集型的工作负载不建议使用。
后续会做详细介绍,这里就不赘述了。

参考

  • Oracle® TimesTen In-Memory Database Operations Guide |8 Working with Data in a TimesTen Database| Understanding indexes
  • How do I check whether B+Tree or T-Tree indexes are being created on a TimesTen datastore? (文档 ID 1504280.1)

你可能感兴趣的:(索引,bitmap,hash,range,timesten)