Cassandra

primary key

cassandra 的 primary key 由 partition key, composite key, clustered key 組成

最簡單的 primary key =>

create table table1 (
    key text PRIMARY KEY,
    data text
);

此時這個 primary key 就是 partition key, clustering key

第二種 =>

create table table2 (
    key1 text,
    key2 int,
    data text,
    PRIMARY KEY(key1, key2)
);

key1、key2 共同組成 primary key, 這個也叫做 composite key, 其中 key1 是 partition key, key2 是 clustering key

第三種是 =>

create table table3(
    key1 text,
    key2 int,
    key_cluster_1 text,
    key_cluster_2 int,
    key_cluster_3 uuid,
    data text,
    RPIMARY KEY((key1, key2), key_cluster_1, key_cluster_2, key_cluster_3)
);

(key1, key2) 共同組成 partition key, 後面三個都是 clustering key

partition key 用來決定資料在各個 cassandra 節點是如何分區的

clustering key 用來做 range query

primary key 決定資料的唯一性

partition key 是用來解決 load balance 的一種方法,可以用 partition key + consistent hashing 來做


查詢小提示:

  • cassandra 查詢要在 primary key 上
  • (A, B) 形式的PK,假如查詢要不帶 A(partition key),則需要開啟 allow filtering,因為cassandra 是 key-value 形式的資料庫,所以如果缺少 partition key 則是會把這個表中所有資料拿出來在一個個過濾 B 的條件,因此 filtering 效率極差
  • ((A, B), C, D): A B 需要一起出現, C D不可跳躍

results matching ""

    No results matching ""