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); 

這裏寫圖片描述


2.3 插入數據


插入測試數據。

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


2.4 查詢數據


查詢數據表與索引表數據。

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

這裏寫圖片描述


2.5 更新數據


更新id爲001的數據(這裏是爲了測試才進行數據更新操作的,否則的話最好不要對聲明瞭immutable的表進行數據更新)。

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

重新查詢數據表與索引表。

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

這裏寫圖片描述

可以看到索引表中的數據並沒有被修改,而是被追加了!這就是immutable類型的索引。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章