1. CassandraでIndexをつける際の備忘録
materialized viewを利用することを前提に、Cassandraのテーブルをどう作るかを検討する
実質検討中な為、内容は変更されることが前提です
materialized viewを利用する為に最低限のindexを振る
パーティションキーとしてカテゴリ、INDEXとしてUNIQになる最小の設定を入れる
baseとなるtable
CREATE TABLE IF NOT EXISTS example.basetable (
partition_id int,
item_code text,
seq_id bigint,
PRIMARY KEY((partition_genre_id), item_code)
);
order by させたいキーをprimary keyとして設定することで order by できるようにする
order_byさせる為のview
create materialized view if not exists example.basetable_mv
as select * from basetable
where item_code is not null
and seq_id is not null
and partition_genre_id is not null
primary key ((partition_genre_id), seq_id, item_code);
primary keyとして設定されていないので、エラーになる
検証用Query--結果NG
SELECT
item_code,
seq_id
FROM example.basetable
where partition_genre_id = 211553 and seq_id > 0
order by seq_id limit 1000 ALLOW FILTERING;
cqlsh:mykeyspace> SELECT item_code, seq_id FROM example.basetable where partition_genre_id = 211553 and seq_id > 0 order by seq_id limit 3 ALLOW FILTERING;
InvalidRequest: Error from server: code=2200 [Invalid query] message="Order by is currently only supported on the clustered columns of the PRIMARY KEY, got seq_id"
primary keyとして再設定することでorder by が可能となる
検証用Query--結果OK
SELECT
item_code,
seq_id
FROM example.basetable_mv
where partition_genre_id = 211553 and seq_id > 0
order by seq_id limit 1000 ALLOW FILTERING;
cqlsh:mykeyspace> SELECT item_code, seq_id FROM example.basetable_mv where partition_genre_id = 211553 and seq_id > 0 order by seq_id limit 3 ALLOW FILTERING;
item_code | seq_id
------------------+----------------
XXX:11234299 | 15358979170100
XX1:11789006 | 15358979170101
XX2:11771190 | 15358979170103
(3 rows)