29
その名は 'Matrix' Amazon Redshiftの元となった スケールアウト型カラムナーDB 徹底解説 平間大輔 株式会社インサイトテクノロジー

[db tech showcase Tokyo 2015] A14:Amazon Redshiftの元となったスケールアウト型カラムナーDB徹底解説 その名は'Matrix' by 株式会社インサイトテクノロジー

Embed Size (px)

Citation preview

その名は'Matrix'Amazon Redshiftの元となったスケールアウト型カラムナーDB

徹底解説

平間大輔株式会社インサイトテクノロジー

データ分析もSQLでやりたい!

SQL on Hadoop

HybridDWH

で、MatrixってどんなDB?

• MPP型でスケールアウト可能なカラムナーDB

• 複数の圧縮アルゴリズムで効率的にデータを圧縮

• クエリはオブジェクトコードにコンパイルされ、高速に実行

• インターコネクトは独自プロトコル(UDPベース)で高速化

• UDF(ユーザー定義関数)が使用可能

• SQL文でHadoopや他RDBといったデータソースを統合して分析可能(ODI)

99%

1%

tweets demo tweets

世界中のツイート(*1)の1%

を抽出

(2012/4/23 - 2012/4/25)

ツイートの語句解析(MeCabに

よる形態素解析)

データベースにロード

デモのためデータを5倍に増幅

- ツイートデータで1000万件

(9GB)

- 解析後語句データで260万件

(100MB)

データロードは各データベースベ

ンダーが用意するローダーを使用

1000万件のツイートデータ

から世界のツイート数の国別

ランキングを出してみると?

1000万件のツイートデータ

から世界のツイートの時間帯

別ツイート数の推移を見てみ

ると?

1000万件のツイートデータ

と260万件の解析後の語句

データから日本でホット

だったキーワードを探って

みると?

まずは軽く力試し

Amazon RedshiftはMatrixを採用

“Actian has an industry leading solution and is rethinking database cloud – we’re excited to back such a strong team.”

- Jeff Blackburn, SVP of Business Development for Amazon

Amazon Redshift is the fastest growing service in their portfolio

Selected after deep evaluation against all competitors based on price-performance value proposition of Actian’s platform

Amazon Redshift service has over 1000 new customers since launch in Feb ’13

Actian complements Redshift with on-premise, high-scale analytics suite and support

Actian Analytics Platform Underpins AMAZON REDSHIFT Created New Cloud Service driving $50M+ revenue annually.

採用の理由はスケールアウト

0:00:00.00

0:00:43.20

0:01:26.40

0:02:09.60

0:02:52.80

0:03:36.00

0:04:19.20

0:05:02.40

0:05:45.60

2nodes 4nodes 8nodes 16nodes

TPC-H 30GB

0:00:00.00

0:01:26.40

0:02:52.80

0:04:19.20

0:05:45.60

0:07:12.00

0:08:38.40

0:10:04.80

0:11:31.20

4nodes 8nodes 16nodes

TPC-H 100GB

採用の理由はスケールアウト

Matrixの構成

スケールアウトの秘密を暴く

Leader Node

Parser

Queries & Joins Non-Query Commands

Execution Planner & Optimizer

Code Generation

Queue & Workload Manager

Scheduler

Communication Layer

listener ODBC / JDBC / PSQL

データ処理の基本は「Slice」

Leader Node

Compute Node 1

Slice 1

1, 10, 20, 30, 40, 50, 60, 70….

5, 14, 24, 34, 44, 54, 64, 74….

Slice 2

2, 11, 21, 31, 41, 51, 61, 71….

6, 15, 25, 35, 45, 55, 65, 75….

Slice 3

3, 12, 22, 32, 42, 52, 62, 72....

7, 16, 26, 36, 46, 56,

66, 76…

Slice 4

4, 13, 33, 43, 53, 63,

73….

8, 17, 27, 37, 47, 57,

67, 77…

Compute Node 2

Slice 5

5, 14, 24, 34, 44, 54, 64, 74….

1, 10, 20, 30, 40, 50, 60, 70….

Slice 6

6, 15, 25, 35, 45, 55, 65, 75….

2, 11, 21, 31, 41, 51, 61, 71….

Slice 7

7, 16, 26, 36, 46, 56, 66, 76….

3, 12, 22, 32, 42, 52, 62, 72....

Slice 8

8, 17, 27, 37, 47, 57,

67, 77…

4, 13, 33, 43, 53, 63,

73….

Stream, Segment, Step

集計処理は2段階

Supplier

Nation AcctBal

S0---------

12448

S1---------

11778

S0---------

$5$10$12$15$30

S1---------

$10$11$15$5$8

S0Hash aggregate table

1 – $52 – $104 – $278 - $30

S1Hash aggregate table

1 – $217 – $208 - $8

S0-----------

11122

S1---------

44

S0---------

$5$21$15$10$7

S1---------

$27$1054

Aggr

Dist

Aggr

Supplier

Nation AcctBal

S2---------

1779

10

S3---------

24469

S2---------

$15$1

$12$10$3

S3---------

$7$1050

$4$10$2

S2Hash aggregate table

1 – $157 – $139 – $1010 - $3

S3Hash aggregate table

2 – $74 – $1054

6 – $109 = $2

S2-----------

677

S3---------

8899

10

S2---------

$10$20$13

S3---------

$30$8

$10$2$3

Node 2Node 1

... ...

Scan

S2Hash aggregate table

6 – $107 – $33

S3Hash aggregate table

8 – $389 – $1210 – $3

S0Hash aggregate table

1 – $412 – $17

S1Hash aggregate table

4 – $1081

select nation, sum(acctbal) from supplier group by nation;

create table supplier (suppkey int4 not null distkey,name char(25) not null,address varchar(40) not null,nation int4 not null,phone char(15) not null,acctbal numeric(12,2) not null,comment varchar(101) not null

) sortkey (suppkey);

Queryの実行例

• TPC-H Q16

selectp_brand, p_type, p_size, count(distinct ps_suppkey) as supplier_cnt

frompartsupp, part

wherep_partkey = ps_partkeyand p_brand <> 'Brand#15'and p_type not like 'STANDARD POLISHED%'and p_size in (3, 8, 49, 19, 29, 9, 47, 32)and ps_suppkey not in (

select s_suppkeyfrom supplierwhere s_comment like '%Customer%Complaints%'

)group by

p_brand, p_type, p_sizeorder by

supplier_cnt desc, p_brand, p_type, p_size;

Queryの実行例stm | seg | step | maxtime | avgtime | rows | bytes | label | slices

-----+-----+------+-----------------+-----------------+----------+------------+----------------------+--------

0 | 0 | 0 | 00:00:00.082427 | 00:00:00.047169 | 479 | 37414 | scan tblP =108506 | 32

0 | 0 | 2 | 00:00:00.082427 | 00:00:00.047169 | 479 | 1916 | bcast | 32

0 | 1 | 0 | 00:00:00.099844 | 00:00:00.094777 | 7664 | 30656 | scan fabric=67 | 32

0 | 1 | 2 | 00:00:00.099844 | 00:00:00.094777 | 7664 | 122624 | hash tblT =240 | 32

1 | 2 | 0 | 00:00:00.002559 | 00:00:00.001517 | 0 | 0 | scan tblP =108490 | 32

1 | 2 | 1 | 00:00:00.002559 | 00:00:00.001517 | 0 | 0 | sort tblT =242 | 32

2 | 3 | 0 | 00:00:00.002244 | 00:00:00.001048 | 0 | 0 | scan tblP =108514 | 32

2 | 3 | 1 | 00:00:00.002244 | 00:00:00.001048 | 0 | 0 | sort tblT =244 | 32

3 | 4 | 0 | 00:00:02.231598 | 00:00:01.725522 | 79999318 | 1279989088 | scan tblP =108514 | 32

3 | 4 | 1 | 00:00:02.231598 | 00:00:01.725522 | 0 | 0 | merge | 32

3 | 4 | 5 | 00:00:02.231598 | 00:00:01.725522 | 11885548 | 0 | mjoin tbl =239 | 32

3 | 4 | 8 | 00:00:02.231598 | 00:00:01.725522 | 11879878 | 0 | hjoin tblT =240 | 32

3 | 4 | 11 | 00:00:02.231598 | 00:00:01.725522 | 11879783 | 712462256 | aggr tblT =257 | 32

3 | 4 | 12 | 00:00:02.231598 | 00:00:01.725522 | 11879783 | 565281908 | dist | 32

3 | 5 | 0 | 00:00:02.426613 | 00:00:02.371618 | 11879783 | 569904860 | scan fabric=68 | 32

3 | 5 | 1 | 00:00:02.426613 | 00:00:02.371618 | 11877404 | 712319756 | aggr tblT =260 | 32

4 | 6 | 0 | 00:00:00.192183 | 00:00:00.133277 | 11877404 | 712319756 | scan tblT =260 | 32

4 | 6 | 5 | 00:00:00.192183 | 00:00:00.133277 | 890878 | 56991616 | aggr tblT =265 | 32

4 | 6 | 6 | 00:00:00.192183 | 00:00:00.133277 | 890878 | 45434532 | dist | 32

4 | 7 | 0 | 00:00:00.301407 | 00:00:00.292919 | 890878 | 49864592 | scan fabric=69 | 32

4 | 7 | 1 | 00:00:00.301407 | 00:00:00.292919 | 27840 | 1780992 | aggr tblT =268 | 32

5 | 8 | 0 | 00:00:00.001789 | 00:00:00.001143 | 27840 | 1780992 | scan tblT =268 | 32

5 | 8 | 3 | 00:00:00.001789 | 00:00:00.001143 | 27840 | 1558272 | sort tblT =238 | 32

6 | 9 | 0 | 00:00:00.000595 | 00:00:00.000336 | 27840 | 1558272 | scan tblT =238 | 32

6 | 9 | 1 | 00:00:00.000595 | 00:00:00.000336 | 27840 | 1419844 | return | 32

6 | 10 | 0 | 00:00:00.046609 | 00:00:00.046609 | 27840 | 0 | merge | 1

6 | 10 | 2 | 00:00:00.046609 | 00:00:00.046609 | 0 | 0 | return | 1

データの持ち方も重要

分散キー (distkey)

catid

category

catgroup catname catdesc

distkey

Slice 0 Slice 1

catitem

catid

distkeyitemid

ソートキー (sortkey)

AmazonBufferCreateDriveEnjoy

.

.

.

.XboxYouzone

SuperFinalhigh

VACUUM

カラムナーDBの十八番、圧縮エンコード名 キーワード サポートされるデータ型 圧縮の特徴

Byte dictionary BYTEDICT VARCHARとBOOLEANを除く全て

ブロックごとに最大255通りの値を辞書に持たせることで1カラム1バイトに圧縮。

Global dictionary

GLOBALDICT256, GLOBALDICT64K

BOOLEAN以外全て カラム全体で1つの辞書を持つ。256だと255通りの値を1バイトに、64Kだと65535通りの値を2バイトに圧縮。

Delta DELTA, DELTA32K

SMALLINT (DELTAのみ), INT, BIGINT, DATE, TIMESTAMP, DECIMAL

前の値との差分を格納。差分の値が元データ型のサイズよりも小さければ圧縮可能。

Run-length RUNLENGTH 全て ランレングス圧縮。同一のデータがいくつ連続するかを格納。連続した同一値には高い圧縮率。

LZ (Lempel-Ziv) DEFLATE BOOLEAN, REAL, DOUBLE PRECISIONを除く全て

LZ法(ZIPなど一般的なファイル圧縮で使われているアルゴリズム)で圧縮。圧縮率は高いが圧縮・展開のCPU負荷も高い。

Mostly MOSTLY8 SMALLINT, INT, BIGINT, DECIMAL

カラム内の値が指定されたデータ型よりも小さいサイズで格納可能な場合、指定された(元のデータ型よりも小さい)サイズでデータを格納する。収まらない場合は元のサイズで格納。

MOSTLY16 INT, BIGINT, DECIMAL

MOSTLY32 BIGINT, DECIMAL

TEXT255, 32K TEXT255, TEXT32K

VARCHARのみ 辞書を使用した圧縮のVARCHAR版。245通りの単語まで辞書に登録可能。

上手に圧縮、サイズは1/2以下!create table lineitem (

l_orderkey int8 not null encode delta sortkey distkey,

l_partkey int4 not null,

l_suppkey int4 not null encode mostly16,

l_linenumber int4 not null encode mostly8,

l_quantity numeric(19,2) not null encode bytedict,

l_extendedprice numeric(19,2) not null encode mostly32,

l_discount numeric(19,2) not null encode mostly8,

l_tax numeric(19,2) not null encode mostly8,

l_returnflag char(1) not null encode runlength,

l_linestatus char(1) not null encode runlength,

l_shipdate date not null encode delta,

l_commitdate date not null encode delta,

l_receiptdate date not null encode delta,

l_shipinstruct char(25) not null encode bytedict,

l_shipmode char(10) not null encode bytedict,

l_comment varchar(44) not null

);

create table lineitem (

l_orderkey int8 not null sortkey distkey,

l_partkey int4 not null,

l_suppkey int4 not null,

l_linenumber int4 not null,

l_quantity numeric(19,2) not null,

l_extendedprice numeric(19,2) not null,

l_discount numeric(19,2) not null,

l_tax numeric(19,2) not null,

l_returnflag char(1) not null,

l_linestatus char(1) not null,

l_shipdate date not null,

l_commitdate date not null,

l_receiptdate date not null,

l_shipinstruct char(25) not null,

l_shipmode char(10) not null,

l_comment varchar(44) not null

);

行数: 600,037,902

40,204 MB 18,900 MB !

隠し味はUDF(ユーザー定義関数)• PL/pgSQLの例

CREATE OR REPLACE FUNCTION f_echo(_text varchar)RETURNS varchar AS$$

BEGINreturn _text;

END;$$LANGUAGE plpgsql;

• C++の例#include "padb_udf.hpp"

PADB_UDF_VERSION(charcount);extern "C"{

padb_udf::int_t charcount ( padb_udf::ScalarArg &aux, padb_udf::varchar_t *target, padb_udf::varchar_t *tst )

{padb_udf::int_t ret = 0;if ( tst->len != 1 ){

aux.throwError( __func__,"probe length must be = 1" );}char ch = tst->str[0];for ( padb_udf::len_t ix = 0; ix < target->len; ix++ ){

if ( target->str[ix] == ch ){

ret++;}

}return aux.retIntVal( ret );

}}

CREATE OR REPLACE FUNCTION charcount(target_string varchar, search_character varchar)

RETURNS intAS '/tmp/scalar_charcount.o'

LANGUAGE C STABLE;

• ユーザーのビジネスロジック組み込み• サードパーティ製分析関数• ODI(On-Demand Integration)の実現

UDFでIoT(もどき)データを分析

データロードも機械学習もSQLで

自室の明るさと室温を1秒ごとに計測!

蓄積

3rd Party UDFでお手軽機械学習

転送

ODIでデータロード

ODIでデータをロードしよう

id | dates | hours | minutes | seconds | light | temperature

---------+------------+-------+---------+---------+-------+-------------

2845949 | 2015-05-01 | 8 | 0 | 0 | 1005 | 24

2845950 | 2015-05-01 | 8 | 0 | 1 | 1006 | 24

2845951 | 2015-05-01 | 8 | 0 | 2 | 1005 | 24

2845952 | 2015-05-01 | 8 | 0 | 3 | 1005 | 24

2845953 | 2015-05-01 | 8 | 0 | 4 | 1006 | 25

2845954 | 2015-05-01 | 8 | 0 | 5 | 1006 | 24

2845955 | 2015-05-01 | 8 | 0 | 6 | 1006 | 25

2845956 | 2015-05-01 | 8 | 0 | 7 | 1005 | 24

2845957 | 2015-05-01 | 8 | 0 | 8 | 1005 | 25

2845958 | 2015-05-01 | 8 | 0 | 9 | 1005 | 24

2845959 | 2015-05-01 | 8 | 0 | 10 | 1005 | 24

2015-05-01 08:00:00 1005 24

2015-05-01 08:00:01 1006 24

2015-05-01 08:00:02 1005 24

2015-05-01 08:00:03 1005 24

2015-05-01 08:00:04 1006 25

2015-05-01 08:00:05 1006 24

2015-05-01 08:00:06 1006 25

2015-05-01 08:00:07 1005 24

2015-05-01 08:00:08 1005 25

2015-05-01 08:00:09 1005 24

2015-05-01 08:00:10 1005 24

ODIはSlave同士が直接通信

insert into getlight

(id,dates,hours,minutes,seconds,light,temperature)

select

row_number() over (order by datetimes),

to_date(substring(datetimes,1,10), 'YYYY-MM-DD'),

cast(substring(datetimes,12,2) as int),

cast(substring(datetimes,15,2) as int),

cast(substring(datetimes,18,2) as int),

light,

temperature

from odi_hadoop_import(

with jobname('get-light_imp')

masternode('iq121')

inputdir('/user/dhirama/get-light')

padb_schema('getlightbase')

delimiter('¥t')

);

HadoopデータもSQLでお手軽に

3rd party UDFで機械学習だ!

ライブラリ カテゴリー UDF数

DB Lytix Statistical Functions 31

Univariate Distributions 95

Math Functions 48

Cross Tab, ANOVA, Hypothesis Testing 12

Matrix Operations 4

Data Mining Functions 17

Fin Lytix Equity Derivatives 12

Fixed Income Functions 26

Interest Rate Functions 4

Time Series Functions 7

※ DB Lytix on Actian Matrix Finance Package 1.3.0

K-Meansでクラスタリング

SQLで一発だ!

select sp_kmeans('getlightkmeans',5,10);

明るさ、室温、計測時(Hour)の3次元で分類したい

UDFのパラメータに合わせたViewを用意

高速、お手軽、SQL

大量データにはスケールアウト。機能拡張はUDFで自由自在。SQLでお手軽に。それが、Actian Matrix。

BigData Project Accelerator医療業界の最先端事例から学ぶ

プロジェクトを加速させる要因

2015年6月30日(火) 14:00 – 17:30@ EBiS303 カンファレンスルーム (in 恵比寿)

お申込みはお手元のアンケートで!

無断転載を禁ず

この文書はあくまでも参考資料であり、掲載されている情報は予告なしに変更されることがあります。

株式会社インサイトテクノロジーは本書の内容に関していかなる保証もしません。また、本書の内容に関連したいかなる損害についても責任を負いかねます。本書で使用している製品やサービス名の名称は、各社の商標または登録商標です。