SlideShare une entreprise Scribd logo
1  sur  39
Télécharger pour lire hors ligne
Three things you need
to know about data modeling
Matthew Revell
matthew@couchbase.com, @matthewrevell
Lead Developer Advocate, Couchbase
1
We’re still learning
Data modeling books
©2014 Couchbase, Inc. 3
Three query methods
Application layer computation Off-load computation
Predictable queries Key-value: pre-computed answers Views
Ad-hoc queries N1QL and key-value with manual
indexes
N1QL and views
©2014 Couchbase, Inc. 4
Key-value
Key-value
Pre-compute answers asynchronously
Store object state
Choose when to embed data and when to refer to it
Design your keys well
©2014 Couchbase, Inc. 6
Pre-computed answers
We’re used to asking questions
©2014 Couchbase, Inc. 8
Key-value look-ups give us a library of answers
©2014 Couchbase, Inc. 9
Answer-oriented databases
©2014 Couchbase, Inc. 10
http://martinfowler.com/bliki/AggregateOrientedDatabase.html
Airline bookings
©2014 Couchbase, Inc. 11
Embed or refer?
How much should we denormalize?
An e-commerce order
©2014 Couchbase, Inc. 13
The same order in a document
©2014 Couchbase, Inc. 14
Embedded vs. referred data
©2014 Couchbase, Inc. 15
You should embed data when:
 Speed trumps all else
 Slow moving data
 No duplication
 Application layer can keep multiple copies of same data in sync
When to embed data
©2014 Couchbase, Inc. 16
You should refer to data when:
 Consistency is a priority
 The data has large growth potential
When to refer to data
©2014 Couchbase, Inc. 17
Key design
Key design is as important as document design.
There are three broad types of keys:
 Human readable/deterministic: e.g., an email address
 Computer generated/random: e.g., a UUID
 Compound: e.g., a UUID with a deterministic portion
Three ways to build a key
©2014 Couchbase, Inc. 19
Human readable/deterministic key
©2014 Couchbase, Inc. 20
public class user {
private String name;
private String email;
private String streetAddress;
private String city;
private String country;
private String postCode;
private String telephone;
private Array orders;
private Array productsViewed;
}
{
"name": "Matthew Revell",
"address": "11-21 Paul Street",
"city": "London",
"postCode": "EC2A 4JU",
"telephone": "44-20-3837-9130",
"orders": [ 1, 9, 698, 32 ],
“productsViewed”: [8, 33, 99, 100]
}
Key: matthew@couchbase.com
Random/computer-generated key
©2014 Couchbase, Inc. 21
{
"name": "Matthew Revell",
"email": "matthew@couchbase.com",
"address": "11-21 Paul Street",
"city": "London",
"postCode": "EC2A 4JU",
"telephone": "44-20-3837-9130",
"orders": [ 1, 9, 698, 32 ],
“productsViewed”: [8, 33, 99, 100]
}
Key: 1001
Using counters to generate keys
©2014 Couchbase, Inc. 22
Application
user_id = incr(“counter_key")
add(user_id, data)
Creating a new user
add(email_address, user_id)
Application
key = get("matthew@couchbase.com")
get(key)
Finding a user by email address
Multiple look-up documents
©2014 Couchbase, Inc. 23
u::count
1001
u::1001
{ "name": “Matthew Revell",
"facebook_id": 16172910,
"email": “matthew@couchbase.com”,
“password”: ab02d#Jf02K
"created_at": "5/1/2012 2:30am",
“facebook_access_token”: xox0v2dje20,
“twitter_access_token”: 20jffieieaaixixj }
fb::16172910
1001
nflx::2939202
1001
twtr::2920283830
1001
em::matthew@couchbase.com
1001
em::matthew@understated.co.uk
1001
uname::mrevell
1001
Compound keys
Compound keys are look-up documents with predictable names.
They continue the discussion of embedded versus referred data.
Compound keys: an example
u::1001
{
"name": "Matthew Revell",
"email": "matthew@couchbase.com",
"address": "11-21 Paul Street",
"city": "London",
"postCode": "EC2A 4JU",
"telephone": "44-20-3837-9130",
"orders": [ 1, 9, 698, 32 ],
“productsViewed”: [8, 33, 99, 100]
}
Compound keys: an example
u::1001
{
"name": "Matthew Revell",
"email": "matthew@couchbase.com",
"address": "11-21 Paul Street",
"city": "London",
"postCode": "EC2A 4JU",
"telephone": "44-20-3837-9130",
"orders": [ 1, 9, 698, 32 ]
}
u::1001::productsviewed
{
"productsList": [
8, 33, 99, 100
]
}
Compound keys: an example
u::1001
{
"name": "Matthew Revell",
"email": "matthew@couchbase.com",
"address": "11-21 Paul Street",
"city": "London",
"postCode": "EC2A 4JU",
"telephone": "44-20-3837-9130",
"orders": [ 1, 9, 698, 32 ]
}
u::1001::productsviewed
{
"productsList": [
8, 33, 99, 100
]
}
p::8
{
id": 1,
"name": "T-shirt",
"description": "Red Couchbase shirt",
"quantityInStock": 99,
"image": "tshirt.jpg”
}
Compound keys: an example
u::1001
{
"name": "Matthew Revell",
"email": "matthew@couchbase.com",
"address": "11-21 Paul Street",
"city": "London",
"postCode": "EC2A 4JU",
"telephone": "44-20-3837-9130",
"orders": [ 1, 9, 698, 32 ]
}
u::1001::productsviewed
{
"productsList": [
8, 33, 99, 100
]
}
p::8
{
id": 1,
"name": "T-shirt",
"description": "Red Couchbase shirt",
"quantityInStock": 99
}
p::8::img
“http://someurl.com/tshirt.jpg”
N1QL and Views!
The world beyond key-value
We’re not in Kansas any more
©2014 Couchbase, Inc. 30
N1QL and views
N1QL Views
Ad-hoc querying Predictable queries
Nested JSON in and unnested JSON
out
Number crunching
Large growth clusters Multidimensional and
geospatial queries
©2014 Couchbase, Inc. 31
N1QL
Indexes
Schema types and document types
Keyspaces
©2014 Couchbase, Inc. 32
Indexes
©2014 Couchbase, Inc. 33
• You always need at least one index
• PRIMARY index gives you all of the keys in the bucket
• Secondary indexes enable ad-hoc querying at speed
Indexes
©2014 Couchbase, Inc. 34
Indexes
©2014 Couchbase, Inc. 35
NEW Global Secondary Indexes Views
N1QL query to create a new index
JavaScript map/reduce
Option to run dedicated indexing and
query nodes
Queries always run on database nodes
ForestDB-backed Couchstore-backed
Supports multidimensional and geospatial
queries
Maintaining the schema and working with keyspaces
©2014 Couchbase, Inc. 36
JOINs work on primary keys and secondary keys.
They JOIN across and within keyspaces (for now, that means buckets).
Airlines:
airline_24 ← Key (“primary key”)
{
"id": "24",
"type": "airline",
"name": "American Airlines",
"iata": "AA",
"icao": "AAL",
"callsign": "AMERICAN",
"country": "United States",
"active": "Y"
}
Routes:
route_5966 ← Key
{
"id": "5966",
"type": "route",
"airline": "AA",
"airlineid": "airline_24", ← This is the foreign key
"sourceairport": "MCO",
"destinationairport": "SEA",
"stops": "0",
"equipment": "737",
"schedule": [...
]
}
Offloading computation to N1QL
©2014 Couchbase, Inc. 37
N1QL allows you to choose which elements are returned at the top level.
This is incredibly useful because it reduces the need to write complex
parsing logic:
• Within the application
• In client-side, front-end frameworks (Angular/Backbone etc.)
SELECT a.name, s.flight, s.utc, r.sourceairport, r.destinationairport,
r.equipment FROM `travel-sample` r UNNEST r.schedule s JOIN `travel-
sample` a ON KEYS r.airlineid WHERE r.sourceairport='LHR' AND
r.destinationairport='LAX' AND s.day=2 ORDER BY a.name
Questions!
Thanks!
Matthew Revell
matthew@couchbase.com
@matthewrevell

Contenu connexe

Similaire à Three Things You Need to Know About Document Data Modeling in NoSQL

EDB NoSQL German Webinar 2015
EDB NoSQL German Webinar 2015EDB NoSQL German Webinar 2015
EDB NoSQL German Webinar 2015
EDB
 

Similaire à Three Things You Need to Know About Document Data Modeling in NoSQL (20)

Big Data Day LA 2015 - Introducing N1QL: SQL for Documents by Jeff Morris of ...
Big Data Day LA 2015 - Introducing N1QL: SQL for Documents by Jeff Morris of ...Big Data Day LA 2015 - Introducing N1QL: SQL for Documents by Jeff Morris of ...
Big Data Day LA 2015 - Introducing N1QL: SQL for Documents by Jeff Morris of ...
 
Couchbase 101
Couchbase 101 Couchbase 101
Couchbase 101
 
Couchbase N1QL: Language & Architecture Overview.
Couchbase N1QL: Language & Architecture Overview.Couchbase N1QL: Language & Architecture Overview.
Couchbase N1QL: Language & Architecture Overview.
 
Json data modeling june 2017 - pittsburgh tech fest
Json data modeling   june 2017 - pittsburgh tech festJson data modeling   june 2017 - pittsburgh tech fest
Json data modeling june 2017 - pittsburgh tech fest
 
How Shutl Delivers Even Faster Using Neo4J
How Shutl Delivers Even Faster Using Neo4JHow Shutl Delivers Even Faster Using Neo4J
How Shutl Delivers Even Faster Using Neo4J
 
Slides: Moving from a Relational Model to NoSQL
Slides: Moving from a Relational Model to NoSQLSlides: Moving from a Relational Model to NoSQL
Slides: Moving from a Relational Model to NoSQL
 
Big Data and NoSQL for Database and BI Pros
Big Data and NoSQL for Database and BI ProsBig Data and NoSQL for Database and BI Pros
Big Data and NoSQL for Database and BI Pros
 
Back to the future : SQL 92 for Elasticsearch ? @nosqlmatters Dublin 2014
Back to the future : SQL 92 for Elasticsearch ? @nosqlmatters Dublin 2014Back to the future : SQL 92 for Elasticsearch ? @nosqlmatters Dublin 2014
Back to the future : SQL 92 for Elasticsearch ? @nosqlmatters Dublin 2014
 
QCon 2014 - How Shutl delivers even faster with Neo4j
QCon 2014 - How Shutl delivers even faster with Neo4jQCon 2014 - How Shutl delivers even faster with Neo4j
QCon 2014 - How Shutl delivers even faster with Neo4j
 
Introduction to Neo4j and .Net
Introduction to Neo4j and .NetIntroduction to Neo4j and .Net
Introduction to Neo4j and .Net
 
Introducción a NoSQL
Introducción a NoSQLIntroducción a NoSQL
Introducción a NoSQL
 
Graphs for Enterprise Architects
Graphs for Enterprise ArchitectsGraphs for Enterprise Architects
Graphs for Enterprise Architects
 
Moving from SQL Server to MongoDB
Moving from SQL Server to MongoDBMoving from SQL Server to MongoDB
Moving from SQL Server to MongoDB
 
CDC to the Max!
CDC to the Max!CDC to the Max!
CDC to the Max!
 
GraphTalks Rome - Selecting the right Technology
GraphTalks Rome - Selecting the right TechnologyGraphTalks Rome - Selecting the right Technology
GraphTalks Rome - Selecting the right Technology
 
Advanced search and Top-k queries in Cassandra - Cassandra Summit Europe 2014
Advanced search and Top-k queries in Cassandra - Cassandra Summit Europe 2014Advanced search and Top-k queries in Cassandra - Cassandra Summit Europe 2014
Advanced search and Top-k queries in Cassandra - Cassandra Summit Europe 2014
 
Advanced search and Top-k queries in Cassandra - Cassandra Summit Europe 2014
Advanced search and Top-k queries in Cassandra - Cassandra Summit Europe 2014Advanced search and Top-k queries in Cassandra - Cassandra Summit Europe 2014
Advanced search and Top-k queries in Cassandra - Cassandra Summit Europe 2014
 
Neo4j GraphTalk Helsinki - Introduction and Graph Use Cases
Neo4j GraphTalk Helsinki - Introduction and Graph Use CasesNeo4j GraphTalk Helsinki - Introduction and Graph Use Cases
Neo4j GraphTalk Helsinki - Introduction and Graph Use Cases
 
EDB NoSQL German Webinar 2015
EDB NoSQL German Webinar 2015EDB NoSQL German Webinar 2015
EDB NoSQL German Webinar 2015
 
Migration and Coexistence between Relational and NoSQL Databases by Manuel H...
 Migration and Coexistence between Relational and NoSQL Databases by Manuel H... Migration and Coexistence between Relational and NoSQL Databases by Manuel H...
Migration and Coexistence between Relational and NoSQL Databases by Manuel H...
 

Plus de DATAVERSITY

The Data Trifecta – Privacy, Security & Governance Race from Reactivity to Re...
The Data Trifecta – Privacy, Security & Governance Race from Reactivity to Re...The Data Trifecta – Privacy, Security & Governance Race from Reactivity to Re...
The Data Trifecta – Privacy, Security & Governance Race from Reactivity to Re...
DATAVERSITY
 
Data Strategy Best Practices
Data Strategy Best PracticesData Strategy Best Practices
Data Strategy Best Practices
DATAVERSITY
 

Plus de DATAVERSITY (20)

Architecture, Products, and Total Cost of Ownership of the Leading Machine Le...
Architecture, Products, and Total Cost of Ownership of the Leading Machine Le...Architecture, Products, and Total Cost of Ownership of the Leading Machine Le...
Architecture, Products, and Total Cost of Ownership of the Leading Machine Le...
 
Data at the Speed of Business with Data Mastering and Governance
Data at the Speed of Business with Data Mastering and GovernanceData at the Speed of Business with Data Mastering and Governance
Data at the Speed of Business with Data Mastering and Governance
 
Exploring Levels of Data Literacy
Exploring Levels of Data LiteracyExploring Levels of Data Literacy
Exploring Levels of Data Literacy
 
Building a Data Strategy – Practical Steps for Aligning with Business Goals
Building a Data Strategy – Practical Steps for Aligning with Business GoalsBuilding a Data Strategy – Practical Steps for Aligning with Business Goals
Building a Data Strategy – Practical Steps for Aligning with Business Goals
 
Make Data Work for You
Make Data Work for YouMake Data Work for You
Make Data Work for You
 
Data Catalogs Are the Answer – What is the Question?
Data Catalogs Are the Answer – What is the Question?Data Catalogs Are the Answer – What is the Question?
Data Catalogs Are the Answer – What is the Question?
 
Data Catalogs Are the Answer – What Is the Question?
Data Catalogs Are the Answer – What Is the Question?Data Catalogs Are the Answer – What Is the Question?
Data Catalogs Are the Answer – What Is the Question?
 
Data Modeling Fundamentals
Data Modeling FundamentalsData Modeling Fundamentals
Data Modeling Fundamentals
 
Showing ROI for Your Analytic Project
Showing ROI for Your Analytic ProjectShowing ROI for Your Analytic Project
Showing ROI for Your Analytic Project
 
How a Semantic Layer Makes Data Mesh Work at Scale
How a Semantic Layer Makes  Data Mesh Work at ScaleHow a Semantic Layer Makes  Data Mesh Work at Scale
How a Semantic Layer Makes Data Mesh Work at Scale
 
Is Enterprise Data Literacy Possible?
Is Enterprise Data Literacy Possible?Is Enterprise Data Literacy Possible?
Is Enterprise Data Literacy Possible?
 
The Data Trifecta – Privacy, Security & Governance Race from Reactivity to Re...
The Data Trifecta – Privacy, Security & Governance Race from Reactivity to Re...The Data Trifecta – Privacy, Security & Governance Race from Reactivity to Re...
The Data Trifecta – Privacy, Security & Governance Race from Reactivity to Re...
 
Emerging Trends in Data Architecture – What’s the Next Big Thing?
Emerging Trends in Data Architecture – What’s the Next Big Thing?Emerging Trends in Data Architecture – What’s the Next Big Thing?
Emerging Trends in Data Architecture – What’s the Next Big Thing?
 
Data Governance Trends - A Look Backwards and Forwards
Data Governance Trends - A Look Backwards and ForwardsData Governance Trends - A Look Backwards and Forwards
Data Governance Trends - A Look Backwards and Forwards
 
Data Governance Trends and Best Practices To Implement Today
Data Governance Trends and Best Practices To Implement TodayData Governance Trends and Best Practices To Implement Today
Data Governance Trends and Best Practices To Implement Today
 
2023 Trends in Enterprise Analytics
2023 Trends in Enterprise Analytics2023 Trends in Enterprise Analytics
2023 Trends in Enterprise Analytics
 
Data Strategy Best Practices
Data Strategy Best PracticesData Strategy Best Practices
Data Strategy Best Practices
 
Who Should Own Data Governance – IT or Business?
Who Should Own Data Governance – IT or Business?Who Should Own Data Governance – IT or Business?
Who Should Own Data Governance – IT or Business?
 
Data Management Best Practices
Data Management Best PracticesData Management Best Practices
Data Management Best Practices
 
MLOps – Applying DevOps to Competitive Advantage
MLOps – Applying DevOps to Competitive AdvantageMLOps – Applying DevOps to Competitive Advantage
MLOps – Applying DevOps to Competitive Advantage
 

Dernier

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Dernier (20)

Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 

Three Things You Need to Know About Document Data Modeling in NoSQL

  • 1. Three things you need to know about data modeling Matthew Revell matthew@couchbase.com, @matthewrevell Lead Developer Advocate, Couchbase 1
  • 3. Data modeling books ©2014 Couchbase, Inc. 3
  • 4. Three query methods Application layer computation Off-load computation Predictable queries Key-value: pre-computed answers Views Ad-hoc queries N1QL and key-value with manual indexes N1QL and views ©2014 Couchbase, Inc. 4
  • 6. Key-value Pre-compute answers asynchronously Store object state Choose when to embed data and when to refer to it Design your keys well ©2014 Couchbase, Inc. 6
  • 8. We’re used to asking questions ©2014 Couchbase, Inc. 8
  • 9. Key-value look-ups give us a library of answers ©2014 Couchbase, Inc. 9
  • 10. Answer-oriented databases ©2014 Couchbase, Inc. 10 http://martinfowler.com/bliki/AggregateOrientedDatabase.html
  • 12. Embed or refer? How much should we denormalize?
  • 13. An e-commerce order ©2014 Couchbase, Inc. 13
  • 14. The same order in a document ©2014 Couchbase, Inc. 14
  • 15. Embedded vs. referred data ©2014 Couchbase, Inc. 15
  • 16. You should embed data when:  Speed trumps all else  Slow moving data  No duplication  Application layer can keep multiple copies of same data in sync When to embed data ©2014 Couchbase, Inc. 16
  • 17. You should refer to data when:  Consistency is a priority  The data has large growth potential When to refer to data ©2014 Couchbase, Inc. 17
  • 19. Key design is as important as document design. There are three broad types of keys:  Human readable/deterministic: e.g., an email address  Computer generated/random: e.g., a UUID  Compound: e.g., a UUID with a deterministic portion Three ways to build a key ©2014 Couchbase, Inc. 19
  • 20. Human readable/deterministic key ©2014 Couchbase, Inc. 20 public class user { private String name; private String email; private String streetAddress; private String city; private String country; private String postCode; private String telephone; private Array orders; private Array productsViewed; } { "name": "Matthew Revell", "address": "11-21 Paul Street", "city": "London", "postCode": "EC2A 4JU", "telephone": "44-20-3837-9130", "orders": [ 1, 9, 698, 32 ], “productsViewed”: [8, 33, 99, 100] } Key: matthew@couchbase.com
  • 21. Random/computer-generated key ©2014 Couchbase, Inc. 21 { "name": "Matthew Revell", "email": "matthew@couchbase.com", "address": "11-21 Paul Street", "city": "London", "postCode": "EC2A 4JU", "telephone": "44-20-3837-9130", "orders": [ 1, 9, 698, 32 ], “productsViewed”: [8, 33, 99, 100] } Key: 1001
  • 22. Using counters to generate keys ©2014 Couchbase, Inc. 22 Application user_id = incr(“counter_key") add(user_id, data) Creating a new user add(email_address, user_id) Application key = get("matthew@couchbase.com") get(key) Finding a user by email address
  • 23. Multiple look-up documents ©2014 Couchbase, Inc. 23 u::count 1001 u::1001 { "name": “Matthew Revell", "facebook_id": 16172910, "email": “matthew@couchbase.com”, “password”: ab02d#Jf02K "created_at": "5/1/2012 2:30am", “facebook_access_token”: xox0v2dje20, “twitter_access_token”: 20jffieieaaixixj } fb::16172910 1001 nflx::2939202 1001 twtr::2920283830 1001 em::matthew@couchbase.com 1001 em::matthew@understated.co.uk 1001 uname::mrevell 1001
  • 24. Compound keys Compound keys are look-up documents with predictable names. They continue the discussion of embedded versus referred data.
  • 25. Compound keys: an example u::1001 { "name": "Matthew Revell", "email": "matthew@couchbase.com", "address": "11-21 Paul Street", "city": "London", "postCode": "EC2A 4JU", "telephone": "44-20-3837-9130", "orders": [ 1, 9, 698, 32 ], “productsViewed”: [8, 33, 99, 100] }
  • 26. Compound keys: an example u::1001 { "name": "Matthew Revell", "email": "matthew@couchbase.com", "address": "11-21 Paul Street", "city": "London", "postCode": "EC2A 4JU", "telephone": "44-20-3837-9130", "orders": [ 1, 9, 698, 32 ] } u::1001::productsviewed { "productsList": [ 8, 33, 99, 100 ] }
  • 27. Compound keys: an example u::1001 { "name": "Matthew Revell", "email": "matthew@couchbase.com", "address": "11-21 Paul Street", "city": "London", "postCode": "EC2A 4JU", "telephone": "44-20-3837-9130", "orders": [ 1, 9, 698, 32 ] } u::1001::productsviewed { "productsList": [ 8, 33, 99, 100 ] } p::8 { id": 1, "name": "T-shirt", "description": "Red Couchbase shirt", "quantityInStock": 99, "image": "tshirt.jpg” }
  • 28. Compound keys: an example u::1001 { "name": "Matthew Revell", "email": "matthew@couchbase.com", "address": "11-21 Paul Street", "city": "London", "postCode": "EC2A 4JU", "telephone": "44-20-3837-9130", "orders": [ 1, 9, 698, 32 ] } u::1001::productsviewed { "productsList": [ 8, 33, 99, 100 ] } p::8 { id": 1, "name": "T-shirt", "description": "Red Couchbase shirt", "quantityInStock": 99 } p::8::img “http://someurl.com/tshirt.jpg”
  • 29. N1QL and Views! The world beyond key-value
  • 30. We’re not in Kansas any more ©2014 Couchbase, Inc. 30
  • 31. N1QL and views N1QL Views Ad-hoc querying Predictable queries Nested JSON in and unnested JSON out Number crunching Large growth clusters Multidimensional and geospatial queries ©2014 Couchbase, Inc. 31
  • 32. N1QL Indexes Schema types and document types Keyspaces ©2014 Couchbase, Inc. 32
  • 33. Indexes ©2014 Couchbase, Inc. 33 • You always need at least one index • PRIMARY index gives you all of the keys in the bucket • Secondary indexes enable ad-hoc querying at speed
  • 35. Indexes ©2014 Couchbase, Inc. 35 NEW Global Secondary Indexes Views N1QL query to create a new index JavaScript map/reduce Option to run dedicated indexing and query nodes Queries always run on database nodes ForestDB-backed Couchstore-backed Supports multidimensional and geospatial queries
  • 36. Maintaining the schema and working with keyspaces ©2014 Couchbase, Inc. 36 JOINs work on primary keys and secondary keys. They JOIN across and within keyspaces (for now, that means buckets). Airlines: airline_24 ← Key (“primary key”) { "id": "24", "type": "airline", "name": "American Airlines", "iata": "AA", "icao": "AAL", "callsign": "AMERICAN", "country": "United States", "active": "Y" } Routes: route_5966 ← Key { "id": "5966", "type": "route", "airline": "AA", "airlineid": "airline_24", ← This is the foreign key "sourceairport": "MCO", "destinationairport": "SEA", "stops": "0", "equipment": "737", "schedule": [... ] }
  • 37. Offloading computation to N1QL ©2014 Couchbase, Inc. 37 N1QL allows you to choose which elements are returned at the top level. This is incredibly useful because it reduces the need to write complex parsing logic: • Within the application • In client-side, front-end frameworks (Angular/Backbone etc.) SELECT a.name, s.flight, s.utc, r.sourceairport, r.destinationairport, r.equipment FROM `travel-sample` r UNNEST r.schedule s JOIN `travel- sample` a ON KEYS r.airlineid WHERE r.sourceairport='LHR' AND r.destinationairport='LAX' AND s.day=2 ORDER BY a.name