SlideShare a Scribd company logo
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved.
Power of Relational With the Flexibility of JSON
August 2022
Matthew D. Groves | Product Marketing Manager
Putting the SQL Back in NoSQL
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 2
Querying with SQL
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 3
Querying JSON with JavaScript/JSON
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved.
01/
02/
03/
04/
Relational Data to JSON
Basics of SQL++
How to Get Started
Questions
Agenda
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 5
1 Relational Data to JSON
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 6
Modeling Data in a Relational World
Billing
Connections
Purchases
Contact
Customer
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 7
Relational Challenges
• "Vertical" scaling –
expensive and has a
ceiling
• "Horizontal" scaling –
complex and difficult to
maintain with a
relational database
• Rigid table-based data
modeling
• Schema changes can
be time consuming,
costly
Flexibility
• Limited by disk access
• Often needs an external
caching solution
• Complex queries and
large number of JOINs
• Single database server
means single point of
failure
• Replication/distribution,
if available, requires
more overhead
Lorem ipsum Lorem ipsum
In other words, why consider NoSQL?
Scaling Performance Availability
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 8
Modeling Data in a JSON World
Customer
{ JSON }
{ JSON }
{ JSON }
{
"Name" : "Jane Smith",
"DOB" : "1990-01-30",
"Billing" : [ … ],
"Connections" : [ … ],
"Purchases" : [ …. ]
}
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved.
{
"Name" : "Jane Smith",
"DOB" : "1990-01-30",
"Billing" : [
{
"type" : "visa",
"cardnum" : "5827-2842-2847-3909",
"expiry" : "2019-03"
},
{
"type" : "mcard",
"cardnum" : "6274-2842-2847-3909",
"expiry" : "2019-03"
}
],
"Connections" : [
{
"CustId" : "XYZ987",
"Name" : "Joe Smith"
},
{
"CustId" : "SKR007",
"Name" : "Sam Smith"
}
],
"Purchases" : [
{ "id":12, item: "mac", "amt": 282.52 }
{ "id":19, item: "ipad2", "amt": 623.52 }
], . . .
}
DocumentKey: CBL2016
CustomerID Name DOB
CBL2016 Bob Jones 1980-01-29
CustomerID Type Cardnum Expiry
CBL2016 visa 5927… 2020-03
CBL2016 mcard 6273… 2019-11
CustomerID ConnId Name
CBL2016 XYZ987 Joe Smith
CBL2016 SKR007 Sam
Smith
CustomerID item amt
CBL2016 mac 282.52
CBL2016 ipad2 623.52
CustomerID Address Email
CBL2016 123
Main
a@b.gov
CBL2016 456
Elm
c@d.gov
Contacts
Customer
Billing
Connections
Purchases
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 10
But at what cost?
• Easy to read, designed to be read
by humans
• Lots of training material, taught in
schools and bootcamps
• Concise, declarative syntax
• Some data has to be modelled as
many-to-many and many-to-one
• Even with JSON consolidation, the
need for joining data together is
critical for some use cases
JOINs
• Within one JSON document,
writes are ACID
• What about when transferring
money between accounts?
• Even with JSON consolidation, the
need for interaction between
multiple documents is critical for
some use cases
SQL standard is the most popular
data query language
What if I can't put everything into one
document?
Not every use case can fit into one
document either
There are GOOD things about relational databases!
SQL ACID transactions
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 11
Enter SQL++
SQL for JSON?
https://resources.couchbase.com/analytics/sql-book
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 12
Relational-to-NoSQL Translation
SQL Server Couchbase Note
Server Cluster ➕ Scalability / High Availability / Built-in caching
Catalog (Database) Bucket
Schema Scope Often just "dbo" in SQL Server or "_default" in Couchbase
Table Collection ➖ Pre-defined columns/constraints
Row Document ➕ Flexible JSON
tSQL SQL++ (Previously known as N1QL in Couchbase)
Primary Key Document Key ➖ Compound keys
➖ "No" keys
Index Index
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 13
2 Basics of SQL++
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 14
Route document example
route_5966 ← key
{
"airlineid": "airline_24", ← "foreign key"
"sourceairport": "MCO",
"destinationairport": "SEA",
"equipment": "737",
"schedule": [
{"day": 1, "utc": "13:25:00", "flight": "AA788"},
{"day": 4, "utc": "13:25:00", "flight": "AA419"},
{"day": 5, "utc": "13:25:00", "flight": "AA519"}
]
}
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 15
Airline document example
airline_24 ← key
{
"active": "Y",
"callsign": "AMERICAN",
"country": "United States",
"iata": "AA",
"icao": "AAL",
"name": "American Airlines",
}
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 16
SELECT
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 17
How do I get that airline name?
route_5966
{
"airlineid": "airline_24",
"airlineName": "Delta", ← denormalize / duplicate
"sourceairport": "MCO",
"destinationairport": "SEA",
"equipment": "737",
"schedule": [
{"day": 1, "utc": "13:25:00", "flight": "AA788"},
{"day": 4, "utc": "13:25:00", "flight": "AA419"},
{"day": 5, "utc": "13:25:00", "flight": "AA519"}
]
}
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 18
SELECT . . . JOIN
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 19
SELECT . . . JOIN . . . HAVING . . . ORDER
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 20
BEGIN/COMMIT/ROLLBACK
ACID Transactions
ID: 1924
{
"name": "Emma",
"balance": 0
}
ID: 4872
{
"name": "Matthew",
"balance": 1400
}
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 21
BEGIN/COMMIT/ROLLBACK
ACID Transactions
BEGIN TRANSACTION;
UPDATE customer
SET balance = balance + 100
WHERE META().id = "1924";
UPDATE customer
SET balance = balance - 100
WHERE META().id = "4872";
SELECT name, balance
FROM customer
WHERE META().id IN ["4872","1924"];
COMMIT TRANSACTION;
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 22
Indexing
bucket scope collection
bucket scope collection field(s) / expression(s)
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 23
Index Advisor
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 24
Cost-based Optimization
Which index to use?
CREATE PRIMARY index ON customer;
CREATE INDEX ix1 ON customer(name, zip, status);
CREATE INDEX ix2 ON customer(zip, name, status);
CREATE INDEX ix3 ON customer(zip, status, name);
CREATE INDEX ix4 ON customer(name, status, zip);
CREATE INDEX ix5 ON customer(status, zip, name);
CREATE INDEX ix6 ON customer(status, name, zip);
CREATE INDEX ix7 ON customer(status);
CREATE INDEX ix8 ON customer(name);
CREATE INDEX ix9 ON customer(zip);
CREATE INDEX ix10 ON customer(status, name);
CREATE INDEX ix11 ON customer(status, zip);
CREATE INDEX ix12 ON customer(name, zip);
SELECT *
FROM customer
WHERE name = "Matt"
AND zip = "43123"
AND status = "Standard";
UPDATE STATISTICS for `customer`
(status, name, zip);
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 25
User-Defined Functions (UDFs)
CREATE FUNCTION getAirlineRank(in_country) { (
SELECT a.id,
a.name AS airline,
COUNT(r.id) AS routecount,
PERCENT_RANK() OVER (ORDER BY COUNT(r.id)) AS `rank`
FROM `travel-sample`.`inventory`.`airline` a
JOIN `travel-sample`.`inventory`.`route` r
ON META(a).id = r.airlineid
WHERE a.country = in_country
GROUP BY a.id, a.name
)};
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 26
Eventing
function OnUpdate(doc, meta) {
var minDistance = 1000;
var results =
SELECT *
FROM `travel-sample`.inventory.route
WHERE distance > $minDistance;
for (var route of results) {
log(route);
break;
}
results.close();
}
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 27
Analytics
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 28
Tools for Moving Data
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 29
3 How to Get Started
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 30
Couchbase Capella
https://www.couchbase.com/products/capella
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 31
Couchbase Playground
https://couchbase.live
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 32
Demo
Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved.
THANK YOU

More Related Content

Putting the SQL Back in NoSQL - October 2022 - All Things Open

  • 1. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. Power of Relational With the Flexibility of JSON August 2022 Matthew D. Groves | Product Marketing Manager Putting the SQL Back in NoSQL
  • 2. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 2 Querying with SQL
  • 3. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 3 Querying JSON with JavaScript/JSON
  • 4. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 01/ 02/ 03/ 04/ Relational Data to JSON Basics of SQL++ How to Get Started Questions Agenda
  • 5. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 5 1 Relational Data to JSON
  • 6. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 6 Modeling Data in a Relational World Billing Connections Purchases Contact Customer
  • 7. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 7 Relational Challenges • "Vertical" scaling – expensive and has a ceiling • "Horizontal" scaling – complex and difficult to maintain with a relational database • Rigid table-based data modeling • Schema changes can be time consuming, costly Flexibility • Limited by disk access • Often needs an external caching solution • Complex queries and large number of JOINs • Single database server means single point of failure • Replication/distribution, if available, requires more overhead Lorem ipsum Lorem ipsum In other words, why consider NoSQL? Scaling Performance Availability
  • 8. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 8 Modeling Data in a JSON World Customer { JSON } { JSON } { JSON } { "Name" : "Jane Smith", "DOB" : "1990-01-30", "Billing" : [ … ], "Connections" : [ … ], "Purchases" : [ …. ] }
  • 9. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. { "Name" : "Jane Smith", "DOB" : "1990-01-30", "Billing" : [ { "type" : "visa", "cardnum" : "5827-2842-2847-3909", "expiry" : "2019-03" }, { "type" : "mcard", "cardnum" : "6274-2842-2847-3909", "expiry" : "2019-03" } ], "Connections" : [ { "CustId" : "XYZ987", "Name" : "Joe Smith" }, { "CustId" : "SKR007", "Name" : "Sam Smith" } ], "Purchases" : [ { "id":12, item: "mac", "amt": 282.52 } { "id":19, item: "ipad2", "amt": 623.52 } ], . . . } DocumentKey: CBL2016 CustomerID Name DOB CBL2016 Bob Jones 1980-01-29 CustomerID Type Cardnum Expiry CBL2016 visa 5927… 2020-03 CBL2016 mcard 6273… 2019-11 CustomerID ConnId Name CBL2016 XYZ987 Joe Smith CBL2016 SKR007 Sam Smith CustomerID item amt CBL2016 mac 282.52 CBL2016 ipad2 623.52 CustomerID Address Email CBL2016 123 Main a@b.gov CBL2016 456 Elm c@d.gov Contacts Customer Billing Connections Purchases
  • 10. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 10 But at what cost? • Easy to read, designed to be read by humans • Lots of training material, taught in schools and bootcamps • Concise, declarative syntax • Some data has to be modelled as many-to-many and many-to-one • Even with JSON consolidation, the need for joining data together is critical for some use cases JOINs • Within one JSON document, writes are ACID • What about when transferring money between accounts? • Even with JSON consolidation, the need for interaction between multiple documents is critical for some use cases SQL standard is the most popular data query language What if I can't put everything into one document? Not every use case can fit into one document either There are GOOD things about relational databases! SQL ACID transactions
  • 11. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 11 Enter SQL++ SQL for JSON? https://resources.couchbase.com/analytics/sql-book
  • 12. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 12 Relational-to-NoSQL Translation SQL Server Couchbase Note Server Cluster ➕ Scalability / High Availability / Built-in caching Catalog (Database) Bucket Schema Scope Often just "dbo" in SQL Server or "_default" in Couchbase Table Collection ➖ Pre-defined columns/constraints Row Document ➕ Flexible JSON tSQL SQL++ (Previously known as N1QL in Couchbase) Primary Key Document Key ➖ Compound keys ➖ "No" keys Index Index
  • 13. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 13 2 Basics of SQL++
  • 14. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 14 Route document example route_5966 ← key { "airlineid": "airline_24", ← "foreign key" "sourceairport": "MCO", "destinationairport": "SEA", "equipment": "737", "schedule": [ {"day": 1, "utc": "13:25:00", "flight": "AA788"}, {"day": 4, "utc": "13:25:00", "flight": "AA419"}, {"day": 5, "utc": "13:25:00", "flight": "AA519"} ] }
  • 15. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 15 Airline document example airline_24 ← key { "active": "Y", "callsign": "AMERICAN", "country": "United States", "iata": "AA", "icao": "AAL", "name": "American Airlines", }
  • 16. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 16 SELECT
  • 17. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 17 How do I get that airline name? route_5966 { "airlineid": "airline_24", "airlineName": "Delta", ← denormalize / duplicate "sourceairport": "MCO", "destinationairport": "SEA", "equipment": "737", "schedule": [ {"day": 1, "utc": "13:25:00", "flight": "AA788"}, {"day": 4, "utc": "13:25:00", "flight": "AA419"}, {"day": 5, "utc": "13:25:00", "flight": "AA519"} ] }
  • 18. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 18 SELECT . . . JOIN
  • 19. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 19 SELECT . . . JOIN . . . HAVING . . . ORDER
  • 20. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 20 BEGIN/COMMIT/ROLLBACK ACID Transactions ID: 1924 { "name": "Emma", "balance": 0 } ID: 4872 { "name": "Matthew", "balance": 1400 }
  • 21. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 21 BEGIN/COMMIT/ROLLBACK ACID Transactions BEGIN TRANSACTION; UPDATE customer SET balance = balance + 100 WHERE META().id = "1924"; UPDATE customer SET balance = balance - 100 WHERE META().id = "4872"; SELECT name, balance FROM customer WHERE META().id IN ["4872","1924"]; COMMIT TRANSACTION;
  • 22. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 22 Indexing bucket scope collection bucket scope collection field(s) / expression(s)
  • 23. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 23 Index Advisor
  • 24. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 24 Cost-based Optimization Which index to use? CREATE PRIMARY index ON customer; CREATE INDEX ix1 ON customer(name, zip, status); CREATE INDEX ix2 ON customer(zip, name, status); CREATE INDEX ix3 ON customer(zip, status, name); CREATE INDEX ix4 ON customer(name, status, zip); CREATE INDEX ix5 ON customer(status, zip, name); CREATE INDEX ix6 ON customer(status, name, zip); CREATE INDEX ix7 ON customer(status); CREATE INDEX ix8 ON customer(name); CREATE INDEX ix9 ON customer(zip); CREATE INDEX ix10 ON customer(status, name); CREATE INDEX ix11 ON customer(status, zip); CREATE INDEX ix12 ON customer(name, zip); SELECT * FROM customer WHERE name = "Matt" AND zip = "43123" AND status = "Standard"; UPDATE STATISTICS for `customer` (status, name, zip);
  • 25. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 25 User-Defined Functions (UDFs) CREATE FUNCTION getAirlineRank(in_country) { ( SELECT a.id, a.name AS airline, COUNT(r.id) AS routecount, PERCENT_RANK() OVER (ORDER BY COUNT(r.id)) AS `rank` FROM `travel-sample`.`inventory`.`airline` a JOIN `travel-sample`.`inventory`.`route` r ON META(a).id = r.airlineid WHERE a.country = in_country GROUP BY a.id, a.name )};
  • 26. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 26 Eventing function OnUpdate(doc, meta) { var minDistance = 1000; var results = SELECT * FROM `travel-sample`.inventory.route WHERE distance > $minDistance; for (var route of results) { log(route); break; } results.close(); }
  • 27. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 27 Analytics
  • 28. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 28 Tools for Moving Data
  • 29. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 29 3 How to Get Started
  • 30. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 30 Couchbase Capella https://www.couchbase.com/products/capella
  • 31. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 31 Couchbase Playground https://couchbase.live
  • 32. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. 32 Demo
  • 33. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2021. All rights reserved. THANK YOU