2016-09-15 44 views
-3

我是新的卡桑德拉。請幫助我瞭解如何在Cassandra中定義最佳數據模型。卡桑德拉數據模型設計方法

模型1:

CREATE TABLE users (
    id uuid PRIMARY KEY, 
    username text, 
    email text, 
    age int 
) 

CREATE TABLE users_by_username (
    username text PRIMARY KEY, 
    id uuid 
) 

CREATE TABLE users_by_email (
    email text PRIMARY KEY, 
    id uuid 
) 

優勢

: 1.No重複記錄。 2.更新/刪除一次,但要查找用戶詳細信息,需要一個選擇
查詢。

派息優點

1. To get user records, Need to select in 2 tables ( users_by_username 
     or users_by_email and users) 

模型2

CREATE TABLE users_by_username (
    id uuid PRIMARY KEY, 
    username text, 
    email text, 
    age int 
) 

    CREATE TABLE users_by_email (
    id uuid PRIMARY KEY, 
    username text, 
    email text, 
    age int 
) 

優點:

1. To get user records, only once select query. 

派息優點:

1.Duplicate records in two tables. 
    2. Update/Delete needs to performed in two tables. 

請建議我該模型會好嗎?

回答

0

模型表(列族)的最佳方法是首先定義如何訪問信息: - 您將通過uuid,電子郵件,用戶名進行查詢嗎?你的例子只有當你通過uuid(主鍵)查找時才能正常工作

+0

通過電子郵件查詢一個用例和通過用戶名查詢另一個用例 – Gnana

+0

在這種情況下,你將需要2個表: CREATE TABLE users_by_username( ID UUID, 用戶名文本, 電子郵件文本, 年齡INT, PRIMARY KEY(ID,用戶名) )' 'CREATE TABLE users_by_email( ID UUID, 用戶名文本, 電子郵件文本, 年齡int, PRIMARY KEY(id,email) )' 在進行插入,更新或刪除時,您需要使用原子[更新](https://docs.datastax.com/en/cql/3.1/cql/cql_reference/batch_r.html) 'BEGIN UNLOGGED BATCH INSERT INTO users_by_username VALUES(...); INSERT INTO users_by_email VALUES(...); APPLY BATCH;' –

+0

謝謝。這將是有益的 – Gnana