SlideShare a Scribd company logo
NoSQL Data Modeling 101
Tzach Livyatan
Content
■ Basic Data Modeling
● CQL
● Partition Key
● Clustering Key
■ Materialized Views
2
3
NoSQL Vs. Relational
Application
Data Model (Schema)
Model (Schema)
Application Data
Relational
NoSQL
➔ Cluster
◆ Keyspace
● Table
● Partition
● Row
○ Column - name / value pair
4
Data Modeling Terminology
What is CQL
■ Cassandra Query Language
■ Similar to SQL (Structured Query Language)
■ Data Definition (DDL)
● CREATE / DELETE / ALTER Keyspace
● CREATE / DELETE / ALTER Table
■ Data Manipulation (DML)
● SELECT
● INSERT
● UPDATE
● DELETE
● BATCH
5
Keyspace
A top-level object that controls the replication per DC.
Contain tables, index, materialized views and user-defined types.
CREATE KEYSPACE Excalibur
WITH replication = {'class': 'NetworkTopologyStrategy', 'DC1'
: 1, 'DC2' : 3}
AND durable_writes = true;
6
Keyspace Example
CREATE KEYSPACE mykeyspace WITH
replication = {'class':
'NetworkTopologyStrategy',
'AWS_US_EAST_1' : 3} AND
durable_writes = true;
USE mykeyspace;
7
Common Data Types
■ ASCII
■ BIGINT
■ BLOB
■ BOOLEAN
■ COUNTER
■ DATE
■ DECIMAL
■ DOUBLE
■ DURATION
■ FLOAT
■ INET
8
■ INT
■ SMALLINT
■ TEXT
■ TIME
■ TIMESTAMP
■ TIMEUUID
■ TINYINT
■ UUID
■ VARCHAR
■ VARINT
* https://docs.scylladb.com/getting-started/types/
Collections
■ Sets
■ Lists
■ Maps
■ UDT
9
Partition Key
10
11
Key / Value Example
SELECT pet_chip_id,owner,pet_name FROM pet_owner;
pet_chip_id owner pet_name
80d39c78-9dc0-11eb-a8b3-
0242ac130003 642adfee-6ad9-... Buddy
80d39c78-9dc0-11eb-a8b3-
0242ac130003 642adfee-6ad9-... Rocky
80d39c78-9dc0-11eb-a8b3-
0242ac130003 642adfee-6ad9-... Cat
... ... ...
Key / Value Example
CREATE TABLE IF NOT EXISTS pet_owner (
pet_chip_id uuid,
owner uuid,
pet_name text,
PRIMARY KEY (pet_chip_id)
);
Partition Key
pet_chip_id owner pet_name
80d39c78-9dc0-11eb-a8b3-
0242ac130003 642adfee-6ad9-... Buddy
80d39c78-9dc0-11eb-a8b3-
0242ac130003 642adfee-6ad9-... Rocky
80d39c78-9dc0-11eb-a8b3-
0242ac130003 642adfee-6ad9-... Cat
... ... ...
12
13
INSERT INTO pet_owner(pet_chip_id,owner,pet_name) VALUES (a2a60505-3e17-4ad4-8e1a-
f11139caa1cc, 642adfee-6ad9-4ca5-aa32-a72e506b8ad8, 'Buddy');
INSERT INTO pet_owner(pet_chip_id,owner,pet_name) VALUES (80d39c78-9dc0-11eb-a8b3-
0242ac130003, 642adfee-6ad9-4ca5-aa32-a72e506b8ad8, 'Rocky');
INSERT INTO pet_owner(pet_chip_id,owner,pet_name) VALUES (92cf4f94-9dc0-11eb-a8b3-
0242ac130003, b4a63c18-9dc0-11eb-a8b3-0242ac130003, 'Rin Tin Tin');
SELECT * FROM pet_owner;
SELECT * FROM pet_owner WHERE pet_chip_id = 80d39c78-9dc0-11eb-a8b3-0242ac130003;
SELECT * FROM pet_owner WHERE pet_name = 'Rocky'; (?)
Key / Value Example
14
UPDATE pet_owner SET pet_name = 'Cat' WHERE pet_chip_id = 92cf4f94-9dc0-11eb-
a8b3-0242ac130003;
DELETE FROM pet_owner WHERE pet_chip_id = 80d39c78-9dc0-11eb-a8b3-0242ac130003;
SELECT * FROM pet_owner;
Key / Value Example
Key / Value Example
15
Choosing a Partition Key
■ High Cardinality
■ Even Distribution
Avoid
■ Low Cardinality
■ Hot Partition
■ Large Partition
16
https://www.codedrome.com/zipfs-law-in-python/
Choosing a Partition Key
17
■ User Name
■ User ID
■ User ID + Time
■ Sensor ID
■ Sensor ID + Time
■ Customer
■ State
■ Age
■ Favorite NBA Team
■ Team Angel or Team Spike
https://commons.wikimedia.org/
Query:
SELECT * from heartrate_v10 WHERE
pet_chip_id = 80d39c78-9dc0-11eb-a8b3-0242ac130003 LIMIT 1;
SELECT * from heartrate_v10 WHERE
pet_chip_id = 80d39c78-9dc0-11eb-a8b3-0242ac130003 AND
time >= '2021-05-01 01:00+0000' AND
time < '2021-05-01 01:03+0000';
18
https://gist.github.com/tzach/7486f1a0cc904c52f4514f20f14d2a97
Wide Partition Example
Wide Partition Example
CREATE TABLE heartrate_v10 (
pet_chip_id uuid,
owner uuid,
time timestamp,
heart_rate int,
PRIMARY KEY (pet_chip_id, time)
);
pet_chip_id time heart_rate
80d39c78-9dc0-11eb-a8b3-0242ac130003 2021-05-01 01:00:00.000000+0000 120
80d39c78-9dc0-11eb-a8b3-0242ac130003 2021-05-01 01:01:00.000000+0000 121
80d39c78-9dc0-11eb-a8b3-0242ac130003 2021-05-01 01:02:00.000000+0000 120
Partition Key Clustering Key
19
20
Large
Partition?
Wide Partition Example
Choosing a Clustering Key
21
■ Allow useful range queries
■ Allow useful LIMIT
https://commons.wikimedia.org/
22
SELECT * from heartrate_v10 WHERE
pet_chip_id = 80d39c78-9dc0-11eb-a8b3-0242ac130003 LIMIT 1;
CREATE TABLE heartrate_v5 (
pet_chip_id uuid,
time timestamp,
heart_rate int,
PRIMARY KEY (pet_chip_id, time)
) WITH CLUSTERING ORDER BY (time DESC);
Partition Key Clustering Key
23
CREATE TABLE heartrate_v6 (
pet_chip_id uuid,
date text,
time timestamp,
heart_rate int,
PRIMARY KEY ((pet_chip_id, date), time));
Partition Key Clustering Key
Too Wide Partition ?
Materialized Views
Example - Query by Owner
SELECT * FROM heartrate_v10 WHERE pet_chip_id = a2a60505-3e17-4ad4-8e1a-
f11139caa1cc;
SELECT * FROM heartrate_v10 WHERE owner = 642adfee-6ad9-4ca5-aa32-
a72e506b8ad8;
SELECT * FROM heartrate_v10 WHERE owner = 642adfee-6ad9-4ca5-aa32-
a72e506b8ad8 ALLOW FILTERING;
25
https://gist.github.com/tzach/4b9dadbc6e8a9c50369da05631c5e13e
Try
TRACING ON;
TRACING OFF;
Solution - Materialized Views
CREATE TABLE heartrate_v10 (
pet_chip_id uuid, owner uuid, time timestamp, heart_rate int,
PRIMARY KEY (pet_chip_id, time)
);
SELECT * FROM heartrate_by_owner WHERE owner = 642adfee-6ad9-4ca5-aa32-
a72e506b8ad8;
CREATE MATERIALIZED VIEW heartrate_by_owner AS
SELECT * FROM heartrate_v10
WHERE owner IS NOT NULL AND pet_chip_id IS NOT NULL AND time IS NOT NULL
PRIMARY KEY(owner, pet_chip_id, time);
DROP MATERIALIZED VIEW heartrate_by_owner;
ALTER MATERIALIZED VIEW heartrate_by_owner [WITH table_options];
https://docs.scylladb.com/getting-started/mv/ 26
Example
27
pet_chip_id time owner heart_rate
80d39c78-9dc0-11eb-a8b3-
0242ac130003 2021-05-01 01:00:00.000000+0000
642adfee-6ad9-4ca5-aa32-
a72e506b8ad8 120
80d39c78-9dc0-11eb-a8b3-
0242ac130003 2021-05-01 01:01:00.000000+0000
642adfee-6ad9-4ca5-aa32-
a72e506b8ad8 121
80d39c78-9dc0-11eb-a8b3-
0242ac130003 2021-05-01 01:02:00.000000+0000
642adfee-6ad9-4ca5-aa32-
a72e506b8ad8 120
owner pet_chip_id time heart_rate
642adfee-6ad9-4ca5-aa32-
a72e506b8ad8
80d39c78-9dc0-11eb-a8b3-
0242ac130003 2021-05-01 01:00:00.000000+0000 120
642adfee-6ad9-4ca5-aa32-
a72e506b8ad8
80d39c78-9dc0-11eb-a8b3-
0242ac130003 2021-05-01 01:01:00.000000+0000 121
642adfee-6ad9-4ca5-aa32-
a72e506b8ad8
80d39c78-9dc0-11eb-a8b3-
0242ac130003 2021-05-01 01:02:00.000000+0000 120
Base Table
View
28
1. INSERT INTO heartrate
(pet_chip_id,
Owner,
Time,
heart_rate)
VALUES (..);
2. INSERT INTO
heartrate
Base replica
View replica
Coordinator
3. INSERT INTO
heartrate_by_owner
MV - Write Path
29
MV - Read Path
2.
SELECT * FROM
heartrate_by_owner
WHERE owner = ‘642a..’;
Base replica
View replica
Coordinator
1.
SELECT * FROM
heartrate_by_owner
WHERE owner = ‘642a..’;
30
http://localhost:3000/d/overview-2019-1/overview 31
Keep in touch!
Tzach Livyatan
ScyllaDB
tzach@scylladb.com
@tzachL

More Related Content

PPTX
Top NoSQL Data Modeling Mistakes
PPTX
NoSQL Data Modeling Foundations — Introducing Concepts & Principles
PPTX
Modeling Data and Queries for Wide Column NoSQL
PDF
RocksDB Performance and Reliability Practices
PPTX
7 Reasons Not to Put an External Cache in Front of Your Database.pptx
PDF
Wide Column Store NoSQL vs SQL Data Modeling
PDF
Tech Talk: RocksDB Slides by Dhruba Borthakur & Haobo Xu of Facebook
PPTX
Top NoSQL Data Modeling Mistakes
NoSQL Data Modeling Foundations — Introducing Concepts & Principles
Modeling Data and Queries for Wide Column NoSQL
RocksDB Performance and Reliability Practices
7 Reasons Not to Put an External Cache in Front of Your Database.pptx
Wide Column Store NoSQL vs SQL Data Modeling
Tech Talk: RocksDB Slides by Dhruba Borthakur & Haobo Xu of Facebook

What's hot (20)

PDF
Solving PostgreSQL wicked problems
PDF
Ceph Object Storage Performance Secrets and Ceph Data Lake Solution
PDF
Log Structured Merge Tree
PDF
Apache Cassandra - wprowadzenie do architektury, modelowania i narzędzi
PDF
How Discord Migrated Trillions of Messages from Cassandra to ScyllaDB
PPTX
Processing Large Data with Apache Spark -- HasGeek
PDF
MySQL/MariaDB Proxy Software Test
PDF
MySQL5.7 GA の Multi-threaded slave
PDF
Oracle db performance tuning
PPTX
MySQL_MariaDB-성능개선-202201.pptx
PDF
Physical Memory Management.pdf
PDF
Seastore: Next Generation Backing Store for Ceph
PDF
MySQL Cluster: El ‘qué’ y el ‘cómo’.
PPTX
How to be Successful with Scylla
PPTX
Redis Reliability, Performance & Innovation
PDF
MySQLアーキテクチャ図解講座
PPTX
Query logging with proxysql
PDF
A Tale of Three Apache Spark APIs: RDDs, DataFrames, and Datasets with Jules ...
PPTX
Qlik Replicateのファイルチャネルの利用
PDF
AWS Black Belt Techシリーズ Amazon Redshift
Solving PostgreSQL wicked problems
Ceph Object Storage Performance Secrets and Ceph Data Lake Solution
Log Structured Merge Tree
Apache Cassandra - wprowadzenie do architektury, modelowania i narzędzi
How Discord Migrated Trillions of Messages from Cassandra to ScyllaDB
Processing Large Data with Apache Spark -- HasGeek
MySQL/MariaDB Proxy Software Test
MySQL5.7 GA の Multi-threaded slave
Oracle db performance tuning
MySQL_MariaDB-성능개선-202201.pptx
Physical Memory Management.pdf
Seastore: Next Generation Backing Store for Ceph
MySQL Cluster: El ‘qué’ y el ‘cómo’.
How to be Successful with Scylla
Redis Reliability, Performance & Innovation
MySQLアーキテクチャ図解講座
Query logging with proxysql
A Tale of Three Apache Spark APIs: RDDs, DataFrames, and Datasets with Jules ...
Qlik Replicateのファイルチャネルの利用
AWS Black Belt Techシリーズ Amazon Redshift
Ad

Similar to NoSQL Data Modeling 101 (20)

PDF
SequoiaDB Distributed Relational Database
PPTX
Using Spark to Load Oracle Data into Cassandra
PPTX
Using Spark to Load Oracle Data into Cassandra (Jim Hatcher, IHS Markit) | C*...
PPTX
Training on Microsoft SQL Server(older version).pptx
PPTX
OpenWorld Sep14 12c for_developers
PPT
Wait Events 10g
PDF
[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스
PDF
Cassandra Summit 2013 Keynote
PPTX
DataStax: An Introduction to DataStax Enterprise Search
PPTX
Cassandra Summit 2015: Intro to DSE Search
PPTX
DNN Database Tips & Tricks
PPT
ash_feel_the_power_kyle_hailey_8951122.ppt
PDF
Proxysql sharding
PPTX
PDF
PostgreSQL Open SV 2018
PPTX
New SQL features in latest MySQL releases
PDF
Time series with Apache Cassandra - Long version
PDF
CCM Escape Case Study - SkySQL Paris Meetup 17.12.2013
PDF
Tech Talk: Best Practices for Data Modeling
PDF
Understanding Optimizer-Statistics-for-Developers
SequoiaDB Distributed Relational Database
Using Spark to Load Oracle Data into Cassandra
Using Spark to Load Oracle Data into Cassandra (Jim Hatcher, IHS Markit) | C*...
Training on Microsoft SQL Server(older version).pptx
OpenWorld Sep14 12c for_developers
Wait Events 10g
[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스
Cassandra Summit 2013 Keynote
DataStax: An Introduction to DataStax Enterprise Search
Cassandra Summit 2015: Intro to DSE Search
DNN Database Tips & Tricks
ash_feel_the_power_kyle_hailey_8951122.ppt
Proxysql sharding
PostgreSQL Open SV 2018
New SQL features in latest MySQL releases
Time series with Apache Cassandra - Long version
CCM Escape Case Study - SkySQL Paris Meetup 17.12.2013
Tech Talk: Best Practices for Data Modeling
Understanding Optimizer-Statistics-for-Developers
Ad

More from ScyllaDB (20)

PDF
Understanding The True Cost of DynamoDB Webinar
PDF
Database Benchmarking for Performance Masterclass: Session 2 - Data Modeling ...
PDF
Database Benchmarking for Performance Masterclass: Session 1 - Benchmarking F...
PDF
New Ways to Reduce Database Costs with ScyllaDB
PDF
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
PDF
Powering a Billion Dreams: Scaling Meesho’s E-commerce Revolution with Scylla...
PDF
Leading a High-Stakes Database Migration
PDF
Achieving Extreme Scale with ScyllaDB: Tips & Tradeoffs
PDF
Securely Serving Millions of Boot Artifacts a Day by João Pedro Lima & Matt ...
PDF
How Agoda Scaled 50x Throughput with ScyllaDB by Worakarn Isaratham
PDF
How Yieldmo Cut Database Costs and Cloud Dependencies Fast by Todd Coleman
PDF
ScyllaDB: 10 Years and Beyond by Dor Laor
PDF
Reduce Your Cloud Spend with ScyllaDB by Tzach Livyatan
PDF
Migrating 50TB Data From a Home-Grown Database to ScyllaDB, Fast by Terence Liu
PDF
Vector Search with ScyllaDB by Szymon Wasik
PDF
Workload Prioritization: How to Balance Multiple Workloads in a Cluster by Fe...
PDF
Two Leading Approaches to Data Virtualization, and Which Scales Better? by Da...
PDF
Scaling a Beast: Lessons from 400x Growth in a High-Stakes Financial System b...
PDF
Object Storage in ScyllaDB by Ran Regev, ScyllaDB
PDF
Lessons Learned from Building a Serverless Notifications System by Srushith R...
Understanding The True Cost of DynamoDB Webinar
Database Benchmarking for Performance Masterclass: Session 2 - Data Modeling ...
Database Benchmarking for Performance Masterclass: Session 1 - Benchmarking F...
New Ways to Reduce Database Costs with ScyllaDB
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Powering a Billion Dreams: Scaling Meesho’s E-commerce Revolution with Scylla...
Leading a High-Stakes Database Migration
Achieving Extreme Scale with ScyllaDB: Tips & Tradeoffs
Securely Serving Millions of Boot Artifacts a Day by João Pedro Lima & Matt ...
How Agoda Scaled 50x Throughput with ScyllaDB by Worakarn Isaratham
How Yieldmo Cut Database Costs and Cloud Dependencies Fast by Todd Coleman
ScyllaDB: 10 Years and Beyond by Dor Laor
Reduce Your Cloud Spend with ScyllaDB by Tzach Livyatan
Migrating 50TB Data From a Home-Grown Database to ScyllaDB, Fast by Terence Liu
Vector Search with ScyllaDB by Szymon Wasik
Workload Prioritization: How to Balance Multiple Workloads in a Cluster by Fe...
Two Leading Approaches to Data Virtualization, and Which Scales Better? by Da...
Scaling a Beast: Lessons from 400x Growth in a High-Stakes Financial System b...
Object Storage in ScyllaDB by Ran Regev, ScyllaDB
Lessons Learned from Building a Serverless Notifications System by Srushith R...

Recently uploaded (20)

PPTX
MYSQL Presentation for SQL database connectivity
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Big Data Technologies - Introduction.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Cloud computing and distributed systems.
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
Spectroscopy.pptx food analysis technology
PDF
Encapsulation theory and applications.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Understanding_Digital_Forensics_Presentation.pptx
MYSQL Presentation for SQL database connectivity
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
The AUB Centre for AI in Media Proposal.docx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Big Data Technologies - Introduction.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Network Security Unit 5.pdf for BCA BBA.
Cloud computing and distributed systems.
Per capita expenditure prediction using model stacking based on satellite ima...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Spectroscopy.pptx food analysis technology
Encapsulation theory and applications.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Understanding_Digital_Forensics_Presentation.pptx

NoSQL Data Modeling 101

  • 1. NoSQL Data Modeling 101 Tzach Livyatan
  • 2. Content ■ Basic Data Modeling ● CQL ● Partition Key ● Clustering Key ■ Materialized Views 2
  • 3. 3 NoSQL Vs. Relational Application Data Model (Schema) Model (Schema) Application Data Relational NoSQL
  • 4. ➔ Cluster ◆ Keyspace ● Table ● Partition ● Row ○ Column - name / value pair 4 Data Modeling Terminology
  • 5. What is CQL ■ Cassandra Query Language ■ Similar to SQL (Structured Query Language) ■ Data Definition (DDL) ● CREATE / DELETE / ALTER Keyspace ● CREATE / DELETE / ALTER Table ■ Data Manipulation (DML) ● SELECT ● INSERT ● UPDATE ● DELETE ● BATCH 5
  • 6. Keyspace A top-level object that controls the replication per DC. Contain tables, index, materialized views and user-defined types. CREATE KEYSPACE Excalibur WITH replication = {'class': 'NetworkTopologyStrategy', 'DC1' : 1, 'DC2' : 3} AND durable_writes = true; 6
  • 7. Keyspace Example CREATE KEYSPACE mykeyspace WITH replication = {'class': 'NetworkTopologyStrategy', 'AWS_US_EAST_1' : 3} AND durable_writes = true; USE mykeyspace; 7
  • 8. Common Data Types ■ ASCII ■ BIGINT ■ BLOB ■ BOOLEAN ■ COUNTER ■ DATE ■ DECIMAL ■ DOUBLE ■ DURATION ■ FLOAT ■ INET 8 ■ INT ■ SMALLINT ■ TEXT ■ TIME ■ TIMESTAMP ■ TIMEUUID ■ TINYINT ■ UUID ■ VARCHAR ■ VARINT * https://docs.scylladb.com/getting-started/types/
  • 11. 11 Key / Value Example SELECT pet_chip_id,owner,pet_name FROM pet_owner; pet_chip_id owner pet_name 80d39c78-9dc0-11eb-a8b3- 0242ac130003 642adfee-6ad9-... Buddy 80d39c78-9dc0-11eb-a8b3- 0242ac130003 642adfee-6ad9-... Rocky 80d39c78-9dc0-11eb-a8b3- 0242ac130003 642adfee-6ad9-... Cat ... ... ...
  • 12. Key / Value Example CREATE TABLE IF NOT EXISTS pet_owner ( pet_chip_id uuid, owner uuid, pet_name text, PRIMARY KEY (pet_chip_id) ); Partition Key pet_chip_id owner pet_name 80d39c78-9dc0-11eb-a8b3- 0242ac130003 642adfee-6ad9-... Buddy 80d39c78-9dc0-11eb-a8b3- 0242ac130003 642adfee-6ad9-... Rocky 80d39c78-9dc0-11eb-a8b3- 0242ac130003 642adfee-6ad9-... Cat ... ... ... 12
  • 13. 13 INSERT INTO pet_owner(pet_chip_id,owner,pet_name) VALUES (a2a60505-3e17-4ad4-8e1a- f11139caa1cc, 642adfee-6ad9-4ca5-aa32-a72e506b8ad8, 'Buddy'); INSERT INTO pet_owner(pet_chip_id,owner,pet_name) VALUES (80d39c78-9dc0-11eb-a8b3- 0242ac130003, 642adfee-6ad9-4ca5-aa32-a72e506b8ad8, 'Rocky'); INSERT INTO pet_owner(pet_chip_id,owner,pet_name) VALUES (92cf4f94-9dc0-11eb-a8b3- 0242ac130003, b4a63c18-9dc0-11eb-a8b3-0242ac130003, 'Rin Tin Tin'); SELECT * FROM pet_owner; SELECT * FROM pet_owner WHERE pet_chip_id = 80d39c78-9dc0-11eb-a8b3-0242ac130003; SELECT * FROM pet_owner WHERE pet_name = 'Rocky'; (?) Key / Value Example
  • 14. 14 UPDATE pet_owner SET pet_name = 'Cat' WHERE pet_chip_id = 92cf4f94-9dc0-11eb- a8b3-0242ac130003; DELETE FROM pet_owner WHERE pet_chip_id = 80d39c78-9dc0-11eb-a8b3-0242ac130003; SELECT * FROM pet_owner; Key / Value Example
  • 15. Key / Value Example 15
  • 16. Choosing a Partition Key ■ High Cardinality ■ Even Distribution Avoid ■ Low Cardinality ■ Hot Partition ■ Large Partition 16 https://www.codedrome.com/zipfs-law-in-python/
  • 17. Choosing a Partition Key 17 ■ User Name ■ User ID ■ User ID + Time ■ Sensor ID ■ Sensor ID + Time ■ Customer ■ State ■ Age ■ Favorite NBA Team ■ Team Angel or Team Spike https://commons.wikimedia.org/
  • 18. Query: SELECT * from heartrate_v10 WHERE pet_chip_id = 80d39c78-9dc0-11eb-a8b3-0242ac130003 LIMIT 1; SELECT * from heartrate_v10 WHERE pet_chip_id = 80d39c78-9dc0-11eb-a8b3-0242ac130003 AND time >= '2021-05-01 01:00+0000' AND time < '2021-05-01 01:03+0000'; 18 https://gist.github.com/tzach/7486f1a0cc904c52f4514f20f14d2a97 Wide Partition Example
  • 19. Wide Partition Example CREATE TABLE heartrate_v10 ( pet_chip_id uuid, owner uuid, time timestamp, heart_rate int, PRIMARY KEY (pet_chip_id, time) ); pet_chip_id time heart_rate 80d39c78-9dc0-11eb-a8b3-0242ac130003 2021-05-01 01:00:00.000000+0000 120 80d39c78-9dc0-11eb-a8b3-0242ac130003 2021-05-01 01:01:00.000000+0000 121 80d39c78-9dc0-11eb-a8b3-0242ac130003 2021-05-01 01:02:00.000000+0000 120 Partition Key Clustering Key 19
  • 21. Choosing a Clustering Key 21 ■ Allow useful range queries ■ Allow useful LIMIT https://commons.wikimedia.org/
  • 22. 22 SELECT * from heartrate_v10 WHERE pet_chip_id = 80d39c78-9dc0-11eb-a8b3-0242ac130003 LIMIT 1; CREATE TABLE heartrate_v5 ( pet_chip_id uuid, time timestamp, heart_rate int, PRIMARY KEY (pet_chip_id, time) ) WITH CLUSTERING ORDER BY (time DESC); Partition Key Clustering Key
  • 23. 23 CREATE TABLE heartrate_v6 ( pet_chip_id uuid, date text, time timestamp, heart_rate int, PRIMARY KEY ((pet_chip_id, date), time)); Partition Key Clustering Key Too Wide Partition ?
  • 25. Example - Query by Owner SELECT * FROM heartrate_v10 WHERE pet_chip_id = a2a60505-3e17-4ad4-8e1a- f11139caa1cc; SELECT * FROM heartrate_v10 WHERE owner = 642adfee-6ad9-4ca5-aa32- a72e506b8ad8; SELECT * FROM heartrate_v10 WHERE owner = 642adfee-6ad9-4ca5-aa32- a72e506b8ad8 ALLOW FILTERING; 25 https://gist.github.com/tzach/4b9dadbc6e8a9c50369da05631c5e13e Try TRACING ON; TRACING OFF;
  • 26. Solution - Materialized Views CREATE TABLE heartrate_v10 ( pet_chip_id uuid, owner uuid, time timestamp, heart_rate int, PRIMARY KEY (pet_chip_id, time) ); SELECT * FROM heartrate_by_owner WHERE owner = 642adfee-6ad9-4ca5-aa32- a72e506b8ad8; CREATE MATERIALIZED VIEW heartrate_by_owner AS SELECT * FROM heartrate_v10 WHERE owner IS NOT NULL AND pet_chip_id IS NOT NULL AND time IS NOT NULL PRIMARY KEY(owner, pet_chip_id, time); DROP MATERIALIZED VIEW heartrate_by_owner; ALTER MATERIALIZED VIEW heartrate_by_owner [WITH table_options]; https://docs.scylladb.com/getting-started/mv/ 26
  • 27. Example 27 pet_chip_id time owner heart_rate 80d39c78-9dc0-11eb-a8b3- 0242ac130003 2021-05-01 01:00:00.000000+0000 642adfee-6ad9-4ca5-aa32- a72e506b8ad8 120 80d39c78-9dc0-11eb-a8b3- 0242ac130003 2021-05-01 01:01:00.000000+0000 642adfee-6ad9-4ca5-aa32- a72e506b8ad8 121 80d39c78-9dc0-11eb-a8b3- 0242ac130003 2021-05-01 01:02:00.000000+0000 642adfee-6ad9-4ca5-aa32- a72e506b8ad8 120 owner pet_chip_id time heart_rate 642adfee-6ad9-4ca5-aa32- a72e506b8ad8 80d39c78-9dc0-11eb-a8b3- 0242ac130003 2021-05-01 01:00:00.000000+0000 120 642adfee-6ad9-4ca5-aa32- a72e506b8ad8 80d39c78-9dc0-11eb-a8b3- 0242ac130003 2021-05-01 01:01:00.000000+0000 121 642adfee-6ad9-4ca5-aa32- a72e506b8ad8 80d39c78-9dc0-11eb-a8b3- 0242ac130003 2021-05-01 01:02:00.000000+0000 120 Base Table View
  • 28. 28
  • 29. 1. INSERT INTO heartrate (pet_chip_id, Owner, Time, heart_rate) VALUES (..); 2. INSERT INTO heartrate Base replica View replica Coordinator 3. INSERT INTO heartrate_by_owner MV - Write Path 29
  • 30. MV - Read Path 2. SELECT * FROM heartrate_by_owner WHERE owner = ‘642a..’; Base replica View replica Coordinator 1. SELECT * FROM heartrate_by_owner WHERE owner = ‘642a..’; 30
  • 32. Keep in touch! Tzach Livyatan ScyllaDB tzach@scylladb.com @tzachL

Editor's Notes

  • #3: Tzach - VP of Product Session is available in Scylla U as a course
  • #5: Let’s go over some important terms: A Cluster is a collection of nodes that Scylla uses to store the data. The nodes are logically distributed like a ring. A minimum cluster typically consists of at least three nodes. Data is automatically replicated across the cluster, depending on the Replication Factor. This cluster is often referred to as a ring architecture, based on a hash ring — the way the cluster knows how to distribute data across the different nodes. A Keyspace is a top-level container that stores tables with attributes that define how data is replicated on nodes. It defines a number of options that apply to all the tables it contains, the most important of which is the replication strategy used by the Keyspace. A keyspace is comparable to the concept of a database Schema in the relational world. Since the keyspace defines the replication factor of all underlying tables, if we have tables that require different replication factors we would store them in different keyspaces. A Table is how Scylla stores data and can be thought of as a set of rows and columns. A Partition is a collection of sorted rows, identified by a unique primary key. More on primary keys later on in this session. Each partition is stored on a node and replicated across nodes. A Row in Scylla is a unit that stores data. Each row has a primary key that uniquely identifies it in a Table. Each row stores data as pairs of column names and values. In case a Clustering Key is defined, the rows in the partition will be sorted accordingly. More on that later on.
  • #6: CQL is a query language that is used to interface with Scylla. It allows us to perform basic functions such as insert, update, select, delete, create, and so on. CQL is in some ways similar to SQL however there are some differences.
  • #7: replication The replication strategy and options to use for the keyspace (see details below). durable_writes Whether to use the commit log for updates on this keyspace (disable this option at your own risk!).
  • #8: Share a terminal > ty-share
  • #9: Before we create a table, we need to know: Data types Keys Table
  • #10: Collections are used to describe a group of items connected to single key -> helps with simplifying data modeling Remember to use appropriate collection per use case Keep collection small to prevent high latency during querying the data Sets are ordered alphabetically or based on the natural sorting method of the type Examples: multiple email addresses or phone numbers per user Lists are ordered objects based on user’s definition Maps is a name and a pair of typed values, very helpful with a sequential events logging Summary: Collections helps users with organizing their data Collections should be used in adequate cases, due to performance impact
  • #11: A Partition Key is one or more columns that are responsible for data distribution across the nodes. It determines in which node to store a given row. Partition Key is a must on every table. In the example below the Partition Key is the ID column. A consistent hash function, also known as the partitioner, is used to determine to which nodes data is written. Scylla transparently partitions data and distributes it to the cluster. Data is replicated across the cluster. A Scylla cluster is visualized as a ring, where each node is responsible for a range of tokens and each value is attached to a token using a partition key
  • #13: Allow fast query for pet, and just for pets! PRIMARY KEY = Partition + Clustering Key
  • #20: https://gist.github.com/tzach/7486f1a0cc904c52f4514f20f14d2a97
  • #21: Why is large partition a problem? Is it a problem? Large may lead to got Index implementation (no longer an issue in Scylla)
  • #23: By default, sorting is based on the natural (ASC) order of the clustering columns. What happens if we want to reverse the order? What if our query is to find the heart rate by pet_chip_id and time, but that we want to look at the ten most recent records.
  • #24: By default, sorting is based on the natural (ASC) order of the clustering columns. What happens if we want to reverse the order? What if our query is to find the heart rate by pet_chip_id and time, but that we want to look at the ten most recent records.
  • #26: Now that we see that we are able to query each individual pet, what about their owners? Let’s try Scylla will output an error message, saying that the query might hurt the performance, if you want to query anyway you should use ALLOW FILTERING Works Scylla raises an error since we are querying a regular column which is not indexed and it will hurt the performance because scylla will do a FULL SCAN on the partition, meaning that will read the entire partition to filter it after. Use TRACING to see how much the performance will be affected One way to solve this problem: create a table using owner id and another for pet id and on the application we do dual writes. The problem here is that we now need to make sure that both table are synchronized.
  • #27: MVs - Is a new table that it’s updated automatically by the base table Show syntax
  • #28: But if we create a view and make owner as the partition key and then we can query the view by it’s partition key (owner)
  • #30: Everytime that a insert is received by the Coordinator, scylla will insert into the base table and updated the mutations on the relevant updates on MVs replicas They are synchronously and works as any other table except Scylla will reject writes done directly on the materialized views No magic, thats a tradeoff between read latency and disk space Every MV that you create, you will need more space for its creation
  • #31: When querying the MV specifically - scylla will query the MV - low latency
  • #32: See Shlomi Session after me of the second track