SlideShare a Scribd company logo
1 of 56
Download to read offline
{ JSON }
in MySQL and MariaDB
Databases
Federico Razzoli
€ whoami
● Federico Razzoli
● Freelance consultant
● Working with databases since 2000
hello@federico-razzoli.com
federico-razzoli.com
● I worked as a consultant for Percona and Ibuildings
(mainly MySQL and MariaDB)
● I worked as a DBA for fast-growing companies like
Catawiki, HumanState, TransferWise
JSON Support
JSON Support
● MySQL 7.0 has a JSON type and JSON functions
● MariaDB 10.2 has JSON alias (LONGTEXT) and JSON functions
○ From MariaDB 10.4 JSON columns must contain valid JSON data
JSON Type Benefits
● Persons from both MySQL and MariaDB state that their approach is faster
○ Surprise, surprise!
● MySQL format is smaller
● When updating a property, MySQL does not rewrite the whole JSON
document
● MySQL binary log only contains the modified properties
○ binlog_row_value_options = 'PARTIAL_JSON'
● However, in MySQL 8, make sure that:
○ Default_tmp_storage_engine = 'TEMPTABLE'
JSON Features Comparison
● Let’s compare JSON features, and more generic features that are useful
working with JSON
● MySQL 8.0 / MariaDB 10.5
● MySQL only:
○ Index arrays (requires multi-valued indexes)
○ Schema validation
○ JSON_TABLE()
○ Functional index / Index on VIRTUAL column
● MariaDB only:
○ CONNECT (use a JSON file as a table)
○ Stored aggregate functions
Use Cases
JSON in relational databases?
● Relational databases are schemaful
● All rows must have the same columns (name, type)
○ (though NULL is often use to relax the schema)
● CHECK constraints:
○ email VARCHAR(100) CHECK (email LIKE '_%@_%._%')
○ birth_date DATE, death_date DATE, CHECK (birth_date <= death_date)
● UNIQUE constraint
● Referential constraints (foreign keys):
○ Each row in a child table matches a row in a parent table
JSON in relational databases?
● JSON is usually schemaless
● Two different objects can have different structures:
{
"product-type": "shirt",
"cost": 10.0,
"size": "M"
}
{
"product-type": "phone",
"cost": 69.99,
"size": [6.37, 2.9, 0.3],
"os": "Android"
}
JSON in relational databases?
● How would you store heterogeneous products in a relational database?
JSON in relational databases?
● How would you store heterogeneous products in a relational database?
● One table per product type?
○ Cannot enforce UNIQUE constraint
○ Try to find a product that could be of any type…
○ Try to get the min and max costs...
JSON in relational databases?
● How would you store heterogeneous products in a relational database?
● One table with one column for each property, NULL where it is not used?
○ Adding a product implies a slow, painful ALTER TABLE
○ If products type is deleted, obsolete columns tend to remain forever
○ Big table is bad for operations (backup, repair…)
○ INSERTs are slow
○ SELECT *
JSON in relational databases?
● How would you store heterogeneous products in a relational database?
● One main table with common properties, and one separate table for each
product type?
○ You can now see min/max costs, or find a product of any type
○ Many JOINs
JSON in relational databases?
● How would you store heterogeneous products in a relational database?
● Entity-Attribute-Value pattern (EAV)?
○ Store all data as texts
○ Crazy amount of JOINs
JSON in relational databases?
● How would you store heterogeneous products in a relational database?
● Single table, where all non-common property are stored as JSON
○ Still not perfect
○ But it’s good enough
JSON in relational databases?
CREATE TABLE product (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
type VARCHAR(50) NOT NULL,
name VARCHAR(50) NOT NULL,
cost DECIMAL(10, 2) NOT NULL,
quantity INT UNSIGNED NOT NULL,
attributes JSON NOT NULL
);
Indexing Properties
Materialising Properties
CREATE TABLE product (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
type VARCHAR(50),
name VARCHAR(50) NOT NULL,
cost DECIMAL(10, 2) NOT NULL,
quantity INT UNSIGNED NOT NULL,
attributes JSON NOT NULL,
colour VARCHAR(50) AS
(JSON_UNQUOTE(
JSON_EXTRACT(attributes, '$.colour')
)) STORED
);
MySQL shortcut
CREATE TABLE product (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
type VARCHAR(50),
name VARCHAR(50) NOT NULL,
cost DECIMAL(10, 2) NOT NULL,
quantity INT UNSIGNED NOT NULL,
attributes JSON NOT NULL,
colour VARCHAR(50) AS
(attributes->>'$.colour') VIRTUAL
);
Index on a Property
CREATE TABLE product (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
type VARCHAR(50),
name VARCHAR(50) NOT NULL,
cost DECIMAL(10, 2) NOT NULL,
quantity INT UNSIGNED NOT NULL,
attributes JSON NOT NULL,
colour VARCHAR(50) AS
(attributes->>'$.colour') VIRTUAL,
INDEX idx_colour (type, colour)
);
Index on a Property
SELECT id FROM product
WHERE type = 'FURNITURE'
AND colour = 'grey'
;
-- only uses the index in MySQL
SELECT id FROM product
WHERE type = 'FURNITURE'
AND attributes->>'$.colour' = 'grey'
;
Indexing Properties
CREATE TABLE product (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
type VARCHAR(50),
name VARCHAR(50) NOT NULL,
cost DECIMAL(10, 2) NOT NULL,
quantity INT UNSIGNED NOT NULL,
attributes JSON NOT NULL,
furniture_colour VARCHAR(50) AS
(IF(type = 'FURNITURE',
attributes->>'$.colour',
NULL
)) VIRTUAL,
INDEX idx_colour (furniture_colour)
Indexing Arrays
Indexing ARRAYS
● MySQL and MariaDB historically don’t support arrays
● Now they can use JSON arrays as “regular” arrays
● MySQL 8.0 supports Multi-Valued Indexes, that allow to index these arrays
○ Can be used to index JSON objects, not covered here
Indexing Arrays
CREATE TABLE `order` (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
customer_id INT UNSIGNED NOT NULL,
product_ids JSON NOT NULL,
INDEX idx_products (
(CAST(product_ids AS UNSIGNED ARRAY))
)
);
INSERT INTO `order` (customer_id, product_ids) VALUES
(24, JSON_ARRAY(10, 20, 30));
Indexing Arrays
SELECT DISTINCT customer_id
FROM `order`
WHERE 20 MEMBER OF (product_ids);
● JSON_CONTAINS()
● JSON_OVERLAPS()
Relationships
Foreign Keys on Properties
CREATE TABLE product (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
type VARCHAR(50),
...
furniture_colour INT UNSIGNED AS
(IF(type = 'FURNITURE',
attributes->>'$.colour',
NULL
)) STORED,
INDEX idx_furniture_colour (furniture_colour),
FOREIGN KEY fk_furniture_colour (furniture_colour)
REFERENCES furniture_colour (id)
);
Not suggesting to use Foreign Keys
● They come with several penalties
○ Slower writes
○ Propagated locks (for CASCADE FKs)
○ Limitations (only InnoDB, no partitions)
○ Cannot run safe online migrations
● Google for:
Federico Razzoli "Foreign Key bugs in MySQL and MariaDB"
But...
● But you can do the same without foreign keys
● You can easily add extra columns to furniture_colour
● You don’t have to mind about colour name details (case, extra spaces…)
● You can easily change a colour name
● You keep the JSON documents smaller
Basically, you can use the relational databases philosophy
While keeping some semi-structured data inside them
Validating JSON documents
Validating Against a JSON Schema
● Both MySQL and MariaDB only accept valid JSON documents in JSON
columns
● The CHECK clause defines the rules that determine if a row is valid
● JSON_SCHEMA_VALID() in MySQL validates a JSON document against a
JSON Schema
○ json-schema.org
○ Draft 4
○ But from my tests we can use drafts 6 or 7
Validating Schema
SELECT JSON_SCHEMA_VALID(@schema, @document);
SELECT JSON_SCHEMA_VALID((
SELECT schema
FROM schemas
WHERE type = 'furniture'
), document)
FROM product
WHERE id = 24
;
Validating Schema
Suppose we want to validate objects of this type:
{
"material": "iron",
"colour": "grey",
"size": [1.5, 1.5, 1.0]
}
{
"$id": "https://federico-razzoli.com/schemas/furniture",
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"material": {
"type": "string",
"enum": ["iron", "wood"]
},
"size": {
"type": "array",
"items": {
"type": "number",
"minItems": 3,
"maxItems": 3
}
},
"colour": {
"type": "string"
}
},
"required": ["material", "size"]
}
Debugging a JSON Schema
● SELECT JSON_SCHEMA_VALID() + SHOW WARNINGS
● JSON_SCHEMA_VALIDATION_REPORT()
Default Properties
Default Properties
● We can have a JSON document with the default properties
● And only store non-default values in each regular JSON document
● This is useful:
○ For large JSON objects (eg. software configuration)
○ For objects whose defaults may change (eg. software whose settings can
be set per user and per team)
Merging Documents
-- get user settings
SET @user_conf := (
SELECT json_settings FROM user_configuration
WHERE id = 24
);
-- get team settings
SET @team_conf := (
SELECT json_settings FROM team_configuration
WHERE id = 123
);
-- merge them. user settings overwrite team settings
SELECT JSON_MERGE_PATCH(@team_conf, @user_conf);
JSON_MERGE_PATCH() example
mysql> SELECT JSON_MERGE_PATCH(
'{"items-per-page": 24, "suppress_warnings": true}',
'{"suppress_warnings": false}')
AS configuration;
+----------------------------------------------------+
| configuration |
+----------------------------------------------------+
| {"items-per-page": 24, "suppress_warnings": false} |
+----------------------------------------------------+
JSON_ARRAYAGG()
JSON_OBJECTAGG()
JSON_ARRAYAGG(),
JSON_OBJECTAGG()
● Available in:
○ MySQL 5.7
○ MariaDB 10.5, currently not GA
● However, they are:
○ Simple to emulate with GROUP_CONCAT()
○ Easy to code with MariaDB Stored Aggregate Functions (10.3)
JSON_ARRAYAGG()
mysql> SELECT type, JSON_ARRAYAGG(colour) AS colours
-> FROM product
-> GROUP BY type
-> ORDER BY type;
+-------+-----------------------------------+
| type | colours |
+-------+-----------------------------------+
| chair | ["green", "blue", "red", "black"] |
| shelf | ["white", "brown"] |
| table | ["white", "gray", "black"] |
+-------+-----------------------------------+
JSON_OBJECTAGG(): one document
mysql> SELECT
-> JSON_PRETTY(
-> JSON_OBJECTAGG(variable, value)
-> ) AS settings
-> FROM config G
*************************** 1. row ***************************
settings: {
"datadir": "/var/mysql/data",
"log_bin": "1",
"general_log": "0",
"slow_query_log": "0",
"innodb_log_file_size": "1G",
"innodb_log_buffer_size": "16M",
"innodb_buffer_pool_size": "16G"
}
JSON_OBJECTAGG()
nested objects
mysql> SELECT
-> JSON_PRETTY(
-> JSON_OBJECTAGG(
-> variable,
-> JSON_OBJECT(
-> 'type', type,
-> 'value', value
-> )
-> )
-> ) AS settings
-> FROM config;
JSON_OBJECTAGG()
nested objects
settings: {
"datadir": {
"type": "string",
"value": "/var/mysql/data"
},
"log_bin": {
"type": "bool",
"value": "1"
},
...
}
JSON_OBJECTAGG():
one document per row
mysql> SELECT JSON_OBJECTAGG(variable, value) AS settings
-> FROM config
-> GROUP BY variable;
+------------------------------------+
| settings |
+------------------------------------+
| {"datadir": "/var/mysql/data"} |
| {"general_log": "0"} |
| {"innodb_buffer_pool_size": "16G"} |
| {"innodb_log_buffer_size": "16M"} |
| {"innodb_log_file_size": "1G"} |
| {"log_bin": "1"} |
| {"slow_query_log": "0"} |
+------------------------------------+
JSON_TABLE()
JSON_TABLE()
● Only MySQL 8
● Transforms a JSON document into a table
● Each property, or some properties, become a table column
● Properties are identified by JSON Path expressions
JSON_TABLE() example
{
"customers": [
{
"name": "John Doe",
"emails": ["john@doe.com"],
"phone": "123456"
},
{
"name": "Jane Dee",
"emails": ["Jane@dee.com"],
"phone": "987654"
},
{
"name": "Donald Duck",
"emails": []
}
],
"products": [],
"orders": []
}
JSON_TABLE() example
SELECT *
FROM JSON_TABLE(
@customers,
'$.customers[*]'
COLUMNS (
id FOR ORDINALITY,
name VARCHAR(50) PATH '$.name',
primary_email VARCHAR(50) PATH '$.emails[0]',
phone VARCHAR(50) PATH '$.phone'
DEFAULT '"UNKNOWN"' ON EMPTY
NULL ON ERROR
)
) AS customers;
JSON_TABLE() example
+------+-------------+---------------+---------+
| id | name | primary_email | phone |
+------+-------------+---------------+---------+
| 1 | John Doe | john@doe.com | 123456 |
| 2 | Jane Dee | Jane@dee.com | 987654 |
| 3 | Donald Duck | NULL | UNKNOWN |
+------+-------------+---------------+---------+
MariaDB CONNECT
What CONNECT is
● Connect is a storage engine for MariaDB
○ Won’t compile on MySQL or Percona Server
● Reads data from different data sources:
○ Remote tables (MySQL protocol, ODBC, JDBC)
○ Files in various formats (JSON, XML, CSV, custom formats…)
○ Special data sources
○ Transformed data from other tables (pivot, …)
CONNECT and JSON
● JSON Table Type (10.1)
● On Windows, can read data from REST APIs (not easy to do)
● CONNECT comes with a library of JSON UDFs
○ Probably not battle-tested as the current built-in functions
Thank you for listening!
Federico
hello@federico-razzoli.com

More Related Content

What's hot

Modern Application Foundations: Underscore and Twitter Bootstrap
Modern Application Foundations: Underscore and Twitter BootstrapModern Application Foundations: Underscore and Twitter Bootstrap
Modern Application Foundations: Underscore and Twitter BootstrapHoward Lewis Ship
 
Embedding a language into string interpolator
Embedding a language into string interpolatorEmbedding a language into string interpolator
Embedding a language into string interpolatorMichael Limansky
 
DataMapper @ RubyEnRails2009
DataMapper @ RubyEnRails2009DataMapper @ RubyEnRails2009
DataMapper @ RubyEnRails2009Dirkjan Bussink
 
Improving RDF Search Performance with Lucene and SIREN
Improving RDF Search Performance with Lucene and SIRENImproving RDF Search Performance with Lucene and SIREN
Improving RDF Search Performance with Lucene and SIRENMike Hugo
 
Modeling for Performance
Modeling for PerformanceModeling for Performance
Modeling for PerformanceMongoDB
 
Data Modeling for Performance
Data Modeling for PerformanceData Modeling for Performance
Data Modeling for PerformanceMichael Dwan
 
Propel sfugmd
Propel sfugmdPropel sfugmd
Propel sfugmdiKlaus
 

What's hot (14)

Modern Application Foundations: Underscore and Twitter Bootstrap
Modern Application Foundations: Underscore and Twitter BootstrapModern Application Foundations: Underscore and Twitter Bootstrap
Modern Application Foundations: Underscore and Twitter Bootstrap
 
Embedding a language into string interpolator
Embedding a language into string interpolatorEmbedding a language into string interpolator
Embedding a language into string interpolator
 
Service Oriented Architecture-Unit-1-XML Schema
Service Oriented Architecture-Unit-1-XML SchemaService Oriented Architecture-Unit-1-XML Schema
Service Oriented Architecture-Unit-1-XML Schema
 
Advanced MongoDB #1
Advanced MongoDB #1Advanced MongoDB #1
Advanced MongoDB #1
 
DataMapper @ RubyEnRails2009
DataMapper @ RubyEnRails2009DataMapper @ RubyEnRails2009
DataMapper @ RubyEnRails2009
 
Hands on JSON
Hands on JSONHands on JSON
Hands on JSON
 
SQL & PLSQL
SQL & PLSQLSQL & PLSQL
SQL & PLSQL
 
Improving RDF Search Performance with Lucene and SIREN
Improving RDF Search Performance with Lucene and SIRENImproving RDF Search Performance with Lucene and SIREN
Improving RDF Search Performance with Lucene and SIREN
 
mondrian-olap JRuby library
mondrian-olap JRuby librarymondrian-olap JRuby library
mondrian-olap JRuby library
 
Modeling for Performance
Modeling for PerformanceModeling for Performance
Modeling for Performance
 
Data Modeling for Performance
Data Modeling for PerformanceData Modeling for Performance
Data Modeling for Performance
 
Propel sfugmd
Propel sfugmdPropel sfugmd
Propel sfugmd
 
Sql commands
Sql commandsSql commands
Sql commands
 
Hibernate
HibernateHibernate
Hibernate
 

Similar to JSON in MySQL and MariaDB Databases

Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDBMike Dirolf
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDBantoinegirbal
 
2011 Mongo FR - MongoDB introduction
2011 Mongo FR - MongoDB introduction2011 Mongo FR - MongoDB introduction
2011 Mongo FR - MongoDB introductionantoinegirbal
 
Conceptos básicos. Seminario web 4: Indexación avanzada, índices de texto y g...
Conceptos básicos. Seminario web 4: Indexación avanzada, índices de texto y g...Conceptos básicos. Seminario web 4: Indexación avanzada, índices de texto y g...
Conceptos básicos. Seminario web 4: Indexación avanzada, índices de texto y g...MongoDB
 
MongoDB .local Chicago 2019: Practical Data Modeling for MongoDB: Tutorial
MongoDB .local Chicago 2019: Practical Data Modeling for MongoDB: TutorialMongoDB .local Chicago 2019: Practical Data Modeling for MongoDB: Tutorial
MongoDB .local Chicago 2019: Practical Data Modeling for MongoDB: TutorialMongoDB
 
UKOUG Tech14 - Getting Started With JSON in the Database
UKOUG Tech14 - Getting Started With JSON in the DatabaseUKOUG Tech14 - Getting Started With JSON in the Database
UKOUG Tech14 - Getting Started With JSON in the DatabaseMarco Gralike
 
MongoDB, PHP and the cloud - php cloud summit 2011
MongoDB, PHP and the cloud - php cloud summit 2011MongoDB, PHP and the cloud - php cloud summit 2011
MongoDB, PHP and the cloud - php cloud summit 2011Steven Francia
 
MySQL without the SQL -- Cascadia PHP
MySQL without the SQL -- Cascadia PHPMySQL without the SQL -- Cascadia PHP
MySQL without the SQL -- Cascadia PHPDave Stokes
 
10gen Presents Schema Design and Data Modeling
10gen Presents Schema Design and Data Modeling10gen Presents Schema Design and Data Modeling
10gen Presents Schema Design and Data ModelingDATAVERSITY
 
Marc s01 e02-crud-database
Marc s01 e02-crud-databaseMarc s01 e02-crud-database
Marc s01 e02-crud-databaseMongoDB
 
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...MongoDB
 
Aggregation Framework MongoDB Days Munich
Aggregation Framework MongoDB Days MunichAggregation Framework MongoDB Days Munich
Aggregation Framework MongoDB Days MunichNorberto Leite
 
Oracle Database - JSON and the In-Memory Database
Oracle Database - JSON and the In-Memory DatabaseOracle Database - JSON and the In-Memory Database
Oracle Database - JSON and the In-Memory DatabaseMarco Gralike
 
Agile Database Development with JSON
Agile Database Development with JSONAgile Database Development with JSON
Agile Database Development with JSONChris Saxon
 
MongoDB.local DC 2018: Tutorial - Data Analytics with MongoDB
MongoDB.local DC 2018: Tutorial - Data Analytics with MongoDBMongoDB.local DC 2018: Tutorial - Data Analytics with MongoDB
MongoDB.local DC 2018: Tutorial - Data Analytics with MongoDBMongoDB
 
Benefits of using MongoDB: Reduce Complexity & Adapt to Changes
Benefits of using MongoDB: Reduce Complexity & Adapt to ChangesBenefits of using MongoDB: Reduce Complexity & Adapt to Changes
Benefits of using MongoDB: Reduce Complexity & Adapt to ChangesAlex Nguyen
 
MySQL Without The SQL -- Oh My! PHP Detroit July 2018
MySQL Without The SQL -- Oh My! PHP Detroit July 2018MySQL Without The SQL -- Oh My! PHP Detroit July 2018
MySQL Without The SQL -- Oh My! PHP Detroit July 2018Dave Stokes
 
Query for json databases
Query for json databasesQuery for json databases
Query for json databasesBinh Le
 

Similar to JSON in MySQL and MariaDB Databases (20)

Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
2011 Mongo FR - MongoDB introduction
2011 Mongo FR - MongoDB introduction2011 Mongo FR - MongoDB introduction
2011 Mongo FR - MongoDB introduction
 
How to Use JSON in MySQL Wrong
How to Use JSON in MySQL WrongHow to Use JSON in MySQL Wrong
How to Use JSON in MySQL Wrong
 
Conceptos básicos. Seminario web 4: Indexación avanzada, índices de texto y g...
Conceptos básicos. Seminario web 4: Indexación avanzada, índices de texto y g...Conceptos básicos. Seminario web 4: Indexación avanzada, índices de texto y g...
Conceptos básicos. Seminario web 4: Indexación avanzada, índices de texto y g...
 
MongoDB .local Chicago 2019: Practical Data Modeling for MongoDB: Tutorial
MongoDB .local Chicago 2019: Practical Data Modeling for MongoDB: TutorialMongoDB .local Chicago 2019: Practical Data Modeling for MongoDB: Tutorial
MongoDB .local Chicago 2019: Practical Data Modeling for MongoDB: Tutorial
 
UKOUG Tech14 - Getting Started With JSON in the Database
UKOUG Tech14 - Getting Started With JSON in the DatabaseUKOUG Tech14 - Getting Started With JSON in the Database
UKOUG Tech14 - Getting Started With JSON in the Database
 
MongoDB, PHP and the cloud - php cloud summit 2011
MongoDB, PHP and the cloud - php cloud summit 2011MongoDB, PHP and the cloud - php cloud summit 2011
MongoDB, PHP and the cloud - php cloud summit 2011
 
MySQL without the SQL -- Cascadia PHP
MySQL without the SQL -- Cascadia PHPMySQL without the SQL -- Cascadia PHP
MySQL without the SQL -- Cascadia PHP
 
10gen Presents Schema Design and Data Modeling
10gen Presents Schema Design and Data Modeling10gen Presents Schema Design and Data Modeling
10gen Presents Schema Design and Data Modeling
 
Marc s01 e02-crud-database
Marc s01 e02-crud-databaseMarc s01 e02-crud-database
Marc s01 e02-crud-database
 
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...
 
Aggregation Framework MongoDB Days Munich
Aggregation Framework MongoDB Days MunichAggregation Framework MongoDB Days Munich
Aggregation Framework MongoDB Days Munich
 
Oracle Database - JSON and the In-Memory Database
Oracle Database - JSON and the In-Memory DatabaseOracle Database - JSON and the In-Memory Database
Oracle Database - JSON and the In-Memory Database
 
Agile Database Development with JSON
Agile Database Development with JSONAgile Database Development with JSON
Agile Database Development with JSON
 
MongoDB.local DC 2018: Tutorial - Data Analytics with MongoDB
MongoDB.local DC 2018: Tutorial - Data Analytics with MongoDBMongoDB.local DC 2018: Tutorial - Data Analytics with MongoDB
MongoDB.local DC 2018: Tutorial - Data Analytics with MongoDB
 
Benefits of using MongoDB: Reduce Complexity & Adapt to Changes
Benefits of using MongoDB: Reduce Complexity & Adapt to ChangesBenefits of using MongoDB: Reduce Complexity & Adapt to Changes
Benefits of using MongoDB: Reduce Complexity & Adapt to Changes
 
MySQL Without The SQL -- Oh My! PHP Detroit July 2018
MySQL Without The SQL -- Oh My! PHP Detroit July 2018MySQL Without The SQL -- Oh My! PHP Detroit July 2018
MySQL Without The SQL -- Oh My! PHP Detroit July 2018
 
MongoDB Meetup
MongoDB MeetupMongoDB Meetup
MongoDB Meetup
 
Query for json databases
Query for json databasesQuery for json databases
Query for json databases
 

More from Federico Razzoli

Webinar - Unleash AI power with MySQL and MindsDB
Webinar - Unleash AI power with MySQL and MindsDBWebinar - Unleash AI power with MySQL and MindsDB
Webinar - Unleash AI power with MySQL and MindsDBFederico Razzoli
 
MariaDB Security Best Practices
MariaDB Security Best PracticesMariaDB Security Best Practices
MariaDB Security Best PracticesFederico Razzoli
 
A first look at MariaDB 11.x features and ideas on how to use them
A first look at MariaDB 11.x features and ideas on how to use themA first look at MariaDB 11.x features and ideas on how to use them
A first look at MariaDB 11.x features and ideas on how to use themFederico Razzoli
 
MariaDB stored procedures and why they should be improved
MariaDB stored procedures and why they should be improvedMariaDB stored procedures and why they should be improved
MariaDB stored procedures and why they should be improvedFederico Razzoli
 
Webinar - MariaDB Temporal Tables: a demonstration
Webinar - MariaDB Temporal Tables: a demonstrationWebinar - MariaDB Temporal Tables: a demonstration
Webinar - MariaDB Temporal Tables: a demonstrationFederico Razzoli
 
Webinar - Key Reasons to Upgrade to MySQL 8.0 or MariaDB 10.11
Webinar - Key Reasons to Upgrade to MySQL 8.0 or MariaDB 10.11Webinar - Key Reasons to Upgrade to MySQL 8.0 or MariaDB 10.11
Webinar - Key Reasons to Upgrade to MySQL 8.0 or MariaDB 10.11Federico Razzoli
 
MariaDB 10.11 key features overview for DBAs
MariaDB 10.11 key features overview for DBAsMariaDB 10.11 key features overview for DBAs
MariaDB 10.11 key features overview for DBAsFederico Razzoli
 
Recent MariaDB features to learn for a happy life
Recent MariaDB features to learn for a happy lifeRecent MariaDB features to learn for a happy life
Recent MariaDB features to learn for a happy lifeFederico Razzoli
 
Advanced MariaDB features that developers love.pdf
Advanced MariaDB features that developers love.pdfAdvanced MariaDB features that developers love.pdf
Advanced MariaDB features that developers love.pdfFederico Razzoli
 
Automate MariaDB Galera clusters deployments with Ansible
Automate MariaDB Galera clusters deployments with AnsibleAutomate MariaDB Galera clusters deployments with Ansible
Automate MariaDB Galera clusters deployments with AnsibleFederico Razzoli
 
Creating Vagrant development machines with MariaDB
Creating Vagrant development machines with MariaDBCreating Vagrant development machines with MariaDB
Creating Vagrant development machines with MariaDBFederico Razzoli
 
MariaDB, MySQL and Ansible: automating database infrastructures
MariaDB, MySQL and Ansible: automating database infrastructuresMariaDB, MySQL and Ansible: automating database infrastructures
MariaDB, MySQL and Ansible: automating database infrastructuresFederico Razzoli
 
Playing with the CONNECT storage engine
Playing with the CONNECT storage enginePlaying with the CONNECT storage engine
Playing with the CONNECT storage engineFederico Razzoli
 
Database Design most common pitfalls
Database Design most common pitfallsDatabase Design most common pitfalls
Database Design most common pitfallsFederico Razzoli
 
How MySQL can boost (or kill) your application v2
How MySQL can boost (or kill) your application v2How MySQL can boost (or kill) your application v2
How MySQL can boost (or kill) your application v2Federico Razzoli
 
MySQL Transaction Isolation Levels (lightning talk)
MySQL Transaction Isolation Levels (lightning talk)MySQL Transaction Isolation Levels (lightning talk)
MySQL Transaction Isolation Levels (lightning talk)Federico Razzoli
 
Cassandra sharding and consistency (lightning talk)
Cassandra sharding and consistency (lightning talk)Cassandra sharding and consistency (lightning talk)
Cassandra sharding and consistency (lightning talk)Federico Razzoli
 

More from Federico Razzoli (20)

Webinar - Unleash AI power with MySQL and MindsDB
Webinar - Unleash AI power with MySQL and MindsDBWebinar - Unleash AI power with MySQL and MindsDB
Webinar - Unleash AI power with MySQL and MindsDB
 
MariaDB Security Best Practices
MariaDB Security Best PracticesMariaDB Security Best Practices
MariaDB Security Best Practices
 
A first look at MariaDB 11.x features and ideas on how to use them
A first look at MariaDB 11.x features and ideas on how to use themA first look at MariaDB 11.x features and ideas on how to use them
A first look at MariaDB 11.x features and ideas on how to use them
 
MariaDB stored procedures and why they should be improved
MariaDB stored procedures and why they should be improvedMariaDB stored procedures and why they should be improved
MariaDB stored procedures and why they should be improved
 
Webinar - MariaDB Temporal Tables: a demonstration
Webinar - MariaDB Temporal Tables: a demonstrationWebinar - MariaDB Temporal Tables: a demonstration
Webinar - MariaDB Temporal Tables: a demonstration
 
Webinar - Key Reasons to Upgrade to MySQL 8.0 or MariaDB 10.11
Webinar - Key Reasons to Upgrade to MySQL 8.0 or MariaDB 10.11Webinar - Key Reasons to Upgrade to MySQL 8.0 or MariaDB 10.11
Webinar - Key Reasons to Upgrade to MySQL 8.0 or MariaDB 10.11
 
MariaDB 10.11 key features overview for DBAs
MariaDB 10.11 key features overview for DBAsMariaDB 10.11 key features overview for DBAs
MariaDB 10.11 key features overview for DBAs
 
Recent MariaDB features to learn for a happy life
Recent MariaDB features to learn for a happy lifeRecent MariaDB features to learn for a happy life
Recent MariaDB features to learn for a happy life
 
Advanced MariaDB features that developers love.pdf
Advanced MariaDB features that developers love.pdfAdvanced MariaDB features that developers love.pdf
Advanced MariaDB features that developers love.pdf
 
Automate MariaDB Galera clusters deployments with Ansible
Automate MariaDB Galera clusters deployments with AnsibleAutomate MariaDB Galera clusters deployments with Ansible
Automate MariaDB Galera clusters deployments with Ansible
 
Creating Vagrant development machines with MariaDB
Creating Vagrant development machines with MariaDBCreating Vagrant development machines with MariaDB
Creating Vagrant development machines with MariaDB
 
MariaDB, MySQL and Ansible: automating database infrastructures
MariaDB, MySQL and Ansible: automating database infrastructuresMariaDB, MySQL and Ansible: automating database infrastructures
MariaDB, MySQL and Ansible: automating database infrastructures
 
Playing with the CONNECT storage engine
Playing with the CONNECT storage enginePlaying with the CONNECT storage engine
Playing with the CONNECT storage engine
 
MariaDB Temporal Tables
MariaDB Temporal TablesMariaDB Temporal Tables
MariaDB Temporal Tables
 
Database Design most common pitfalls
Database Design most common pitfallsDatabase Design most common pitfalls
Database Design most common pitfalls
 
MySQL and MariaDB Backups
MySQL and MariaDB BackupsMySQL and MariaDB Backups
MySQL and MariaDB Backups
 
How MySQL can boost (or kill) your application v2
How MySQL can boost (or kill) your application v2How MySQL can boost (or kill) your application v2
How MySQL can boost (or kill) your application v2
 
MySQL Transaction Isolation Levels (lightning talk)
MySQL Transaction Isolation Levels (lightning talk)MySQL Transaction Isolation Levels (lightning talk)
MySQL Transaction Isolation Levels (lightning talk)
 
Cassandra sharding and consistency (lightning talk)
Cassandra sharding and consistency (lightning talk)Cassandra sharding and consistency (lightning talk)
Cassandra sharding and consistency (lightning talk)
 
MariaDB Temporal Tables
MariaDB Temporal TablesMariaDB Temporal Tables
MariaDB Temporal Tables
 

Recently uploaded

Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Mater
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 

Recently uploaded (20)

Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 

JSON in MySQL and MariaDB Databases

  • 1. { JSON } in MySQL and MariaDB Databases Federico Razzoli
  • 2. € whoami ● Federico Razzoli ● Freelance consultant ● Working with databases since 2000 hello@federico-razzoli.com federico-razzoli.com ● I worked as a consultant for Percona and Ibuildings (mainly MySQL and MariaDB) ● I worked as a DBA for fast-growing companies like Catawiki, HumanState, TransferWise
  • 4. JSON Support ● MySQL 7.0 has a JSON type and JSON functions ● MariaDB 10.2 has JSON alias (LONGTEXT) and JSON functions ○ From MariaDB 10.4 JSON columns must contain valid JSON data
  • 5. JSON Type Benefits ● Persons from both MySQL and MariaDB state that their approach is faster ○ Surprise, surprise! ● MySQL format is smaller ● When updating a property, MySQL does not rewrite the whole JSON document ● MySQL binary log only contains the modified properties ○ binlog_row_value_options = 'PARTIAL_JSON' ● However, in MySQL 8, make sure that: ○ Default_tmp_storage_engine = 'TEMPTABLE'
  • 6. JSON Features Comparison ● Let’s compare JSON features, and more generic features that are useful working with JSON ● MySQL 8.0 / MariaDB 10.5 ● MySQL only: ○ Index arrays (requires multi-valued indexes) ○ Schema validation ○ JSON_TABLE() ○ Functional index / Index on VIRTUAL column ● MariaDB only: ○ CONNECT (use a JSON file as a table) ○ Stored aggregate functions
  • 8. JSON in relational databases? ● Relational databases are schemaful ● All rows must have the same columns (name, type) ○ (though NULL is often use to relax the schema) ● CHECK constraints: ○ email VARCHAR(100) CHECK (email LIKE '_%@_%._%') ○ birth_date DATE, death_date DATE, CHECK (birth_date <= death_date) ● UNIQUE constraint ● Referential constraints (foreign keys): ○ Each row in a child table matches a row in a parent table
  • 9. JSON in relational databases? ● JSON is usually schemaless ● Two different objects can have different structures: { "product-type": "shirt", "cost": 10.0, "size": "M" } { "product-type": "phone", "cost": 69.99, "size": [6.37, 2.9, 0.3], "os": "Android" }
  • 10. JSON in relational databases? ● How would you store heterogeneous products in a relational database?
  • 11. JSON in relational databases? ● How would you store heterogeneous products in a relational database? ● One table per product type? ○ Cannot enforce UNIQUE constraint ○ Try to find a product that could be of any type… ○ Try to get the min and max costs...
  • 12. JSON in relational databases? ● How would you store heterogeneous products in a relational database? ● One table with one column for each property, NULL where it is not used? ○ Adding a product implies a slow, painful ALTER TABLE ○ If products type is deleted, obsolete columns tend to remain forever ○ Big table is bad for operations (backup, repair…) ○ INSERTs are slow ○ SELECT *
  • 13. JSON in relational databases? ● How would you store heterogeneous products in a relational database? ● One main table with common properties, and one separate table for each product type? ○ You can now see min/max costs, or find a product of any type ○ Many JOINs
  • 14. JSON in relational databases? ● How would you store heterogeneous products in a relational database? ● Entity-Attribute-Value pattern (EAV)? ○ Store all data as texts ○ Crazy amount of JOINs
  • 15. JSON in relational databases? ● How would you store heterogeneous products in a relational database? ● Single table, where all non-common property are stored as JSON ○ Still not perfect ○ But it’s good enough
  • 16. JSON in relational databases? CREATE TABLE product ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, type VARCHAR(50) NOT NULL, name VARCHAR(50) NOT NULL, cost DECIMAL(10, 2) NOT NULL, quantity INT UNSIGNED NOT NULL, attributes JSON NOT NULL );
  • 18. Materialising Properties CREATE TABLE product ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, type VARCHAR(50), name VARCHAR(50) NOT NULL, cost DECIMAL(10, 2) NOT NULL, quantity INT UNSIGNED NOT NULL, attributes JSON NOT NULL, colour VARCHAR(50) AS (JSON_UNQUOTE( JSON_EXTRACT(attributes, '$.colour') )) STORED );
  • 19. MySQL shortcut CREATE TABLE product ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, type VARCHAR(50), name VARCHAR(50) NOT NULL, cost DECIMAL(10, 2) NOT NULL, quantity INT UNSIGNED NOT NULL, attributes JSON NOT NULL, colour VARCHAR(50) AS (attributes->>'$.colour') VIRTUAL );
  • 20. Index on a Property CREATE TABLE product ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, type VARCHAR(50), name VARCHAR(50) NOT NULL, cost DECIMAL(10, 2) NOT NULL, quantity INT UNSIGNED NOT NULL, attributes JSON NOT NULL, colour VARCHAR(50) AS (attributes->>'$.colour') VIRTUAL, INDEX idx_colour (type, colour) );
  • 21. Index on a Property SELECT id FROM product WHERE type = 'FURNITURE' AND colour = 'grey' ; -- only uses the index in MySQL SELECT id FROM product WHERE type = 'FURNITURE' AND attributes->>'$.colour' = 'grey' ;
  • 22. Indexing Properties CREATE TABLE product ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, type VARCHAR(50), name VARCHAR(50) NOT NULL, cost DECIMAL(10, 2) NOT NULL, quantity INT UNSIGNED NOT NULL, attributes JSON NOT NULL, furniture_colour VARCHAR(50) AS (IF(type = 'FURNITURE', attributes->>'$.colour', NULL )) VIRTUAL, INDEX idx_colour (furniture_colour)
  • 24. Indexing ARRAYS ● MySQL and MariaDB historically don’t support arrays ● Now they can use JSON arrays as “regular” arrays ● MySQL 8.0 supports Multi-Valued Indexes, that allow to index these arrays ○ Can be used to index JSON objects, not covered here
  • 25. Indexing Arrays CREATE TABLE `order` ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, customer_id INT UNSIGNED NOT NULL, product_ids JSON NOT NULL, INDEX idx_products ( (CAST(product_ids AS UNSIGNED ARRAY)) ) ); INSERT INTO `order` (customer_id, product_ids) VALUES (24, JSON_ARRAY(10, 20, 30));
  • 26. Indexing Arrays SELECT DISTINCT customer_id FROM `order` WHERE 20 MEMBER OF (product_ids); ● JSON_CONTAINS() ● JSON_OVERLAPS()
  • 28. Foreign Keys on Properties CREATE TABLE product ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, type VARCHAR(50), ... furniture_colour INT UNSIGNED AS (IF(type = 'FURNITURE', attributes->>'$.colour', NULL )) STORED, INDEX idx_furniture_colour (furniture_colour), FOREIGN KEY fk_furniture_colour (furniture_colour) REFERENCES furniture_colour (id) );
  • 29. Not suggesting to use Foreign Keys ● They come with several penalties ○ Slower writes ○ Propagated locks (for CASCADE FKs) ○ Limitations (only InnoDB, no partitions) ○ Cannot run safe online migrations ● Google for: Federico Razzoli "Foreign Key bugs in MySQL and MariaDB"
  • 30. But... ● But you can do the same without foreign keys ● You can easily add extra columns to furniture_colour ● You don’t have to mind about colour name details (case, extra spaces…) ● You can easily change a colour name ● You keep the JSON documents smaller Basically, you can use the relational databases philosophy While keeping some semi-structured data inside them
  • 32. Validating Against a JSON Schema ● Both MySQL and MariaDB only accept valid JSON documents in JSON columns ● The CHECK clause defines the rules that determine if a row is valid ● JSON_SCHEMA_VALID() in MySQL validates a JSON document against a JSON Schema ○ json-schema.org ○ Draft 4 ○ But from my tests we can use drafts 6 or 7
  • 33. Validating Schema SELECT JSON_SCHEMA_VALID(@schema, @document); SELECT JSON_SCHEMA_VALID(( SELECT schema FROM schemas WHERE type = 'furniture' ), document) FROM product WHERE id = 24 ;
  • 34. Validating Schema Suppose we want to validate objects of this type: { "material": "iron", "colour": "grey", "size": [1.5, 1.5, 1.0] }
  • 35. { "$id": "https://federico-razzoli.com/schemas/furniture", "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "properties": { "material": { "type": "string", "enum": ["iron", "wood"] }, "size": { "type": "array", "items": { "type": "number", "minItems": 3, "maxItems": 3 } }, "colour": { "type": "string" } }, "required": ["material", "size"] }
  • 36. Debugging a JSON Schema ● SELECT JSON_SCHEMA_VALID() + SHOW WARNINGS ● JSON_SCHEMA_VALIDATION_REPORT()
  • 38. Default Properties ● We can have a JSON document with the default properties ● And only store non-default values in each regular JSON document ● This is useful: ○ For large JSON objects (eg. software configuration) ○ For objects whose defaults may change (eg. software whose settings can be set per user and per team)
  • 39. Merging Documents -- get user settings SET @user_conf := ( SELECT json_settings FROM user_configuration WHERE id = 24 ); -- get team settings SET @team_conf := ( SELECT json_settings FROM team_configuration WHERE id = 123 ); -- merge them. user settings overwrite team settings SELECT JSON_MERGE_PATCH(@team_conf, @user_conf);
  • 40. JSON_MERGE_PATCH() example mysql> SELECT JSON_MERGE_PATCH( '{"items-per-page": 24, "suppress_warnings": true}', '{"suppress_warnings": false}') AS configuration; +----------------------------------------------------+ | configuration | +----------------------------------------------------+ | {"items-per-page": 24, "suppress_warnings": false} | +----------------------------------------------------+
  • 42. JSON_ARRAYAGG(), JSON_OBJECTAGG() ● Available in: ○ MySQL 5.7 ○ MariaDB 10.5, currently not GA ● However, they are: ○ Simple to emulate with GROUP_CONCAT() ○ Easy to code with MariaDB Stored Aggregate Functions (10.3)
  • 43. JSON_ARRAYAGG() mysql> SELECT type, JSON_ARRAYAGG(colour) AS colours -> FROM product -> GROUP BY type -> ORDER BY type; +-------+-----------------------------------+ | type | colours | +-------+-----------------------------------+ | chair | ["green", "blue", "red", "black"] | | shelf | ["white", "brown"] | | table | ["white", "gray", "black"] | +-------+-----------------------------------+
  • 44. JSON_OBJECTAGG(): one document mysql> SELECT -> JSON_PRETTY( -> JSON_OBJECTAGG(variable, value) -> ) AS settings -> FROM config G *************************** 1. row *************************** settings: { "datadir": "/var/mysql/data", "log_bin": "1", "general_log": "0", "slow_query_log": "0", "innodb_log_file_size": "1G", "innodb_log_buffer_size": "16M", "innodb_buffer_pool_size": "16G" }
  • 45. JSON_OBJECTAGG() nested objects mysql> SELECT -> JSON_PRETTY( -> JSON_OBJECTAGG( -> variable, -> JSON_OBJECT( -> 'type', type, -> 'value', value -> ) -> ) -> ) AS settings -> FROM config;
  • 46. JSON_OBJECTAGG() nested objects settings: { "datadir": { "type": "string", "value": "/var/mysql/data" }, "log_bin": { "type": "bool", "value": "1" }, ... }
  • 47. JSON_OBJECTAGG(): one document per row mysql> SELECT JSON_OBJECTAGG(variable, value) AS settings -> FROM config -> GROUP BY variable; +------------------------------------+ | settings | +------------------------------------+ | {"datadir": "/var/mysql/data"} | | {"general_log": "0"} | | {"innodb_buffer_pool_size": "16G"} | | {"innodb_log_buffer_size": "16M"} | | {"innodb_log_file_size": "1G"} | | {"log_bin": "1"} | | {"slow_query_log": "0"} | +------------------------------------+
  • 49. JSON_TABLE() ● Only MySQL 8 ● Transforms a JSON document into a table ● Each property, or some properties, become a table column ● Properties are identified by JSON Path expressions
  • 50. JSON_TABLE() example { "customers": [ { "name": "John Doe", "emails": ["john@doe.com"], "phone": "123456" }, { "name": "Jane Dee", "emails": ["Jane@dee.com"], "phone": "987654" }, { "name": "Donald Duck", "emails": [] } ], "products": [], "orders": [] }
  • 51. JSON_TABLE() example SELECT * FROM JSON_TABLE( @customers, '$.customers[*]' COLUMNS ( id FOR ORDINALITY, name VARCHAR(50) PATH '$.name', primary_email VARCHAR(50) PATH '$.emails[0]', phone VARCHAR(50) PATH '$.phone' DEFAULT '"UNKNOWN"' ON EMPTY NULL ON ERROR ) ) AS customers;
  • 52. JSON_TABLE() example +------+-------------+---------------+---------+ | id | name | primary_email | phone | +------+-------------+---------------+---------+ | 1 | John Doe | john@doe.com | 123456 | | 2 | Jane Dee | Jane@dee.com | 987654 | | 3 | Donald Duck | NULL | UNKNOWN | +------+-------------+---------------+---------+
  • 54. What CONNECT is ● Connect is a storage engine for MariaDB ○ Won’t compile on MySQL or Percona Server ● Reads data from different data sources: ○ Remote tables (MySQL protocol, ODBC, JDBC) ○ Files in various formats (JSON, XML, CSV, custom formats…) ○ Special data sources ○ Transformed data from other tables (pivot, …)
  • 55. CONNECT and JSON ● JSON Table Type (10.1) ● On Windows, can read data from REST APIs (not easy to do) ● CONNECT comes with a library of JSON UDFs ○ Probably not battle-tested as the current built-in functions
  • 56. Thank you for listening! Federico hello@federico-razzoli.com