It can be difficult to scale out relational databases and provide more schema flexibility, thus the rise of NoSQL. However, you shouldn’t have to sacrifice data integrity and transactions in order to scale out on commodity hardware and support semi-structured data. By using an RDBMS with built-in sharding and JSON support, you don’t have to. You get the scalability and flexibility of a NoSQL database along with the consistency and reliability of a relational database – and the ability to mix and match relational and JSON data.
In this webinar, we’ll explain how MariaDB Platform can be deployed as a NoSQL database by using the Spider storage engine and built-in SQL functions for JSON. In addition, we’ll discuss how you can access relational data as JSON documents, and how to enforce data integrity if a relational data model is extended with JSON.
4. Proven Technology
● 1970: “Relational Completeness of Data Base Sublanguages”
● ANSI managed Structured Query Language (SQL)
● Large ecosystem of RDBMS Vendors and Tools
● Large Talent Pool
○ Developers & Data Modelers
○ DBAs/Operators
○ Architects
7. NoSQL & Document Databases
● NoSQL
○ Key-Value, Wide Column, Document, and more
● Drivers of Popularity of Document Databases
○ Scalability
○ JSON Support & Schema-less
○ Drivers and Modern Application Stacks (Node.js)
9. NoSQL: Caveats
● Limited Transaction support
● Proprietary and inefficient SQL access
● Fewer integrations and smaller ecosystem
● Smaller talent pool
● Down-sides to documents and schema-less
○ Data hygiene
○ Storage size and query efficiency
○ Expensive updates of denormalized data
11. Default Database on Leading Linux Distros,
Available on Leading Cloud Platforms
Cloud Services & StacksLinux Distributions
12. 12 Million Users in
45 Countries Trust Critical
Business Data to MariaDB
Technology & InternetTelecom
Retail & EcommerceTravel
Financial Services Gvmt & Education
Media & Social
14. Scalability
● MariaDB has multiple options for Scalability
○ Read Scalability with Replicas
○ Write Scalability with Spider (Partitioning/Sharding)
○ Scaling for Big Data Analytics with MariaDB ColumnStore
○ Other techniques via MaxScale router
○ Vertical scaling
15. Scalability: Read Scalability via Replicas
MariaDB Platform
MariaDB Server
(slave)
MariaDB Server
(master)
MariaDB Server
(slave)
MariaDB MaxScale
16. Scalability: Write Scalability via Spider
● Range
● Hash
● Key
● List
MariaDB Platform
MariaDB ServerMariaDB Server MariaDB Server
MariaDB Server
MariaDB MaxScale
Spider Storage Engine
17. Scalability: Big Data via Platform &
ColumnStore
MariaDB Platform
MariaDB MaxScale
CDC
MariaDB Server
InnoDB
MariaDB Server
ColumnStore
Transactional Analytical
20. Structured and semi-structured data
with relational + JSON
Structure data described
by the schema
Structured described
by the data
21. JSON Example: definition
CREATE TABLE IF NOT EXISTS products (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
type VARCHAR(1) NOT NULL,
name VARCHAR(40) NOT NULL,
format VARCHAR(20) NOT NULL,
price FLOAT(5, 2) NOT NULL,
attr JSON NOT NULL);
23. Example: read field
SELECT name, format, price,
JSON_VALUE(attr, '$.video.aspectRatio') AS aspect_ratio
FROM products
WHERE type = 'M';
name format price aspect_ratio
Aliens Blu-ray 13.99 1.85:1
24. Example: null field
SELECT type, name, format, price,
JSON_VALUE(attr,$.video.aspectRatio') AS aspect_ratio
FROM products;
type name format price aspect_ratio
M Aliens Blu-ray 13.99 1.85:1
B Foundation Paperback 7.99 NULL
25. Example: contains value
SELECT id, name, format, price
FROM products
WHERE type = 'M' AND
JSON_CONTAINS(attr, '"DTS HD"', '$.audio') = 1;
name format price aspect_ratio
Aliens Blu-ray 13.99 1.85:1
26. Example: read array
SELECT name, format, price,
JSON_QUERY(attr, '$.audio') AS audio
FROM products
WHERE type = 'M';
name format price audio
Aliens Blu-ray 13.99 [
"DTS HD",
"Dolby Surround"
]
27. Example: read array element
SELECT name, format, price,
JSON_VALUE(attr, '$.audio[0]') AS default_audio
FROM products
WHERE type = 'M';
name format price audio
Aliens Blu-ray 13.99 DTS HD
28. Example: read object
SELECT name, format, price,
JSON_QUERY(attr, '$.video') AS video
FROM products
WHERE type = 'M';
name format price video
Aliens Blu-ray 13.99 {
"resolution": "1080p",
"aspectRatio": "1.85:1"
}
29. Example: field equals
SELECT id, name, format, price
FROM products
WHERE type = 'M' AND
JSON_VALUE(attr, '$.video.resolution') = '1080p';
name format price aspect_ratio
Aliens Blu-ray 13.99 1.85:1
30. Example: indexing (1/2)
ALTER TABLE products ADD COLUMN
video_resolution VARCHAR(5) AS (JSON_VALUE(attr, '$.video.resolution')) VIRTUAL;
EXPLAIN SELECT name, format, price
FROM products
WHERE video_resolution = '1080p';
id select_type table type possible_keys
1 SIMPLE products ALL NULL
31. Example: indexing (2/2)
CREATE INDEX resolutions ON products(video_resolution);
EXPLAIN SELECT name, format, price
FROM products
WHERE video_resolution = '1080p';
id select_type table ref possible_keys
1 SIMPLE products ref resolutions
32. Example: insert field
UPDATE products
SET attr = JSON_INSERT(attr, '$.disks', 1)
WHERE id = 1;
name format price disks
Aliens Blu-ray 13.99 1
SELECT name, format, price,
JSON_VALUE(attr, '$.disks') AS disks
FROM products
WHERE type = 'M';
33. Example: insert array
UPDATE products
SET attr =JSON_INSERT(attr, '$.languages',
JSON_ARRAY('English', 'French'))
WHERE id = 1;
name format price languages
Aliens Blu-ray 13.99 [
"English",
"French"
]
SELECT name, format, price,
JSON_QUERY(attr, '$.languages')
AS languages
FROM products
WHERE type = 'M';
34. Example: add array element
UPDATE products
SET attr = JSON_ARRAY_APPEND(attr,
'$.languages', 'Spanish')
WHERE id = 1;
name format price languages
Aliens Blu-ray 13.99 [
"English",
"French",
"Spanish"
]
SELECT name, format, price,
JSON_QUERY(attr, '$.languages')
AS languages
FROM products
WHERE type = 'M';
35. Example: remove array element
UPDATE products
SET attr = JSON_REMOVE(attr, '$.languages[0]')
WHERE id = 1;
name format price languages
Aliens Blu-ray 13.99 [
"French",
"Spanish"
]
SELECT name, format, price,
JSON_QUERY(attr, '$.languages')
AS languages
FROM products
WHERE type = 'M';
36. Example: relational to JSON
SELECT JSON_OBJECT('name', name, 'format', format, 'price', price) AS data
FROM products
WHERE type = 'M';
data
{
"name": "Tron",
"format": "Blu-ray",
"price": 29.99
}
37. Example: relational + JSON
SELECT JSON_OBJECT('name', name, 'format', format, 'price', price, 'resolution',
JSON_VALUE(attr, '$.video.resolution')) AS data
FROM products
WHERE type = 'M';
data
{
"name": "Aliens",
"format": "Blu-ray",
"price": 13.99,
"resolution": "1080p"
}
38. Example: relational + JSON
SELECT JSON_MERGE(
JSON_OBJECT(
'name', name,
'format', format),
attr) AS data
FROM products
WHERE type = 'M';
data
{
"name": "Tron",
"format": "Blu-ray",
"video": {
"resolution": "1080p",
"aspectRatio": "1.85:1"},
"cuts": [
{"name": "Theatrical", "runtime": 138},
{"name": "Special Edition", "runtime": 155}],
"audio": [
"DTS HD",
"Dolby Surround"]}
39. Example: constraints (1/3)
ALTER TABLE products ADD CONSTRAINT check_attr
CHECK (
type != 'M' or (type = 'M' and
JSON_TYPE(JSON_QUERY(attr, '$.video')) = 'OBJECT' and
JSON_TYPE(JSON_QUERY(attr, '$.cuts')) = 'ARRAY' and
JSON_TYPE(JSON_QUERY(attr, '$.audio')) = 'ARRAY' and
JSON_TYPE(JSON_VALUE(attr, '$.disks')) = 'INTEGER' and
JSON_EXISTS(attr, '$.video.resolution') = 1 and
JSON_EXISTS(attr, '$.video.aspectRatio') = 1 and
JSON_LENGTH(JSON_QUERY(attr, '$.cuts')) > 0 and
JSON_LENGTH(JSON_QUERY(attr, '$.audio')) > 0));
41. Example: constraints (3/3)
INSERT INTO products (type, name, format, price, attr) VALUES
('M', 'Tron', 'Blu-ray', 29.99, '{"video": {"resolution": "1080p", "aspectRatio": "2.21:1"}, "cuts":
[{"name": "Theatrical", "runtime": 96}], "audio": ["DTS HD", "Dolby Digital"], "disks": "one"}');
ERROR 4038 (HY000): Syntax error in JSON text in argument 1 to function 'json_type' at
position 1
"one" is not a number!
43. • Ryan Dahl, JSConf 2009
• Event Loop, Callbacks & Non-blocking IO
• Different mindset from synchronous
programming
44. GA MariaDB Access since 2014
2011-2013: MySQL Development
Jan 2014: MySQL GA Release
Sep 2016: MySQL2 GA Release
Dec 2018: MariaDB RC
Jan 2019: MariaDB GA Release
45. Node.js Driver
● MariaDB Released Driver available Jan 2019
● Increased performance
○ Pipelining of statements
○ Bulk operations
● ORM Support: Sequelize
● Frameworks: Feathers.js
46. Sequelize Model Definition
class Task extends Model {}
Task.init({
title: Sequelize.STRING,
description: Sequelize.TEXT,
deadline: Sequelize.DATE
}, { sequelize, modelName: 'task' })
http://docs.sequelizejs.com/
47. Installation js-demo$ npm install mariadb
+ mariadb@2.0.2-rc
updated 1 package and audited 180 packages in 1.321s
found 0 vulnerabilities
js-demo$
• NPM Package Manager
• Download at https://mariadb.com/downloads/#connectors
js-demo$ tar -xzf mariadb-connector-nodejs-2.0.2-rc.tar.gz
js-demo$ cd mariadb-connector-nodejs-2.0.2-rc
js-demo$ npm link
js-demo$ cd <project directory>
js-demo$ npm link mariadb