3. ROOUG, December 2017
MySQL Document Store
How to replace a NoSQL database by MySQL without effort but with a lot of
gains ?
Frédéric Descamps - MySQL Community Manager - Oracle
3 / 110
4.
Safe Harbor Statement
The following is intended to outline our general product direction. It is intended for
information purpose only, and may not be incorporated into any contract. It is not a
commitment to deliver any material, code, or functionality, and should not be relied up in
making purchasing decisions. The development, release and timing of any features or
functionality described for Oracle´s product remains at the sole discretion of Oracle.
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
4 / 110
5. about.me/lefred
Who am I ?
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
5 / 110
15. Relational Databases
Data Integrity
normalization
constraints (foreign keys, ...)
Atomicity, Consistency, Isolation, Durability
ACID compliant
transactions
SQL
powerfull query language
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
15 / 110
16. NoSQL & MySQL
NoSQL Databases
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
16 / 110
17. NoSQL or Document Store
Schemaless
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
17 / 110
18. NoSQL or Document Store
Schemaless
no schema design, no normalization, no foreign keys, no data types, ...
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
18 / 110
19. NoSQL or Document Store
Schemaless
no schema design, no normalization, no foreign keys, no data types, ...
very quick initial development
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
19 / 110
20. NoSQL or Document Store
Schemaless
no schema design, no normalization, no foreign keys, no data types, ...
very quick initial development
Flexible data structure
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
20 / 110
21. NoSQL or Document Store
Schemaless
no schema design, no normalization, no foreign keys, no data types, ...
very quick initial development
Flexible data structure
embedded arrays or objects
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
21 / 110
22. NoSQL or Document Store
Schemaless
no schema design, no normalization, no foreign keys, no data types, ...
very quick initial development
Flexible data structure
embedded arrays or objects
valid solution when natural data can't be modelized optimaly into a relational
model
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
22 / 110
23. NoSQL or Document Store
Schemaless
no schema design, no normalization, no foreign keys, no data types, ...
very quick initial development
Flexible data structure
embedded arrays or objects
valid solution when natural data can't be modelized optimaly into a relational
model
objects persistance without the use of any ORM - mapping oobject-oriented
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
23 / 110
24. NoSQL or Document Store
Schemaless
no schema design, no normalization, no foreign keys, no data types, ...
very quick initial development
Flexible data structure
embedded arrays or objects
valid solution when natural data can't be modelized optimaly into a relational
model
objects persistance without the use of any ORM - mapping oobject-oriented
JSON
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
24 / 110
25. NoSQL or Document Store
Schemaless
no schema design, no normalization, no foreign keys, no data types, ...
very quick initial development
Flexible data structure
embedded arrays or objects
valid solution when natural data can't be modelized optimaly into a relational
model
objects persistance without the use of any ORM - mapping oobject-oriented
JSON
close to frontend
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
25 / 110
26. NoSQL or Document Store
Schemaless
no schema design, no normalization, no foreign keys, no data types, ...
very quick initial development
Flexible data structure
embedded arrays or objects
valid solution when natural data can't be modelized optimaly into a relational
model
objects persistance without the use of any ORM - mapping oobject-oriented
JSON
close to frontend
native in JS
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
26 / 110
27. NoSQL or Document Store
Schemaless
no schema design, no normalization, no foreign keys, no data types, ...
very quick initial development
Flexible data structure
embedded arrays or objects
valid solution when natural data can't be modelized optimaly into a relational
model
objects persistance without the use of any ORM - mapping oobject-oriented
JSON
close to frontend
native in JS
easy to learn
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
27 / 110
28. How DBAs see data
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
28 / 110
29. How DBAs see data How Developers see data
{
"GNP" : 249704,
"Name" : "Belgium",
"government" : {
"GovernmentForm" :
"Constitutional Monarchy, Federation",
"HeadOfState" : "Philippe I"
},
"_id" : "BEL",
"IndepYear" : 1830,
"demographics" : {
"Population" : 10239000,
"LifeExpectancy" : 77.8000030517578
},
"geography" : {
"Region" : "Western Europe",
"SurfaceArea" : 30518,
"Continent" : "Europe"
}
}
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
29 / 110
30. Relational Model
Higher upfront effort during design
stage
data modeling and normalization
physical schema definition
Database ensures integrity (invalid
data rejected)
Data types
Constraints (foreign keys etc.)
Relational Databases vs Document Store
Systems
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
30 / 110
31. Relational Model
Higher upfront effort during design
stage
data modeling and normalization
physical schema definition
Database ensures integrity (invalid
data rejected)
Data types
Constraints (foreign keys etc.)
Document Model
Lower upfront effort
data structure defined in
application code
Application responsible for data
integrity
possibility for bad data to creep
into the system (because of bugs,
application changes etc.)
Relational Databases vs Document Store
Systems
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
31 / 110
32. Relational Model
Steeper learning curve
requires knowledge of SQL
Query language allows optimizer to
execute complex queries efficiently
(specially for JOINs)
Schema changes can be problematic
(ex upgrades)
Relational Databases vs Document Store
Systems (2)
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
32 / 110
33. Relational Model
Steeper learning curve
requires knowledge of SQL
Query language allows optimizer to
execute complex queries efficiently
(specially for JOINs)
Schema changes can be problematic
(ex upgrades)
Document Model
Easier to get started with
Limited or no JOINs, simple optimizer -
complexity off-loaded to application
Simpler deployment, different versions
of the document can coexist
Relational Databases vs Document Store
Systems (2)
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
33 / 110
34. DMBS or NoSQL ?
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
34 / 110
35. DMBS or NoSQL ?
Why not both ?
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
35 / 110
36. Developers:
[x] schemaless
[x] rapid prototying/simpler APIs
[x] document model
[x] transactions
Operations:
[x] performance
management/visibility
[x] robust replication, backup,
restore
[x] comprehensive tooling
ecosystem
[x] simpler application schema
upgrades
Business Owner:
[x] don't lose my data == ACID trx
[x] capture all my data =
extensible/schemaless
[x] product on schedule/time to
market = rapid developement
A solution for all
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
36 / 110
37. MySQL as Document Store
Requirements
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
37 / 110
38. MySQL as Document Store: requirements
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
38 / 110
39. MySQL as Document Store: requirements
1. support of JSON datatype
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
39 / 110
40. MySQL as Document Store: requirements
1. support of JSON datatype
2. CRUD Operations
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
40 / 110
41. MySQL as Document Store: requirements
1. support of JSON datatype
2. CRUD Operations
3. X-Plugin & X Protocol
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
41 / 110
42. MySQL as Document Store: requirements
1. support of JSON datatype
2. CRUD Operations
3. X-Plugin & X Protocol
extends and replaces the traditional MySQL protocol
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
42 / 110
43. MySQL as Document Store: requirements
1. support of JSON datatype
2. CRUD Operations
3. X-Plugin & X Protocol
extends and replaces the traditional MySQL protocol
supports asynchronous communication to the server
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
43 / 110
44. MySQL as Document Store: requirements
1. support of JSON datatype
2. CRUD Operations
3. X-Plugin & X Protocol
extends and replaces the traditional MySQL protocol
supports asynchronous communication to the server
supports multiple API calls like Javascript, Python, SQL...
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
44 / 110
45. MySQL as Document Store: requirements
1. support of JSON datatype
2. CRUD Operations
3. X-Plugin & X Protocol
extends and replaces the traditional MySQL protocol
supports asynchronous communication to the server
supports multiple API calls like Javascript, Python, SQL...
based on Google Protocol Buffers
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
45 / 110
46. MySQL as Document Store: requirements
1. support of JSON datatype
2. CRUD Operations
3. X-Plugin & X Protocol
extends and replaces the traditional MySQL protocol
supports asynchronous communication to the server
supports multiple API calls like Javascript, Python, SQL...
based on Google Protocol Buffers
4. MySQL Shell
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
46 / 110
48. MySQL as Document Store
Starting with DS
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
48 / 110
49. Starting using MySQL DS in few minutes
Install X Plugin
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
49 / 110
50. Starting using MySQL DS in few minutes
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
50 / 110
51. Migration from MongoDB to MySQL DS
For this example, I will use the well known restaurants collection:
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
51 / 110
52. Migration from MongoDB to MySQL DS
For this example, I will use the well known restaurants collection:
We need to dump the data to a file and we will use MySQL Shell with the Python
interpreter to load the data.
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
52 / 110
53. Migration from MongoDB to MySQL DS
For this example, I will use the well known restaurants collection:
We need to dump the data to a file and we will use MySQL Shell with the Python
interpreter to load the data.
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
53 / 110
54. Dump and load using MySQL Shell & Python
this example is inspired by @datacharmer's work:
https://www.slideshare.net/datacharmer/mysql-documentstore
$ mongo --quiet --eval 'DBQuery.shellBatchSize=30000;
db.restaurants. nd().shellPrint()'
| perl -pe 's/(?:ObjectId|ISODate)(("[^"]+"))/ $1/g' > all_recs.json
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
54 / 110
55. Dump and load using MySQL Shell & Python
this example is inspired by @datacharmer's work:
https://www.slideshare.net/datacharmer/mysql-documentstore
$ mongo --quiet --eval 'DBQuery.shellBatchSize=30000;
db.restaurants. nd().shellPrint()'
| perl -pe 's/(?:ObjectId|ISODate)(("[^"]+"))/ $1/g' > all_recs.json
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
55 / 110
56. Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
56 / 110
65. 8.0.3 what´s new ?
WL10446 - Customizable prompts
WL10568 - Handling of Ctrl+C has been improved on many scenarios
WL10708 and WL10823 - NodeSession disappeared in favor of simply Session
WL10912 - Changes on connection options handling:
case insensitive: i.e. ssl-mode == Ssl-Mode
renamed from camel case to word dashed format: sslMode --> ssl-mode
WL11054 - Admin API supports automatic turn of of super-read-only option on
several functions.
WL10682 - Mysqlx.CRUD.Update on top level document
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
65 / 110
66. 8.0.3 what´s new ? (part 2)
WL10645 - X Protocol Crud.Find with row locking
WL10734 - Spatial index type
WL10744 - Full-Text Index Type
WL10797 - Mysqlx.Crud.Update: MERGE_PATCH
WL10447 - auto-completion by hitting in the middle of typing a shell command,
database object, object function, ...
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
66 / 110
69. 8.0.3 labs: auto-completion - even for SQL !
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
69 / 110
70. what about my old SQL ?
The hidden part of the iceberg
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
70 / 110
71. JSON datatype is behind the scene
native datatype (since 5.7.8)
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
71 / 110
72. JSON datatype is behind the scene
native datatype (since 5.7.8)
JSON values are stored in MySQL tables using UTF8MB4
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
72 / 110
73. JSON datatype is behind the scene
native datatype (since 5.7.8)
JSON values are stored in MySQL tables using UTF8MB4
Conversion from "native" SQL types to JSON values
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
73 / 110
74. JSON datatype is behind the scene
native datatype (since 5.7.8)
JSON values are stored in MySQL tables using UTF8MB4
Conversion from "native" SQL types to JSON values
JSON manipulation functions (JSON_EXTRACT, JSON_KEYS,
JSON_SEARCH,...)
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
74 / 110
75. JSON datatype is behind the scene
native datatype (since 5.7.8)
JSON values are stored in MySQL tables using UTF8MB4
Conversion from "native" SQL types to JSON values
JSON manipulation functions (JSON_EXTRACT, JSON_KEYS,
JSON_SEARCH,...)
Generated/virtual columns
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
75 / 110
76. JSON datatype is behind the scene
native datatype (since 5.7.8)
JSON values are stored in MySQL tables using UTF8MB4
Conversion from "native" SQL types to JSON values
JSON manipulation functions (JSON_EXTRACT, JSON_KEYS,
JSON_SEARCH,...)
Generated/virtual columns
Indexing JSON data
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
76 / 110
77. JSON datatype is behind the scene
native datatype (since 5.7.8)
JSON values are stored in MySQL tables using UTF8MB4
Conversion from "native" SQL types to JSON values
JSON manipulation functions (JSON_EXTRACT, JSON_KEYS,
JSON_SEARCH,...)
Generated/virtual columns
Indexing JSON data
Foreign Keys to JSON data
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
77 / 110
78. JSON datatype is behind the scene
native datatype (since 5.7.8)
JSON values are stored in MySQL tables using UTF8MB4
Conversion from "native" SQL types to JSON values
JSON manipulation functions (JSON_EXTRACT, JSON_KEYS,
JSON_SEARCH,...)
Generated/virtual columns
Indexing JSON data
Foreign Keys to JSON data
SQL Views to JSON data
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
78 / 110
79. How does it work ?
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
79 / 110
80. Mapping to SQL examples
createCollection('mycollection')
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
80 / 110
81. Mapping to SQL examples
createCollection('mycollection')
CREATE TABLE `test`.`mycoll` (
doc JSON,
_id VARCHAR(32)
GENERATED ALWAYS AS (doc->>'$._id') STORED
PRIMARY KEY
) CHARSET utf8mb4;
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
81 / 110
82. Mapping to SQL examples
createCollection('mycollection')
CREATE TABLE `test`.`mycoll` (
doc JSON,
_id VARCHAR(32)
GENERATED ALWAYS AS (doc->>'$._id') STORED
PRIMARY KEY
) CHARSET utf8mb4;
mycollection.add({'test': 1234})
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
82 / 110
83. Mapping to SQL examples
createCollection('mycollection')
CREATE TABLE `test`.`mycoll` (
doc JSON,
_id VARCHAR(32)
GENERATED ALWAYS AS (doc->>'$._id') STORED
PRIMARY KEY
) CHARSET utf8mb4;
mycollection.add({'test': 1234})
INSERT INTO `test`.`mycoll` (doc)
VALUES (
JSON_OBJECT('_id','663807fe367ee6114e0e5458bdac28bf',
'test',1234)
);
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
83 / 110
84. Mapping to SQL examples (2)
mycollection.find("test > 100")
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
84 / 110
85. Mapping to SQL examples (2)
mycollection.find("test > 100")
SELECT doc
FROM `test`.`mycoll`
WHERE (JSON_EXTRACT(doc,'$.test') > 100);
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
85 / 110
86. SQL and JSON Example
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
86 / 110
87. SQL and JSON Example
same as:
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
87 / 110
88. SQL and JSON Example
same as:
we can use it in SQL:
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
88 / 110
89. SQL and JSON Example (2): validation
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
89 / 110
90. SQL and JSON Example (2): validation
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
90 / 110
91. SQL and JSON Example (2): validation
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
91 / 110
92. SQL and JSON Example (3): explain
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
92 / 110
93. SQL and JSON Example (3): explain
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
93 / 110
94. SQL and JSON Example (4): add index
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
94 / 110
95. SQL and JSON Example (4): add index
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
95 / 110
96. SQL and JSON Example (5): arrays
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
96 / 110
97. SQL and JSON Example (5): arrays
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
97 / 110
98. SQL and JSON Example (5): arrays
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
98 / 110
99. SQL and JSON Example (5): arrays
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
99 / 110
100. NoSQL as SQL
New in MySQL 8.0.3-labs: JSON_TABLE
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
100 / 110
101. NoSQL as SQL (2)
New in MySQL 8.0.3-labs: JSON_TABLE
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
101 / 110
102. SQL and NoSQL
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
102 / 110
103. SQL and NoSQL
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
103 / 110
104. SQL and NoSQL
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
104 / 110
105. SQL and NoSQL (2)
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
105 / 110
106. SQL and NoSQL (2)
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
106 / 110
107. SQL and NoSQL (2)
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
107 / 110
108. what do I gain ?
Conclusion
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
108 / 110
109. schemaless
flexible data structure
easy to start (CRUD)
Conclusion
This is the best of the two worlds in one product !
Data integrity
ACID Compliant
SQL
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
109 / 110
110. Thank you !
Any Questions ?
http://lefred.be
Copyright @ 2017 Oracle and/or its affiliates. All rights reserved.
110 / 110