Phoenix(十)二级索引之— —Append-only Data

1. 说明

觉得还是有必要把这种类型的索引说明一下,phoenix将其二级索引技术划分为global and local indexing 2种,但是如果继续往下细分的话又分为mutable global indexing、mutable local indexing、immutable global indexing、immutable local indexing一共四种。

默认创建的二级索引为mutable的(mutable global ing或者mutable local indexing)。在上两篇文章中都对这两种索引技术大致都做出了说明。immutable类型的索引主要针对的是数据一次入库之后永不改变的场景(only written once and never updated)


2. Append-only Data

For a table in which the data is only written once and never updated in-place, certain optimizations may be made to reduce the write-time overhead for incremental maintenance. This is common with time-series data such as log or event data, where once a row is written, it will never be updated. To take advantage of these optimizations, declare your table as immutable by adding the IMMUTABLE_ROWS=true property to your DDL statement:

CREATE TABLE my_table (k VARCHAR PRIMARY KEY, v VARCHAR) IMMUTABLE_ROWS=true;

All indexes on a table declared with IMMUTABLE_ROWS=true are considered immutable (note that by default, tables are considered mutable). For global immutable indexes, the index is maintained entirely on the client-side with the index table being generated as change to the data table occur. Local immutable indexes, on the other hand, are maintained on the server-side. Note that no safeguards are in-place to enforce that a table declared as immutable doesn’t actually mutate data (as that would negate the performance gain achieved). If that was to occur, the index would no longer be in sync with the table.

在一些数据一次写入永不更新的场景中,核心的优化就是减少了在写数据时性能的开销。例如日志数据与事件类型的数据都是一次写入永不更新。通过在场景数据表的时候声明IMMUTABLE_ROWS=true来显示的说明该表的所有索引都是immutable的(默认的是mutable类型)。Global immutable indexes由客户端维护,而Local immutable indexes由服务端维护。即使创建表的时候使用了immutable声明,数据表中的数据也是可以进行更新的。如果进行了这个的操作会引起数据表的数据与索引表的数据不同步。


2.1 创建表

在创建数据表的时候声明IMMUTABLE_ROWS=true来显示的说明该表的所有索引都是immutable的。

> create table company_immutable(id varchar primary key, name varchar, address varchar) IMMUTABLE_ROWS=true; 


2.2 创建索引

对company_immutable表的name字段创建索引。

> create index name_test on company_immutable(name); 

Phoenix(十)二级索引之— —Append-only Data_第1张图片


2.3 插入数据

插入测试数据。

> upsert into company_immutable(id, name, address) values('001', 'dimensoft', 'nanjing');


2.4 查询数据

查询数据表与索引表数据。

> select * from company_immutable;
> select * from name_test;

Phoenix(十)二级索引之— —Append-only Data_第2张图片


2.5 更新数据

更新id为001的数据(这里是为了测试才进行数据更新操作的,否则的话最好不要对声明了immutable的表进行数据更新)。

upsert into company_immutable(id, name, address) values('001', 'baidu', 'beijing');

重新查询数据表与索引表。

> select * from company_immutable;
> select * from name_test;

Phoenix(十)二级索引之— —Append-only Data_第3张图片

可以看到索引表中的数据并没有被修改,而是被追加了!这就是immutable类型的索引。

你可能感兴趣的:(append,Phoenix,二级索引)