SlideShare a Scribd company logo
How can MySQL boost
Your applications?
How can MySQL boost
Your applications?
Mmh, wait...
How can MySQL boost
(or kill)
Your applications?
€ whoami
● Federico Razzoli
● Freelance consultant
● Writing SQL since MySQL 2.23
hello@federico-razzoli.com
● I love open source, sharing,
Collaboration, win-win, etc
● I love MariaDB, MySQL, Postgres, etc
○ Even Db2, somehow
This talk applies to...
● MySQL
● Percona Server
● MariaDB
And most information applies, with some changes, to:
● All other relational DBMSs
This talk is not about...
● ORMs
● PHP code
● Query optimisation
● SQL_MODE
● MySQL characteristics that I don’t want to advertise
○ For a reason
○ But you are allowed to ask questions that I hope
you don’t ask
○ I will still say “thank you for your question”
Why do I want to
talk about MySQL
at a PHP event?
Good practices™
for your dev machine
Configuration
/etc/mysql/my.cnf should always contain:
log_slow = 1
long_query_time = 0
performance_schema = 1
Slow log
ls -1 $( mysql -e 'SELECT @@datadir' ) | grep slow
● Empty the slow log before a test
○ echo '' > /path/to/slowlog
● Check the slow log when you want to check your queries
○ Includes query duration, rows returned and some details on the execution
plan
Performance Schema
Before a test:
TRUNCATE TABLE
performance_schema.events_statements_summary_by_digest;
Queries that fail
SELECT
DIGEST_TEXT,
COUNT_STAR
FROM performance_schema.events_statements_summary_by_digest
WHERE
DIGEST_TEXT IS NOT NULL
AND SUM_ERRORS > 0 -- SUM_WARNINGS
ORDER BY COUNT_STAR DESC
LIMIT 10
G
Queries with no results
SELECT *
FROM performance_schema.events_statements_summary_by_digest
WHERE
(
TRIM(DIGEST_TEXT) LIKE 'SELECT%'
OR TRIM(DIGEST_TEXT) LIKE 'CREATE%TABLE%SELECT%'
OR TRIM(DIGEST_TEXT) LIKE 'DELETE%'
OR TRIM(DIGEST_TEXT) LIKE 'UPDATE%'
OR TRIM(DIGEST_TEXT) LIKE 'REPLACE%'
)
AND SUM_ROWS_SENT = 0
AND SUM_ROWS_AFFECTED = 0
ORDER BY SUM_ROWS_EXAMINED DESC
LIMIT 10
G
Non-optimised queries
SELECT
DIGEST_TEXT,
COUNT_STAR
FROM performance_schema.events_statements_summary_by_digest
WHERE
DIGEST_TEXT IS NOT NULL AND (
SUM_NO_INDEX_USED > 0 OR
SUM_CREATED_TMP_DISK_TABLES > 0
)
ORDER BY SUM_ROWS_EXAMINED DESC
LIMIT 10
G
Indexes
An index is an ordered data structure
● Think to a phone book
● It is a table with an index on (last_name, first_name)
● First takeaway: the order of columns matters
● Your mind contains a pretty good SQL optimiser
● When you want to know which queries can be optimised with a certain index,
think to a phone book
Which queries can be optimised?
● WHERE last_name = 'Baker'
● WHERE first_name = 'Tom'
● WHERE first_name = 'Tom' AND last_name = 'Baker'
● WHERE last_name = 'Baker' AND first_name = 'Tom'
Rule #1:
A query can use a whole index
Or a leftmost part of an index
Which queries can be optimised?
● WHERE last_name = 'Baker'
● WHERE last_name <> 'Baker'
● WHERE last_name > 'Baker'
● WHERE last_name >= 'Baker'
● WHERE last_name < 'Baker'
● WHERE last_name =< 'Baker'
Which queries can be optimised?
● WHERE last_name > 'B' AND last_name < 'C'
● WHERE last_name BETWEEN 'B' AND 'BZZZZZZZZZZZ';
● WHERE last_name LIKE 'B%'
● WHERE last_name LIKE '%ake%'
● WHERE last_name LIKE '%r'
Rule #2:
You can use an index to find a value
Or a (closed/open) range
Which queries can be optimised?
● WHERE last_name = 'Nimoy' OR first_name = 'Leonard'
● WHERE last_name = 'Nimoy' OR last_name = 'Shatner'
Rule #3:
OR on different columns
Is not fully optimised
Which queries can be optimised?
● WHERE last_name = 'Nimoy' AND first_name = 'Leonard'
● WHERE last_name = 'Nimoy' AND first_name > 'Leonard'
● WHERE last_name > 'Nimoy' AND first_name = 'Leonard'
● WHERE last_name > 'Nimoy' AND first_name > 'Leonar0d'
Rule #4:
The index use
Stops at the first range
Use proper SQL
N + 1 problem
Don’t:
foreach ( SELECT * FROM author WHERE a.LIKE 'P%'; )
SELECT * FROM book WHERE author_id = ?;
Do:
SELECT a.first_name, a.last_name, b.*
FROM book b
JOIN author a
ON b.id = a.book_id
WHERE a.last_name = 'P%';
Dealing with duplicates
INSERT INTO product (id, ...) VALUES (24, ...);
INSERT IGNORE INTO product (id, ...) VALUES (24, ...);
INSERT INTO product (id, ...)
ON DUPLICATE KEY UPDATE name = 'Sonic screwdriver';
REPLACE INTO product (id, ...) VALUES (24, ...);
DELETE IGNORE ...
UPDATE IGNORE ...
Insert many rows
INSERT INTO user
(first_name, last_name, email)
VALUES
('William', 'Hartnell', 'first@bbc.co.uk'),
('Tom', 'Baker', 'tom@gmail.com'),
('Jody', 'Wittaker', 'first_lady@hotmail.com');
INSERT INTO `order` (user_id, product_id) VALUES
(LAST_INSERT_ID(), 24);
Delete/Update many tables
DELETE `order`, user
FROM `order`
INNER JOIN `order`
ON order.user_id = user.id
WHERE user = 24;
UPDATE `order`, user
FROM `order`
INNER JOIN `order`
ON order.user_id = user.id
SET status = 'CANCELLED'
WHERE user = 24;
Read+Delete data
Only MariaDB:
DELETE FROM user
WHERE id = 2424
RETURNING first_name, last_name;
Creating table with rows
CREATE TABLE past_order LIKE `order`;
INSERT INTO past_order
SELECT * FROM `order`
WHERE status IN ('SHIPPED', 'CANCELLED');
Or:
CREATE TABLE customer
SELECT u.id, u.first_name, u.last_name
FROM user u JOIN `order` o
ON u.id = o.user_id
WHERE o.status <> 'CANCELED';
MySQL and Transactions
What are transactions?
ACID
● Atomicity
○ All writes in a transaction fail or succeed altogether.
● Consistency
○ Data always switch from one consistent point to another.
● Isolation
○ Transactions are logically sequential.
● Durability
○ Data changes persist after system failures (crashes).
What are transactions?
START TRANSACTION;
SELECT … ;
UPDATE … ;
INSERT … ;
COMMIT;
START TRANSACTION;
DELETE … ;
INSERT … ;
ROLLBACK;
SET SESSION autocommit := 1; -- this is the default
DELETE … ;
What are transactions?
START TRANSACTION;
SELECT qty
FROM product
-- why did we use "qty > 0"?
WHERE id = 240 AND qty > 0
-- what is this?
IN SHARE MODE;
INSERT INTO orders (user_id, product_id) VALUES (24, 240);
UPDATE product SET qty = qty - 1 WHERE id = 240;
COMMIT;
Isolation levels
● READ UNCOMMITTED
○ You could see not-yet-committed changes.
● READ COMMITTED
○ Each query acquires a separate snapshot.
● REPEATABLE READ (default)
○ One snapshot for the whole transaction.
● SERIALIZABLE
○ Like REPEATABLE READ, but SELECTs are implicitly IN SHARE MODE
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
START TRANSACTION;
...
Use cases for READ UNCOMMITTED?
Use cases for READ UNCOMMITTED?
● Statistics (avg on 1M rows)
● “Hi Theresa, your last access was on 29th March”
● Delete old data
Use cases for READ COMMITTED?
Use cases for READ COMMITTED?
● Delete/update rows by id from multiple tables
● Show user’s payment history
● For each exam, show users who passed it
READ ONLY transactions
● Make sense with REPEATABLE READ
● 2 SELECTs will see consistent data
● Attempts to change data will return an error
● Performance optimisation
○ But not as much as READ UNCOMMITTED
START TRANSACTION READ ONLY;
Ways to kill MySQL
Having SELECT privilege is enough to kill MySQL!
(or any RDBMS)
Method 1:
START TRANSACTION;
SELECT * FROM `order`;
SELECT SLEEP(3600 * 12);
Ways to kill MySQL
Having SELECT privilege is enough to kill MySQL!
(or any RDBMS)
Method 2:
START TRANSACTION;
SELECT * FROM `order` WHERE id = 24 FOR UPDATE;
SELECT SLEEP(3600 * 12);
Advanced Table Features
CHECK constraints
MySQL 8.0+, MariaDB 10.2+
CREATE TABLE person (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL,
CHECK (email LIKE '_%@_%.__%'),
birth_date DATE NOT NULL,
death_date DATE,
CHECK (birth_date <= death_date OR death_date IS NULL)
);
Computed columns
CREATE TABLE person (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
full_name GENERATED ALWAYS AS
(CONCAT(first_name, ' ', last_name)),
email VARCHAR(100) NOT NULL,
birth_date DATE NOT NULL,
death_date DATE,
is_alive BOOL GENERATED ALWAYS AS (death_date IS NULL)
);
DEFAULT clauses
MySQL 8.0+, MariaDB 10.2+
CREATE TABLE person (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
full_name VARCHAR(100) NOT NULL
DEFAULT (CONCAT(first_name, ' ', last_name)),
email VARCHAR(100) NOT NULL,
birth_date DATE NOT NULL,
death_date DATE,
is_alive BOOL NOT NULL DEFAULT (death_date IS NULL)
);
DEFAULT v. Computed columns
● Computed values cannot be changed
● Both regular and computed columns can be indexed
○ Your DBA will not consider this option
● Indexed computed columns will work “implicitly”:
SELECT ...
WHERE CONCAT(first_name, ' ', last_name) =
'Peter Capaldi';
...but not in MariaDB
DEFAULT v. Computed columns
On a computed column, you can also build:
● CHECK constraints
○ Enforce a minimum length for full_name
○ Reject dead users
● UNIQUE indexes
○ REPLACE(last_name, ' ', ''), first_name
How to kill MySQL
In this case, it’s hard.
● DEFAULTs are normally lightweight
● The same is true for CHECKs
○ You cannot use a SELECT as a CHECK
● You can still try to make writes slow and fill the disk
with computed columns that produce big values
How to kill MySQL
For work-intensive workloads:
● UNIQUE may cause many disk reads
● FOREIGN KEYs cause many extra checks
JSON
Compose JSON values
CREATE TABLE person (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
... ,
data JSON NOT NULL DEFAULT (
JSON_OBJECT(
'id', id,
'emails', JSON_ARRAY(email_main, email_emergency),
'full_name', JSON_OBJECT(
'first', first_name,
'last', first_name,
)
)
)
);
Extract values from JSON
CREATE TABLE person (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
email_main VARCHAR(100) NOT NULL
DEFAULT (JSON_EXTRACT(data, '$[0]')),
email_emergency VARCHAR(100) NOT NULL
DEFAULT (JSON_EXTRACT(data, '$[1]')),
first_name VARCHAR(50) NOT NULL
DEFAULT (JSON_EXTRACT(data, '$.full_name.first')),
last_name VARCHAR(50) NOT NULL
DEFAULT (JSON_EXTRACT(data, '$.full_name.last')),
data JSON NOT NULL ...
);
Thanks for being still awake!

More Related Content

What's hot

Explaining the MySQL Explain
Explaining the MySQL ExplainExplaining the MySQL Explain
Explaining the MySQL Explain
MYXPLAIN
 
Efficient Pagination Using MySQL
Efficient Pagination Using MySQLEfficient Pagination Using MySQL
Efficient Pagination Using MySQL
Evan Weaver
 
Oraclesql
OraclesqlOraclesql
Oraclesql
Priya Goyal
 
Oracle basic queries
Oracle basic queriesOracle basic queries
Oracle basic queries
PRAKHAR JHA
 
Intro To TSQL - Unit 4
Intro To TSQL - Unit 4Intro To TSQL - Unit 4
Intro To TSQL - Unit 4
iccma
 
Intro To TSQL - Unit 1
Intro To TSQL - Unit 1Intro To TSQL - Unit 1
Intro To TSQL - Unit 1
iccma
 
My Sql concepts
My Sql conceptsMy Sql concepts
My Sql concepts
Pragya Rastogi
 
Optimizing MySQL Queries
Optimizing MySQL QueriesOptimizing MySQL Queries
Optimizing MySQL Queries
Achievers Tech
 
Optimizing queries MySQL
Optimizing queries MySQLOptimizing queries MySQL
Optimizing queries MySQL
Georgi Sotirov
 
SQL202.2 Accelerated Introduction to SQL Using SQL Server Module 2
SQL202.2 Accelerated Introduction to SQL Using SQL Server Module 2SQL202.2 Accelerated Introduction to SQL Using SQL Server Module 2
SQL202.2 Accelerated Introduction to SQL Using SQL Server Module 2
Dan D'Urso
 
Intro To TSQL - Unit 3
Intro To TSQL - Unit 3Intro To TSQL - Unit 3
Intro To TSQL - Unit 3
iccma
 
45 Essential SQL Interview Questions
45 Essential SQL Interview Questions45 Essential SQL Interview Questions
45 Essential SQL Interview Questions
Best SEO Tampa
 
Predicting Future Sale
Predicting Future SalePredicting Future Sale
Predicting Future Sale
Debmalya Pramanik
 
Subqueries, Backups, Users and Privileges
Subqueries, Backups, Users and PrivilegesSubqueries, Backups, Users and Privileges
Subqueries, Backups, Users and Privileges
Ashwin Dinoriya
 

What's hot (14)

Explaining the MySQL Explain
Explaining the MySQL ExplainExplaining the MySQL Explain
Explaining the MySQL Explain
 
Efficient Pagination Using MySQL
Efficient Pagination Using MySQLEfficient Pagination Using MySQL
Efficient Pagination Using MySQL
 
Oraclesql
OraclesqlOraclesql
Oraclesql
 
Oracle basic queries
Oracle basic queriesOracle basic queries
Oracle basic queries
 
Intro To TSQL - Unit 4
Intro To TSQL - Unit 4Intro To TSQL - Unit 4
Intro To TSQL - Unit 4
 
Intro To TSQL - Unit 1
Intro To TSQL - Unit 1Intro To TSQL - Unit 1
Intro To TSQL - Unit 1
 
My Sql concepts
My Sql conceptsMy Sql concepts
My Sql concepts
 
Optimizing MySQL Queries
Optimizing MySQL QueriesOptimizing MySQL Queries
Optimizing MySQL Queries
 
Optimizing queries MySQL
Optimizing queries MySQLOptimizing queries MySQL
Optimizing queries MySQL
 
SQL202.2 Accelerated Introduction to SQL Using SQL Server Module 2
SQL202.2 Accelerated Introduction to SQL Using SQL Server Module 2SQL202.2 Accelerated Introduction to SQL Using SQL Server Module 2
SQL202.2 Accelerated Introduction to SQL Using SQL Server Module 2
 
Intro To TSQL - Unit 3
Intro To TSQL - Unit 3Intro To TSQL - Unit 3
Intro To TSQL - Unit 3
 
45 Essential SQL Interview Questions
45 Essential SQL Interview Questions45 Essential SQL Interview Questions
45 Essential SQL Interview Questions
 
Predicting Future Sale
Predicting Future SalePredicting Future Sale
Predicting Future Sale
 
Subqueries, Backups, Users and Privileges
Subqueries, Backups, Users and PrivilegesSubqueries, Backups, Users and Privileges
Subqueries, Backups, Users and Privileges
 

Similar to How MySQL can boost (or kill) your application

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
Federico Razzoli
 
Database Design most common pitfalls
Database Design most common pitfallsDatabase Design most common pitfalls
Database Design most common pitfalls
Federico 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.pdf
Federico Razzoli
 
How to leave the ORM at home and write SQL
How to leave the ORM at home and write SQLHow to leave the ORM at home and write SQL
How to leave the ORM at home and write SQL
MariaDB plc
 
MariaDB Temporal Tables
MariaDB Temporal TablesMariaDB Temporal Tables
MariaDB Temporal Tables
Federico 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
 
Meg bernal insight2014 4219
Meg bernal insight2014 4219Meg bernal insight2014 4219
Meg bernal insight2014 4219
Peter Schouboe
 
SQL -PHP Tutorial
SQL -PHP TutorialSQL -PHP Tutorial
SQL -PHP Tutorial
Information Technology
 
Database
Database Database
Dok Talks #133 - My First 90 days with Clickhouse
Dok Talks #133 - My First 90 days with ClickhouseDok Talks #133 - My First 90 days with Clickhouse
Dok Talks #133 - My First 90 days with Clickhouse
DoKC
 
My first 90 days with ClickHouse.pdf
My first 90 days with ClickHouse.pdfMy first 90 days with ClickHouse.pdf
My first 90 days with ClickHouse.pdf
Alkin Tezuysal
 
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should KnowDBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
Alex Zaballa
 
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should KnowDBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
Alex Zaballa
 
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should KnowDBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
Alex Zaballa
 
Introduction to SQL Antipatterns
Introduction to SQL AntipatternsIntroduction to SQL Antipatterns
Introduction to SQL Antipatterns
Krishnakumar S
 
Geek Sync | Rewriting Bad SQL Code 101
Geek Sync | Rewriting Bad SQL Code 101Geek Sync | Rewriting Bad SQL Code 101
Geek Sync | Rewriting Bad SQL Code 101
IDERA Software
 
Use Your MySQL Knowledge to Become an Instant Cassandra Guru
Use Your MySQL Knowledge to Become an Instant Cassandra GuruUse Your MySQL Knowledge to Become an Instant Cassandra Guru
Use Your MySQL Knowledge to Become an Instant Cassandra Guru
Tim Callaghan
 
Really Big Elephants: PostgreSQL DW
Really Big Elephants: PostgreSQL DWReally Big Elephants: PostgreSQL DW
Really Big Elephants: PostgreSQL DW
PostgreSQL Experts, Inc.
 
Scaling MySQL Strategies for Developers
Scaling MySQL Strategies for DevelopersScaling MySQL Strategies for Developers
Scaling MySQL Strategies for Developers
Jonathan Levin
 
Krug 02 2014
Krug 02 2014Krug 02 2014
Krug 02 2014
Tomasz Giereś
 

Similar to How MySQL can boost (or kill) your application (20)

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
 
Database Design most common pitfalls
Database Design most common pitfallsDatabase Design most common pitfalls
Database Design most common pitfalls
 
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
 
How to leave the ORM at home and write SQL
How to leave the ORM at home and write SQLHow to leave the ORM at home and write SQL
How to leave the ORM at home and write SQL
 
MariaDB Temporal Tables
MariaDB Temporal TablesMariaDB Temporal Tables
MariaDB Temporal Tables
 
MySQL Transaction Isolation Levels (lightning talk)
MySQL Transaction Isolation Levels (lightning talk)MySQL Transaction Isolation Levels (lightning talk)
MySQL Transaction Isolation Levels (lightning talk)
 
Meg bernal insight2014 4219
Meg bernal insight2014 4219Meg bernal insight2014 4219
Meg bernal insight2014 4219
 
SQL -PHP Tutorial
SQL -PHP TutorialSQL -PHP Tutorial
SQL -PHP Tutorial
 
Database
Database Database
Database
 
Dok Talks #133 - My First 90 days with Clickhouse
Dok Talks #133 - My First 90 days with ClickhouseDok Talks #133 - My First 90 days with Clickhouse
Dok Talks #133 - My First 90 days with Clickhouse
 
My first 90 days with ClickHouse.pdf
My first 90 days with ClickHouse.pdfMy first 90 days with ClickHouse.pdf
My first 90 days with ClickHouse.pdf
 
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should KnowDBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
 
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should KnowDBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
 
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should KnowDBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
DBA Brasil 1.0 - DBA Commands and Concepts That Every Developer Should Know
 
Introduction to SQL Antipatterns
Introduction to SQL AntipatternsIntroduction to SQL Antipatterns
Introduction to SQL Antipatterns
 
Geek Sync | Rewriting Bad SQL Code 101
Geek Sync | Rewriting Bad SQL Code 101Geek Sync | Rewriting Bad SQL Code 101
Geek Sync | Rewriting Bad SQL Code 101
 
Use Your MySQL Knowledge to Become an Instant Cassandra Guru
Use Your MySQL Knowledge to Become an Instant Cassandra GuruUse Your MySQL Knowledge to Become an Instant Cassandra Guru
Use Your MySQL Knowledge to Become an Instant Cassandra Guru
 
Really Big Elephants: PostgreSQL DW
Really Big Elephants: PostgreSQL DWReally Big Elephants: PostgreSQL DW
Really Big Elephants: PostgreSQL DW
 
Scaling MySQL Strategies for Developers
Scaling MySQL Strategies for DevelopersScaling MySQL Strategies for Developers
Scaling MySQL Strategies for Developers
 
Krug 02 2014
Krug 02 2014Krug 02 2014
Krug 02 2014
 

More from Federico Razzoli

Webinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data WarehouseWebinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data Warehouse
Federico Razzoli
 
High-level architecture of a complete MariaDB deployment
High-level architecture of a complete MariaDB deploymentHigh-level architecture of a complete MariaDB deployment
High-level architecture of a complete MariaDB deployment
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 MindsDB
Federico Razzoli
 
MariaDB Security Best Practices
MariaDB Security Best PracticesMariaDB Security Best Practices
MariaDB Security Best Practices
Federico 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 them
Federico 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 improved
Federico Razzoli
 
Webinar - MariaDB Temporal Tables: a demonstration
Webinar - MariaDB Temporal Tables: a demonstrationWebinar - MariaDB Temporal Tables: a demonstration
Webinar - MariaDB Temporal Tables: a demonstration
Federico 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.11
Federico 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 DBAs
Federico 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 life
Federico 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 Ansible
Federico Razzoli
 
Creating Vagrant development machines with MariaDB
Creating Vagrant development machines with MariaDBCreating Vagrant development machines with MariaDB
Creating Vagrant development machines with MariaDB
Federico 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 infrastructures
Federico Razzoli
 
Playing with the CONNECT storage engine
Playing with the CONNECT storage enginePlaying with the CONNECT storage engine
Playing with the CONNECT storage engine
Federico Razzoli
 
MariaDB Temporal Tables
MariaDB Temporal TablesMariaDB Temporal Tables
MariaDB Temporal Tables
Federico Razzoli
 
MySQL and MariaDB Backups
MySQL and MariaDB BackupsMySQL and MariaDB Backups
MySQL and MariaDB Backups
Federico Razzoli
 
JSON in MySQL and MariaDB Databases
JSON in MySQL and MariaDB DatabasesJSON in MySQL and MariaDB Databases
JSON in MySQL and MariaDB Databases
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 (18)

Webinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data WarehouseWebinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data Warehouse
 
High-level architecture of a complete MariaDB deployment
High-level architecture of a complete MariaDB deploymentHigh-level architecture of a complete MariaDB deployment
High-level architecture of a complete MariaDB deployment
 
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
 
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
 
MySQL and MariaDB Backups
MySQL and MariaDB BackupsMySQL and MariaDB Backups
MySQL and MariaDB Backups
 
JSON in MySQL and MariaDB Databases
JSON in MySQL and MariaDB DatabasesJSON in MySQL and MariaDB Databases
JSON in MySQL and MariaDB Databases
 
Cassandra sharding and consistency (lightning talk)
Cassandra sharding and consistency (lightning talk)Cassandra sharding and consistency (lightning talk)
Cassandra sharding and consistency (lightning talk)
 

Recently uploaded

APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
Boni García
 
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise EditionWhy Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Envertis Software Solutions
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
Peter Muessig
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
TheSMSPoint
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
Green Software Development
 
What is Augmented Reality Image Tracking
What is Augmented Reality Image TrackingWhat is Augmented Reality Image Tracking
What is Augmented Reality Image Tracking
pavan998932
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
Neo4j
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptxLORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
lorraineandreiamcidl
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
mz5nrf0n
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
Deuglo Infosystem Pvt Ltd
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
lorraineandreiamcidl
 
SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024
Hironori Washizaki
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
Green Software Development
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
Hornet Dynamics
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
NYGGS Automation Suite
 

Recently uploaded (20)

APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
 
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise EditionWhy Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
 
What is Augmented Reality Image Tracking
What is Augmented Reality Image TrackingWhat is Augmented Reality Image Tracking
What is Augmented Reality Image Tracking
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptxLORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
 
SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
 
E-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet DynamicsE-commerce Development Services- Hornet Dynamics
E-commerce Development Services- Hornet Dynamics
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
 

How MySQL can boost (or kill) your application

  • 1. How can MySQL boost Your applications?
  • 2. How can MySQL boost Your applications? Mmh, wait...
  • 3. How can MySQL boost (or kill) Your applications?
  • 4. € whoami ● Federico Razzoli ● Freelance consultant ● Writing SQL since MySQL 2.23 hello@federico-razzoli.com ● I love open source, sharing, Collaboration, win-win, etc ● I love MariaDB, MySQL, Postgres, etc ○ Even Db2, somehow
  • 5. This talk applies to... ● MySQL ● Percona Server ● MariaDB And most information applies, with some changes, to: ● All other relational DBMSs
  • 6. This talk is not about... ● ORMs ● PHP code ● Query optimisation ● SQL_MODE ● MySQL characteristics that I don’t want to advertise ○ For a reason ○ But you are allowed to ask questions that I hope you don’t ask ○ I will still say “thank you for your question”
  • 7. Why do I want to talk about MySQL at a PHP event?
  • 9. Configuration /etc/mysql/my.cnf should always contain: log_slow = 1 long_query_time = 0 performance_schema = 1
  • 10. Slow log ls -1 $( mysql -e 'SELECT @@datadir' ) | grep slow ● Empty the slow log before a test ○ echo '' > /path/to/slowlog ● Check the slow log when you want to check your queries ○ Includes query duration, rows returned and some details on the execution plan
  • 11. Performance Schema Before a test: TRUNCATE TABLE performance_schema.events_statements_summary_by_digest;
  • 12. Queries that fail SELECT DIGEST_TEXT, COUNT_STAR FROM performance_schema.events_statements_summary_by_digest WHERE DIGEST_TEXT IS NOT NULL AND SUM_ERRORS > 0 -- SUM_WARNINGS ORDER BY COUNT_STAR DESC LIMIT 10 G
  • 13. Queries with no results SELECT * FROM performance_schema.events_statements_summary_by_digest WHERE ( TRIM(DIGEST_TEXT) LIKE 'SELECT%' OR TRIM(DIGEST_TEXT) LIKE 'CREATE%TABLE%SELECT%' OR TRIM(DIGEST_TEXT) LIKE 'DELETE%' OR TRIM(DIGEST_TEXT) LIKE 'UPDATE%' OR TRIM(DIGEST_TEXT) LIKE 'REPLACE%' ) AND SUM_ROWS_SENT = 0 AND SUM_ROWS_AFFECTED = 0 ORDER BY SUM_ROWS_EXAMINED DESC LIMIT 10 G
  • 14. Non-optimised queries SELECT DIGEST_TEXT, COUNT_STAR FROM performance_schema.events_statements_summary_by_digest WHERE DIGEST_TEXT IS NOT NULL AND ( SUM_NO_INDEX_USED > 0 OR SUM_CREATED_TMP_DISK_TABLES > 0 ) ORDER BY SUM_ROWS_EXAMINED DESC LIMIT 10 G
  • 16. An index is an ordered data structure ● Think to a phone book ● It is a table with an index on (last_name, first_name) ● First takeaway: the order of columns matters ● Your mind contains a pretty good SQL optimiser ● When you want to know which queries can be optimised with a certain index, think to a phone book
  • 17. Which queries can be optimised? ● WHERE last_name = 'Baker' ● WHERE first_name = 'Tom' ● WHERE first_name = 'Tom' AND last_name = 'Baker' ● WHERE last_name = 'Baker' AND first_name = 'Tom'
  • 18. Rule #1: A query can use a whole index Or a leftmost part of an index
  • 19. Which queries can be optimised? ● WHERE last_name = 'Baker' ● WHERE last_name <> 'Baker' ● WHERE last_name > 'Baker' ● WHERE last_name >= 'Baker' ● WHERE last_name < 'Baker' ● WHERE last_name =< 'Baker'
  • 20. Which queries can be optimised? ● WHERE last_name > 'B' AND last_name < 'C' ● WHERE last_name BETWEEN 'B' AND 'BZZZZZZZZZZZ'; ● WHERE last_name LIKE 'B%' ● WHERE last_name LIKE '%ake%' ● WHERE last_name LIKE '%r'
  • 21. Rule #2: You can use an index to find a value Or a (closed/open) range
  • 22. Which queries can be optimised? ● WHERE last_name = 'Nimoy' OR first_name = 'Leonard' ● WHERE last_name = 'Nimoy' OR last_name = 'Shatner'
  • 23. Rule #3: OR on different columns Is not fully optimised
  • 24. Which queries can be optimised? ● WHERE last_name = 'Nimoy' AND first_name = 'Leonard' ● WHERE last_name = 'Nimoy' AND first_name > 'Leonard' ● WHERE last_name > 'Nimoy' AND first_name = 'Leonard' ● WHERE last_name > 'Nimoy' AND first_name > 'Leonar0d'
  • 25. Rule #4: The index use Stops at the first range
  • 27. N + 1 problem Don’t: foreach ( SELECT * FROM author WHERE a.LIKE 'P%'; ) SELECT * FROM book WHERE author_id = ?; Do: SELECT a.first_name, a.last_name, b.* FROM book b JOIN author a ON b.id = a.book_id WHERE a.last_name = 'P%';
  • 28. Dealing with duplicates INSERT INTO product (id, ...) VALUES (24, ...); INSERT IGNORE INTO product (id, ...) VALUES (24, ...); INSERT INTO product (id, ...) ON DUPLICATE KEY UPDATE name = 'Sonic screwdriver'; REPLACE INTO product (id, ...) VALUES (24, ...); DELETE IGNORE ... UPDATE IGNORE ...
  • 29. Insert many rows INSERT INTO user (first_name, last_name, email) VALUES ('William', 'Hartnell', 'first@bbc.co.uk'), ('Tom', 'Baker', 'tom@gmail.com'), ('Jody', 'Wittaker', 'first_lady@hotmail.com'); INSERT INTO `order` (user_id, product_id) VALUES (LAST_INSERT_ID(), 24);
  • 30. Delete/Update many tables DELETE `order`, user FROM `order` INNER JOIN `order` ON order.user_id = user.id WHERE user = 24; UPDATE `order`, user FROM `order` INNER JOIN `order` ON order.user_id = user.id SET status = 'CANCELLED' WHERE user = 24;
  • 31. Read+Delete data Only MariaDB: DELETE FROM user WHERE id = 2424 RETURNING first_name, last_name;
  • 32. Creating table with rows CREATE TABLE past_order LIKE `order`; INSERT INTO past_order SELECT * FROM `order` WHERE status IN ('SHIPPED', 'CANCELLED'); Or: CREATE TABLE customer SELECT u.id, u.first_name, u.last_name FROM user u JOIN `order` o ON u.id = o.user_id WHERE o.status <> 'CANCELED';
  • 34. What are transactions? ACID ● Atomicity ○ All writes in a transaction fail or succeed altogether. ● Consistency ○ Data always switch from one consistent point to another. ● Isolation ○ Transactions are logically sequential. ● Durability ○ Data changes persist after system failures (crashes).
  • 35. What are transactions? START TRANSACTION; SELECT … ; UPDATE … ; INSERT … ; COMMIT; START TRANSACTION; DELETE … ; INSERT … ; ROLLBACK; SET SESSION autocommit := 1; -- this is the default DELETE … ;
  • 36. What are transactions? START TRANSACTION; SELECT qty FROM product -- why did we use "qty > 0"? WHERE id = 240 AND qty > 0 -- what is this? IN SHARE MODE; INSERT INTO orders (user_id, product_id) VALUES (24, 240); UPDATE product SET qty = qty - 1 WHERE id = 240; COMMIT;
  • 37. Isolation levels ● READ UNCOMMITTED ○ You could see not-yet-committed changes. ● READ COMMITTED ○ Each query acquires a separate snapshot. ● REPEATABLE READ (default) ○ One snapshot for the whole transaction. ● SERIALIZABLE ○ Like REPEATABLE READ, but SELECTs are implicitly IN SHARE MODE SET TRANSACTION ISOLATION LEVEL READ COMMITTED; START TRANSACTION; ...
  • 38. Use cases for READ UNCOMMITTED?
  • 39. Use cases for READ UNCOMMITTED? ● Statistics (avg on 1M rows) ● “Hi Theresa, your last access was on 29th March” ● Delete old data
  • 40. Use cases for READ COMMITTED?
  • 41. Use cases for READ COMMITTED? ● Delete/update rows by id from multiple tables ● Show user’s payment history ● For each exam, show users who passed it
  • 42. READ ONLY transactions ● Make sense with REPEATABLE READ ● 2 SELECTs will see consistent data ● Attempts to change data will return an error ● Performance optimisation ○ But not as much as READ UNCOMMITTED START TRANSACTION READ ONLY;
  • 43. Ways to kill MySQL Having SELECT privilege is enough to kill MySQL! (or any RDBMS) Method 1: START TRANSACTION; SELECT * FROM `order`; SELECT SLEEP(3600 * 12);
  • 44. Ways to kill MySQL Having SELECT privilege is enough to kill MySQL! (or any RDBMS) Method 2: START TRANSACTION; SELECT * FROM `order` WHERE id = 24 FOR UPDATE; SELECT SLEEP(3600 * 12);
  • 46. CHECK constraints MySQL 8.0+, MariaDB 10.2+ CREATE TABLE person ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, first_name VARCHAR(50) NOT NULL, last_name VARCHAR(50) NOT NULL, email VARCHAR(100) NOT NULL, CHECK (email LIKE '_%@_%.__%'), birth_date DATE NOT NULL, death_date DATE, CHECK (birth_date <= death_date OR death_date IS NULL) );
  • 47. Computed columns CREATE TABLE person ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, first_name VARCHAR(50) NOT NULL, last_name VARCHAR(50) NOT NULL, full_name GENERATED ALWAYS AS (CONCAT(first_name, ' ', last_name)), email VARCHAR(100) NOT NULL, birth_date DATE NOT NULL, death_date DATE, is_alive BOOL GENERATED ALWAYS AS (death_date IS NULL) );
  • 48. DEFAULT clauses MySQL 8.0+, MariaDB 10.2+ CREATE TABLE person ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, first_name VARCHAR(50) NOT NULL, last_name VARCHAR(50) NOT NULL, full_name VARCHAR(100) NOT NULL DEFAULT (CONCAT(first_name, ' ', last_name)), email VARCHAR(100) NOT NULL, birth_date DATE NOT NULL, death_date DATE, is_alive BOOL NOT NULL DEFAULT (death_date IS NULL) );
  • 49. DEFAULT v. Computed columns ● Computed values cannot be changed ● Both regular and computed columns can be indexed ○ Your DBA will not consider this option ● Indexed computed columns will work “implicitly”: SELECT ... WHERE CONCAT(first_name, ' ', last_name) = 'Peter Capaldi'; ...but not in MariaDB
  • 50. DEFAULT v. Computed columns On a computed column, you can also build: ● CHECK constraints ○ Enforce a minimum length for full_name ○ Reject dead users ● UNIQUE indexes ○ REPLACE(last_name, ' ', ''), first_name
  • 51. How to kill MySQL In this case, it’s hard. ● DEFAULTs are normally lightweight ● The same is true for CHECKs ○ You cannot use a SELECT as a CHECK ● You can still try to make writes slow and fill the disk with computed columns that produce big values
  • 52. How to kill MySQL For work-intensive workloads: ● UNIQUE may cause many disk reads ● FOREIGN KEYs cause many extra checks
  • 53. JSON
  • 54. Compose JSON values CREATE TABLE person ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, ... , data JSON NOT NULL DEFAULT ( JSON_OBJECT( 'id', id, 'emails', JSON_ARRAY(email_main, email_emergency), 'full_name', JSON_OBJECT( 'first', first_name, 'last', first_name, ) ) ) );
  • 55. Extract values from JSON CREATE TABLE person ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, email_main VARCHAR(100) NOT NULL DEFAULT (JSON_EXTRACT(data, '$[0]')), email_emergency VARCHAR(100) NOT NULL DEFAULT (JSON_EXTRACT(data, '$[1]')), first_name VARCHAR(50) NOT NULL DEFAULT (JSON_EXTRACT(data, '$.full_name.first')), last_name VARCHAR(50) NOT NULL DEFAULT (JSON_EXTRACT(data, '$.full_name.last')), data JSON NOT NULL ... );
  • 56. Thanks for being still awake!