SlideShare a Scribd company logo
Advanced MariaDB features
that developers love
Federico Razzoli
vettabase.com
Advanced MariaDB features
that developers 💘 💖 💕 😍😍😍
Federico Razzoli
vettabase.com
$ whoami
Hi, I’m Federico Razzoli, founder of Vettabase
Database specialist, open source supporter,
long time MariaDB and MySQL user
Despite my accent, I live in 󰧺
● vettabase.com
● federico.razzoli@vettabase.com
This talk is…
● NOT a MariaDB tutorial
● We’ll see some cool MariaDB features arbitrarily chosen by me:
○ CONNECT storage engine
○ Temporal table
○ JSON columns
● In the process, we’ll also see quickly some other nice features
Querying remote / heterogeneous
data sources… in SQL
Why?
● Because… it’s SQL!
● Because sometimes we import data from other sources
● Because you may have to interact with systems you don’t control
CONNECT storage engine
● MariaDB doesn’t know how to read/write tables, indexes, data caches…
● These actions are delegated to plugins called storage engines
● A cool consequence is that when we run a query they could do anything they
like
○ BLACKHOLE does nothing
○ SEQUENCE returns numerical sequences
○ Run SHOW ENGINES
● CONNECT treats an external data source as if it was a local table
● So you can run your SELECTs, JOINs, INSERTs…
CONNECT storage engine
● Files: CSV, JSON, XML, tables in HTML pages… files in archives… custom files…
● Remote databases: Remote databases: MySQL protocol, MongoDB, ODBC, JDBC
● Data transformation
● More
Installing CONNECT
INSTALL SONAME 'ha_connect';
If you get this error:
ERROR 1126 (HY000): Can't open shared library
'/usr/lib/mysql/plugin/ha_connect.so' (errno: 2, cannot open shared
object file: No such file or directory)
…you don’t have the plugin in plugin_dir. Most probably MariaDB was installed from a
Linux repository. There should be a package for CONNECT:
● yum install MariaDB-connect-engine
● apt-get install mariadb-plugin-connect
If you installed MariaDB from tarball, you’ll need to install dependencies, use ldd.
Creating a CONNECT table (CSV)
CREATE OR REPLACE TABLE import_product (
id INTEGER UNSIGNED NOT NULL,
name VARCHAR(100) NOT NULL,
quantity INTEGER NOT NULL,
last_modified DATE NOT NULL date_format='YY/MM/DD'
)
ENGINE=CONNECT
TABLE_TYPE=CSV
FILE_NAME='/var/import/product.csv'
SEP_CHAR='t'
BLOCK_SIZE=2048
;
Copying data into a regular table
INSERT IGNORE INTO product
SELECT *
FROM import_product
WHERE last_modified > (CURRENT_DATE() - INTERVAL 7 DAY);
Inserting data into CONNECT
MariaDB [test]> INSERT INTO import_product
(id, name, quantity, last_modified)
VALUES (24, 'Sonic screwdriver', 100,
CURRENT_DATE());
Query OK, 1 row affected (0.067 sec)
MariaDB [test]> ! tail -1 /var/import/product.csv
24 Sonic screwdriver 100 22/06/20
CONNECT Indexes
● You can build indexes
● Indexes are stored in separate files
● Avoid this message by using proper character sets and specifying VARCHAR
length:
Specified key was too long; max key length is 255 bytes
● CONNECT indexes work better if rows are pre-ordered
ALTER TABLE import_product ADD INDEX idx_name (date);
CONNECT storage engine: merging tables
● We can merge multiple CONNECT tables:
CREATE OR REPLACE TABLE import_product_all
ENGINE=CONNECT
TABLE_TYPE=TBL
TABLE_LIST='import_product_1,import_product_2'
;
CONNECT storage engine: merging tables
● We can merge CONNECT and regular tables:
CREATE OR REPLACE TABLE product_proxy
ENGINE=CONNECT
TABLE_TYPE=PROXY
TABNAME=product
;
CREATE OR REPLACE TABLE import_product_all
ENGINE=CONNECT
TABLE_TYPE=TBL
TABLE_LIST='product_proxy,import_product_1,import_product_2'
;
CONNECT + SQL Server
● CONNECT treats an external data source as if it was a local table
● Column definitions can be automatically retrieved
● (though you may need to map column types & character sets)
● Remote indexes are used
CREATE TABLE import_product
ENGINE=CONNECT,
TABLE_TYPE=ODBC,
TABNAME='product'
CONNECTION='Driver=SQL Server Native Client
13.0;Server=sql-server-hostname;Database=shop;UID=mariadb_co
nnect;PWD=secret';
CONNECT data transformation
Some table types are used for data transformation:
● PIVOT contains the contents of another table or a query results as a pivot table
● XCOL “normalises” tables with a column containing a comma-separated list
● OCCUR is the opposite of XCOL
Example:
Scotland Edinburgh, Glasgow
England London, Cambridge
Scotland Edinburgh
Scotland Glasgow
England London
England Cambridge
CONNECT storage engine
● CONNECT treats an external data source as if it was a local table
Temporal tables
Temporal Tables
Temporal tables contain versioned rows.
Types of Temporal Tables:
● System-versioned
○ MariaDB automatically maintains row versions, with start and end timestamps
● Application-time
○ The application can use special SQL syntax to maintain and query row versions
● Bitemporal tables
○ Both system-versioned and application-time
In all cases, regular SQL queries will work and will only return current data
Temporal Tables: System-versioned
● MariaDB automatically maintains row versions, with start and end timestamps
● Or, one can have transaction IDs instead of timestamps
● There is no way to make a change that is not versioned
● You can use them even for proprietary applications that you can’t modify
● It is even possible to make a table system-versioned in a MariaDB replica
Temporal Tables
CREATE OR REPLACE TABLE ticket (
id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
state ENUM('OPEN', 'VERIFIED', 'FIXED', 'INVALID') NOT
NULL DEFAULT 'OPEN',
summary VARCHAR(200) NOT NULL,
description TEXT NOT NULL
)
ENGINE InnoDB
;
We want to start to track changes to bugs over time.
Temporal Tables
ALTER TABLE ticket
LOCK = SHARED,
ALGORITHM = COPY,
ADD COLUMN valid_from DATETIME NOT NULL DEFAULT NOW(),
ADD COLUMN valid_to DATETIME NOT NULL DEFAULT
'2038-01-19 03:14:07.999999',
ADD INDEX idx_valid_from (valid_from),
ADD INDEX idx_valid_to (valid_to),
ADD PERIOD FOR system_period (valid_from, valid_to)
ADD SYSTEM VERSIONING;
Querying a system-versioned table
-- get current version of the rows
-- without the temporal columns (they’re INVISIBLE)
SELECT * FROM ticket;
-- get current version of the rows
-- with the temporal columns
SELECT *, inserted_at, deleted_at FROM ticket;
-- all current and old data
SELECT *, inserted_at, deleted_at
FROM ticket FOR SYSTEM_TIME ALL;
Get old versions of the rows
-- get deleted rows
SELECT *, inserted_at, deleted_at
FROM ticket FOR SYSTEM_TIME
FROM '1970-00-00' TO (NOW() - 1 MICROSECOND);
SELECT *, inserted_at, deleted_at
FROM ticket FOR SYSTEM_TIME ALL
WHERE deleted_at < NOW();
HIstory of a row
SELECT id, state, inserted_at, deleted_at
FROM ticket FOR SYSTEM_TIME ALL
WHERE id = 3
ORDER BY deleted_at;
Read a row from a specific point in time
SELECT id, state
FROM ticket FOR SYSTEM_TIME AS OF TIMESTAMP '2020-08-22
08:52:36'
WHERE id = 3;
SELECT id, state
FROM ticket FOR SYSTEM_TIME ALL
WHERE id = 3 AND
'2020-08-22 08:52:36' BETWEEN inserted_at AND
deleted_at;
Temporal JOINs
-- rows that were present on 07/01
-- whose state did not change after one month
SELECT t1.id, t1.inserted_at, t1.deleted_at
FROM ticket FOR SYSTEM_TIME ALL AS t1
LEFT JOIN ticket FOR SYSTEM_TIME ALL AS t2
ON t1.id = t2.id
AND t1.state = t2.state
WHERE '2020-07-01 00:00:00' BETWEEN t1.inserted_at AND t1.deleted_at
AND '2020-08-01 00:00:00' BETWEEN t2.inserted_at AND t2.deleted_at
AND t2.id IS NULL
ORDER BY t1.id;
Hints about other things you can do
● Stats on added/deleted rows by year, month, weekday, day, daytime…
● Stats on rows life length
● Get rows that never changed
● Anomaly detection: get rows that change too often, or change at weird times
● Examine history of a row to find problems
JSON in MariaDB
JSON use cases
● Build a prototype
● Store data to import/export in an intermediate form
● Complex, nested data
● Store partially heterogeneous data
● Store documents with inheritance
● Store arrays / lists / sets
● Store key/value pairs (a more standard replacement for SET or ENUM types)
Some of these will be elaborated later
JSON use cases: partially heterogeneous data
Typical example: a product catalog
product: name, desc, price
shirt: name, desc, price, size, colour
phone: name, desc, price, size, brand, model, weight
Solutions:
● 1 table with a column for every property that at least 1 product has
● 1 table per product type
● 1 common table + 1 table per product type
● 1 table with common columns + 1 JSON column
○ (or more than 1)
JSON use cases: partially heterogeneous data
CREATE TABLE product (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
product_type VARCHAR(50) NOT NULL
COMMENT 'Determines which attributes a product has',
name VARCHAR(50) NOT NULL,
description TEXT NOT NULL DEFAULT '',
cost DECIMAL(10, 2) NOT NULL
attributes JSON NOT NULL,
UNIQUE unq_name_type (name, type)
);
BUT:
SELECT DISTINCT JSON_EXTRACT(attributes, '$.colour')
FROM product
WHERE type = shirt AND JSON_EXTRACT(attributes, '$.size) = 'M';
JSON use cases: partially heterogeneous data
CREATE TABLE product (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
product_type VARCHAR(50) NOT NULL
COMMENT 'Determines which attributes a product has',
name VARCHAR(50) NOT NULL,
description TEXT NOT NULL DEFAULT '',
cost DECIMAL(10, 2) NOT NULL
attributes JSON NOT NULL,
colour GENERATED ALWAYS AS (JSON_EXTRACT(attributes, '$.colour')) STORED,
size GENERATED ALWAYS AS (JSON_EXTRACT(attributes, '$.size)) STORED,
UNIQUE unq_name_type (name, type),
INDEX idx_size_colour (size, colour)
);
JSON with typical DBMS features
CHECK:
CREATE TABLE product (
...
colour GENERATED ALWAYS AS (JSON_EXTRACT(attributes, '$.colour')) STORED
CHECK (colour IN ('BLACK', 'WHITE', 'BLUE'))
)
JSON with typical DBMS features
UNIQUE indexes:
CREATE TABLE product (
...
phone_brand_colour GENERATED ALWAYS AS (
IF(
type != 'phone',
NULL,
CONCAT_WS('.', model, JSON_EXTRACT(attributes, '$.colour'))
)) STORED
)
JSON with typical DBMS features
● With the same techniques shown earlier, the colour generated column can be
used in a foreign key
● But I don’t recommend the use of foreign keys
Thanks for attending!
federico.razzoli@vettabase.com

More Related Content

PDF
Google & FIDO Authentication
KEY
Django Celery
PPT
Software Quality - A perspective
PPTX
Kotlin for Android App Development Presentation
PPT
Advanced JavaScript
PDF
Spock Testing Framework - The Next Generation
PDF
iOSアプリ UIテスト自動化入門
Google & FIDO Authentication
Django Celery
Software Quality - A perspective
Kotlin for Android App Development Presentation
Advanced JavaScript
Spock Testing Framework - The Next Generation
iOSアプリ UIテスト自動化入門

What's hot (9)

PPT
Stegnography
PPTX
Welcome and FIDO Update.pptx
PDF
Portfolio Software Engineer
PDF
Gérer le chiffrement de vos disques en entreprise avec Microsoft BitLocker Ad...
PPTX
Presentation on Android application life cycle and saved instancestate
PDF
Introduction to SQLAlchemy ORM
PDF
New Framework - ORM
PPTX
C# lecture 1: Introduction to Dot Net Framework
Stegnography
Welcome and FIDO Update.pptx
Portfolio Software Engineer
Gérer le chiffrement de vos disques en entreprise avec Microsoft BitLocker Ad...
Presentation on Android application life cycle and saved instancestate
Introduction to SQLAlchemy ORM
New Framework - ORM
C# lecture 1: Introduction to Dot Net Framework
Ad

Similar to Advanced MariaDB features that developers love.pdf (20)

PDF
What's New in MariaDB Server 10.2 and MariaDB MaxScale 2.1
PDF
What's New in MariaDB Server 10.2 and MariaDB MaxScale 2.1
PDF
MariaDB Temporal Tables
PDF
When and Why to Use MariaDB: New Features in 10.0 to 10.5
PDF
5_MariaDB_What's New in MariaDB Server 10.2 and Big Data Analytics with Maria...
PDF
MariaDB workshop
PDF
MariaDB ColumnStore
PDF
Big Data Analytics with MariaDB ColumnStore
PDF
Die Neuheiten in MariaDB 10.2 und MaxScale 2.1
PPTX
MySQL 8.0 Released Update
PDF
Modern solutions for modern database load: improvements in the latest MariaDB...
PDF
MariaDB 10.2 New Features
PDF
MariaDB Temporal Tables
PDF
What is MariaDB Server 10.3?
PDF
MariaDB: Connect Storage Engine
PPTX
MySQL 8 - UKOUG Techfest Brighton December 2nd, 2019
PDF
Optimizer features in recent releases of other databases
PDF
How MySQL can boost (or kill) your application
PDF
MySQL Goes to 8! FOSDEM 2020 Database Track, January 2nd, 2020
PDF
What’s New in MariaDB Server 10.2
What's New in MariaDB Server 10.2 and MariaDB MaxScale 2.1
What's New in MariaDB Server 10.2 and MariaDB MaxScale 2.1
MariaDB Temporal Tables
When and Why to Use MariaDB: New Features in 10.0 to 10.5
5_MariaDB_What's New in MariaDB Server 10.2 and Big Data Analytics with Maria...
MariaDB workshop
MariaDB ColumnStore
Big Data Analytics with MariaDB ColumnStore
Die Neuheiten in MariaDB 10.2 und MaxScale 2.1
MySQL 8.0 Released Update
Modern solutions for modern database load: improvements in the latest MariaDB...
MariaDB 10.2 New Features
MariaDB Temporal Tables
What is MariaDB Server 10.3?
MariaDB: Connect Storage Engine
MySQL 8 - UKOUG Techfest Brighton December 2nd, 2019
Optimizer features in recent releases of other databases
How MySQL can boost (or kill) your application
MySQL Goes to 8! FOSDEM 2020 Database Track, January 2nd, 2020
What’s New in MariaDB Server 10.2
Ad

More from Federico Razzoli (20)

PDF
MariaDB Data Protection: Backup Strategies for the Real World
PDF
MariaDB/MySQL_: Developing Scalable Applications
PDF
Webinar: Designing a schema for a Data Warehouse
PDF
High-level architecture of a complete MariaDB deployment
PDF
Webinar - Unleash AI power with MySQL and MindsDB
PDF
MariaDB Security Best Practices
PDF
A first look at MariaDB 11.x features and ideas on how to use them
PDF
MariaDB stored procedures and why they should be improved
PDF
Webinar - MariaDB Temporal Tables: a demonstration
PDF
Webinar - Key Reasons to Upgrade to MySQL 8.0 or MariaDB 10.11
PDF
MariaDB 10.11 key features overview for DBAs
PDF
Recent MariaDB features to learn for a happy life
PDF
Automate MariaDB Galera clusters deployments with Ansible
PDF
Creating Vagrant development machines with MariaDB
PDF
MariaDB, MySQL and Ansible: automating database infrastructures
PDF
Playing with the CONNECT storage engine
PDF
Database Design most common pitfalls
PDF
MySQL and MariaDB Backups
PDF
JSON in MySQL and MariaDB Databases
PDF
How MySQL can boost (or kill) your application v2
MariaDB Data Protection: Backup Strategies for the Real World
MariaDB/MySQL_: Developing Scalable Applications
Webinar: Designing a schema for a Data Warehouse
High-level architecture of a complete MariaDB deployment
Webinar - Unleash AI power with MySQL and MindsDB
MariaDB Security Best Practices
A first look at MariaDB 11.x features and ideas on how to use them
MariaDB stored procedures and why they should be improved
Webinar - MariaDB Temporal Tables: a demonstration
Webinar - Key Reasons to Upgrade to MySQL 8.0 or MariaDB 10.11
MariaDB 10.11 key features overview for DBAs
Recent MariaDB features to learn for a happy life
Automate MariaDB Galera clusters deployments with Ansible
Creating Vagrant development machines with MariaDB
MariaDB, MySQL and Ansible: automating database infrastructures
Playing with the CONNECT storage engine
Database Design most common pitfalls
MySQL and MariaDB Backups
JSON in MySQL and MariaDB Databases
How MySQL can boost (or kill) your application v2

Recently uploaded (20)

PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Digital Strategies for Manufacturing Companies
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
System and Network Administraation Chapter 3
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
Transform Your Business with a Software ERP System
PDF
medical staffing services at VALiNTRY
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Understanding Forklifts - TECH EHS Solution
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Operating system designcfffgfgggggggvggggggggg
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Odoo Companies in India – Driving Business Transformation.pdf
VVF-Customer-Presentation2025-Ver1.9.pptx
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Reimagine Home Health with the Power of Agentic AI​
2025 Textile ERP Trends: SAP, Odoo & Oracle
Digital Strategies for Manufacturing Companies
Odoo POS Development Services by CandidRoot Solutions
System and Network Administraation Chapter 3
Softaken Excel to vCard Converter Software.pdf
Transform Your Business with a Software ERP System
medical staffing services at VALiNTRY
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Understanding Forklifts - TECH EHS Solution
How to Choose the Right IT Partner for Your Business in Malaysia

Advanced MariaDB features that developers love.pdf

  • 1. Advanced MariaDB features that developers love Federico Razzoli vettabase.com
  • 2. Advanced MariaDB features that developers 💘 💖 💕 😍😍😍 Federico Razzoli vettabase.com
  • 3. $ whoami Hi, I’m Federico Razzoli, founder of Vettabase Database specialist, open source supporter, long time MariaDB and MySQL user Despite my accent, I live in 󰧺 ● vettabase.com ● federico.razzoli@vettabase.com
  • 4. This talk is… ● NOT a MariaDB tutorial ● We’ll see some cool MariaDB features arbitrarily chosen by me: ○ CONNECT storage engine ○ Temporal table ○ JSON columns ● In the process, we’ll also see quickly some other nice features
  • 5. Querying remote / heterogeneous data sources… in SQL
  • 6. Why? ● Because… it’s SQL! ● Because sometimes we import data from other sources ● Because you may have to interact with systems you don’t control
  • 7. CONNECT storage engine ● MariaDB doesn’t know how to read/write tables, indexes, data caches… ● These actions are delegated to plugins called storage engines ● A cool consequence is that when we run a query they could do anything they like ○ BLACKHOLE does nothing ○ SEQUENCE returns numerical sequences ○ Run SHOW ENGINES ● CONNECT treats an external data source as if it was a local table ● So you can run your SELECTs, JOINs, INSERTs…
  • 8. CONNECT storage engine ● Files: CSV, JSON, XML, tables in HTML pages… files in archives… custom files… ● Remote databases: Remote databases: MySQL protocol, MongoDB, ODBC, JDBC ● Data transformation ● More
  • 9. Installing CONNECT INSTALL SONAME 'ha_connect'; If you get this error: ERROR 1126 (HY000): Can't open shared library '/usr/lib/mysql/plugin/ha_connect.so' (errno: 2, cannot open shared object file: No such file or directory) …you don’t have the plugin in plugin_dir. Most probably MariaDB was installed from a Linux repository. There should be a package for CONNECT: ● yum install MariaDB-connect-engine ● apt-get install mariadb-plugin-connect If you installed MariaDB from tarball, you’ll need to install dependencies, use ldd.
  • 10. Creating a CONNECT table (CSV) CREATE OR REPLACE TABLE import_product ( id INTEGER UNSIGNED NOT NULL, name VARCHAR(100) NOT NULL, quantity INTEGER NOT NULL, last_modified DATE NOT NULL date_format='YY/MM/DD' ) ENGINE=CONNECT TABLE_TYPE=CSV FILE_NAME='/var/import/product.csv' SEP_CHAR='t' BLOCK_SIZE=2048 ;
  • 11. Copying data into a regular table INSERT IGNORE INTO product SELECT * FROM import_product WHERE last_modified > (CURRENT_DATE() - INTERVAL 7 DAY);
  • 12. Inserting data into CONNECT MariaDB [test]> INSERT INTO import_product (id, name, quantity, last_modified) VALUES (24, 'Sonic screwdriver', 100, CURRENT_DATE()); Query OK, 1 row affected (0.067 sec) MariaDB [test]> ! tail -1 /var/import/product.csv 24 Sonic screwdriver 100 22/06/20
  • 13. CONNECT Indexes ● You can build indexes ● Indexes are stored in separate files ● Avoid this message by using proper character sets and specifying VARCHAR length: Specified key was too long; max key length is 255 bytes ● CONNECT indexes work better if rows are pre-ordered ALTER TABLE import_product ADD INDEX idx_name (date);
  • 14. CONNECT storage engine: merging tables ● We can merge multiple CONNECT tables: CREATE OR REPLACE TABLE import_product_all ENGINE=CONNECT TABLE_TYPE=TBL TABLE_LIST='import_product_1,import_product_2' ;
  • 15. CONNECT storage engine: merging tables ● We can merge CONNECT and regular tables: CREATE OR REPLACE TABLE product_proxy ENGINE=CONNECT TABLE_TYPE=PROXY TABNAME=product ; CREATE OR REPLACE TABLE import_product_all ENGINE=CONNECT TABLE_TYPE=TBL TABLE_LIST='product_proxy,import_product_1,import_product_2' ;
  • 16. CONNECT + SQL Server ● CONNECT treats an external data source as if it was a local table ● Column definitions can be automatically retrieved ● (though you may need to map column types & character sets) ● Remote indexes are used CREATE TABLE import_product ENGINE=CONNECT, TABLE_TYPE=ODBC, TABNAME='product' CONNECTION='Driver=SQL Server Native Client 13.0;Server=sql-server-hostname;Database=shop;UID=mariadb_co nnect;PWD=secret';
  • 17. CONNECT data transformation Some table types are used for data transformation: ● PIVOT contains the contents of another table or a query results as a pivot table ● XCOL “normalises” tables with a column containing a comma-separated list ● OCCUR is the opposite of XCOL Example: Scotland Edinburgh, Glasgow England London, Cambridge Scotland Edinburgh Scotland Glasgow England London England Cambridge
  • 18. CONNECT storage engine ● CONNECT treats an external data source as if it was a local table
  • 20. Temporal Tables Temporal tables contain versioned rows. Types of Temporal Tables: ● System-versioned ○ MariaDB automatically maintains row versions, with start and end timestamps ● Application-time ○ The application can use special SQL syntax to maintain and query row versions ● Bitemporal tables ○ Both system-versioned and application-time In all cases, regular SQL queries will work and will only return current data
  • 21. Temporal Tables: System-versioned ● MariaDB automatically maintains row versions, with start and end timestamps ● Or, one can have transaction IDs instead of timestamps ● There is no way to make a change that is not versioned ● You can use them even for proprietary applications that you can’t modify ● It is even possible to make a table system-versioned in a MariaDB replica
  • 22. Temporal Tables CREATE OR REPLACE TABLE ticket ( id INT PRIMARY KEY NOT NULL AUTO_INCREMENT, state ENUM('OPEN', 'VERIFIED', 'FIXED', 'INVALID') NOT NULL DEFAULT 'OPEN', summary VARCHAR(200) NOT NULL, description TEXT NOT NULL ) ENGINE InnoDB ; We want to start to track changes to bugs over time.
  • 23. Temporal Tables ALTER TABLE ticket LOCK = SHARED, ALGORITHM = COPY, ADD COLUMN valid_from DATETIME NOT NULL DEFAULT NOW(), ADD COLUMN valid_to DATETIME NOT NULL DEFAULT '2038-01-19 03:14:07.999999', ADD INDEX idx_valid_from (valid_from), ADD INDEX idx_valid_to (valid_to), ADD PERIOD FOR system_period (valid_from, valid_to) ADD SYSTEM VERSIONING;
  • 24. Querying a system-versioned table -- get current version of the rows -- without the temporal columns (they’re INVISIBLE) SELECT * FROM ticket; -- get current version of the rows -- with the temporal columns SELECT *, inserted_at, deleted_at FROM ticket; -- all current and old data SELECT *, inserted_at, deleted_at FROM ticket FOR SYSTEM_TIME ALL;
  • 25. Get old versions of the rows -- get deleted rows SELECT *, inserted_at, deleted_at FROM ticket FOR SYSTEM_TIME FROM '1970-00-00' TO (NOW() - 1 MICROSECOND); SELECT *, inserted_at, deleted_at FROM ticket FOR SYSTEM_TIME ALL WHERE deleted_at < NOW();
  • 26. HIstory of a row SELECT id, state, inserted_at, deleted_at FROM ticket FOR SYSTEM_TIME ALL WHERE id = 3 ORDER BY deleted_at;
  • 27. Read a row from a specific point in time SELECT id, state FROM ticket FOR SYSTEM_TIME AS OF TIMESTAMP '2020-08-22 08:52:36' WHERE id = 3; SELECT id, state FROM ticket FOR SYSTEM_TIME ALL WHERE id = 3 AND '2020-08-22 08:52:36' BETWEEN inserted_at AND deleted_at;
  • 28. Temporal JOINs -- rows that were present on 07/01 -- whose state did not change after one month SELECT t1.id, t1.inserted_at, t1.deleted_at FROM ticket FOR SYSTEM_TIME ALL AS t1 LEFT JOIN ticket FOR SYSTEM_TIME ALL AS t2 ON t1.id = t2.id AND t1.state = t2.state WHERE '2020-07-01 00:00:00' BETWEEN t1.inserted_at AND t1.deleted_at AND '2020-08-01 00:00:00' BETWEEN t2.inserted_at AND t2.deleted_at AND t2.id IS NULL ORDER BY t1.id;
  • 29. Hints about other things you can do ● Stats on added/deleted rows by year, month, weekday, day, daytime… ● Stats on rows life length ● Get rows that never changed ● Anomaly detection: get rows that change too often, or change at weird times ● Examine history of a row to find problems
  • 31. JSON use cases ● Build a prototype ● Store data to import/export in an intermediate form ● Complex, nested data ● Store partially heterogeneous data ● Store documents with inheritance ● Store arrays / lists / sets ● Store key/value pairs (a more standard replacement for SET or ENUM types) Some of these will be elaborated later
  • 32. JSON use cases: partially heterogeneous data Typical example: a product catalog product: name, desc, price shirt: name, desc, price, size, colour phone: name, desc, price, size, brand, model, weight Solutions: ● 1 table with a column for every property that at least 1 product has ● 1 table per product type ● 1 common table + 1 table per product type ● 1 table with common columns + 1 JSON column ○ (or more than 1)
  • 33. JSON use cases: partially heterogeneous data CREATE TABLE product ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, product_type VARCHAR(50) NOT NULL COMMENT 'Determines which attributes a product has', name VARCHAR(50) NOT NULL, description TEXT NOT NULL DEFAULT '', cost DECIMAL(10, 2) NOT NULL attributes JSON NOT NULL, UNIQUE unq_name_type (name, type) ); BUT: SELECT DISTINCT JSON_EXTRACT(attributes, '$.colour') FROM product WHERE type = shirt AND JSON_EXTRACT(attributes, '$.size) = 'M';
  • 34. JSON use cases: partially heterogeneous data CREATE TABLE product ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, product_type VARCHAR(50) NOT NULL COMMENT 'Determines which attributes a product has', name VARCHAR(50) NOT NULL, description TEXT NOT NULL DEFAULT '', cost DECIMAL(10, 2) NOT NULL attributes JSON NOT NULL, colour GENERATED ALWAYS AS (JSON_EXTRACT(attributes, '$.colour')) STORED, size GENERATED ALWAYS AS (JSON_EXTRACT(attributes, '$.size)) STORED, UNIQUE unq_name_type (name, type), INDEX idx_size_colour (size, colour) );
  • 35. JSON with typical DBMS features CHECK: CREATE TABLE product ( ... colour GENERATED ALWAYS AS (JSON_EXTRACT(attributes, '$.colour')) STORED CHECK (colour IN ('BLACK', 'WHITE', 'BLUE')) )
  • 36. JSON with typical DBMS features UNIQUE indexes: CREATE TABLE product ( ... phone_brand_colour GENERATED ALWAYS AS ( IF( type != 'phone', NULL, CONCAT_WS('.', model, JSON_EXTRACT(attributes, '$.colour')) )) STORED )
  • 37. JSON with typical DBMS features ● With the same techniques shown earlier, the colour generated column can be used in a foreign key ● But I don’t recommend the use of foreign keys