SlideShare uma empresa Scribd logo
1 de 58
Baixar para ler offline
Making the most out of
MySQL
Gabriela D’Avila

@gabidavila
Feb/2017
Who?
• Data Engineer
• Lego Hoarder
• @gabidavila
• http://gabriela.io
2
What to Expect?
• DDL Changes
• SQL Modes
• Generated Columns
• JSON Data Type
• `sys` Schema
3
DDL Changes on InnoDB
DDL Changes
• In Place (ALGORITHM=INPLACE)
• Rename Index
• VARCHAR from 1B to 255B
• VARCHAR from 256B to 65353B*
• Add a Virtual Column
• Table-copy (ALGORITHM=COPY)
• Add a Column
• VARCHAR between 255B and >=256
• Decreasing VARCHAR size
• Type Conversion
5
More Info
SQL Modes
SQL modes (SELECT @@GLOBAL.sql_mode;)
7
On MySQL 5.6:
+--------------------------------------------+
| @@GLOBAL.sql_mode |
+--------------------------------------------+
| STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION |
+--------------------------------------------+
1 row in set (0.00 sec)
On MySQL 5.7:
+-----------------------------------------------------------------------+
| @@GLOBAL.sql_mode |
+-----------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE, |

| ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+-----------------------------------------------------------------------+
1 row in set (0.00 sec)
SQL modes comparison
On MySQL 5.6.35:
• STRICT_TRANS_TABLES (Bug #69743)
• NO_ENGINE_SUBSTITUTION
On MySQL 5.7.17:
• STRICT_TRANS_TABLES
• NO_ENGINE_SUBSTITUTION
• ONLY_FULL_GROUP_BY
• NO_ZERO_IN_DATE*
• NO_ZERO_DATE*
• ERROR_FOR_DIVISION_BY_ZERO*
8
Full list of sql_mode
YYYY-MM-DD
• NO_ZERO_DATE
• 0000-00-00 👎
• 0000-00-00 00:00:00 👎
• NO_ZERO_IN_DATE
• 2017-01-00 👎
• 2017-00-04 👎
• 0000-01-04 👍
9
ERROR_FOR_DIVISION_BY_ZERO
10
mysql> UPDATE users SET id = id/0 WHERE id = 2;
ERROR 1365 (22012): Division by 0
• Write Operation: UPDATE
mysql> SELECT id, mod(id,0) FROM users WHERE id = 2;
+----+-----------+
| id | mod(id,0) |
+----+-----------+
| 2 | NULL |
+----+-----------+
1 row in set, 1 warning (0.00 sec)
• Read Operation: SELECT
mysql> SHOW WARNINGS;
+---------+------+---------------+
| Level | Code | Message |
+---------+------+---------------+
| Warning | 1365 | Division by 0 |
+---------+------+---------------+
1 row in set (0.00 sec)
ONLY_FULL_GROUP_BY
Table `users`
Field Type Null Key Default Extra
id int(10) unsigned NO PRI auto_increment
first_name varchar(255) NO NULL
last_name varchar(255) NO NULL
email varchar(255) NO NULL
twitter_info json YES
created_at datetime NO CURRENT_TIMESTAMP
updated_at datetime NO CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP
13
Repeated emails
SELECT * FROM users GROUP BY email HAVING count(*) > 1
+-------+------------+-----------+---------------------+-------------------+---------------------+---------------------+
| id | first_name | last_name | email | twitter_info | created_at | updated_at |
+-------+------------+-----------+---------------------+-------------------+---------------------+---------------------+
| 8965 | Conor | Quitzon | yjacobson@yahoo.com | {"id": 100838242, | 2017-01-02 20:57:39 | 2017-01-02 20:57:39 |
| 69772 | Junius | Mante | nkuhlman@gmail.com | {"id": 20039476, | 2017-01-02 20:59:34 | 2017-01-02 20:59:34 |
| 66525 | Katelynn | Feil | qgottlieb@gmail.com | {"id": 111644778, | 2017-01-02 20:59:29 | 2017-01-02 20:59:29 |
| 92577 | Tillman | Nienow | zzieme@yahoo.com | {"id": 1359073920,| 2017-01-02 21:00:14 | 2017-01-02 21:00:14 |
| 18046 | Guillermo | Lebsack | cdoyle@gmail.com | {"id": 120064855, | 2017-01-02 20:57:59 | 2017-01-02 20:57:59 |
+-------+------------+-----------+---------------------+-------------------+---------------------+---------------------+
5 rows in set (0.65 sec)
5.6
14
Repeated emails
SELECT * FROM users WHERE email = 'cdoyle@gmail.com'
+-------+------------+-----------+------------------+--------------+---------------------+---------------------+
| id | first_name | last_name | email | twitter_info | created_at | updated_at |
+-------+------------+-----------+------------------+--------------+---------------------+---------------------+
| 18046 | Guillermo | Lebsack | cdoyle@gmail.com | NULL | 2017-01-02 14:57:59 | 2017-01-02 14:57:59 |
| 20559 | Marietta | Quitzon | cdoyle@gmail.com | NULL | 2017-01-02 14:58:03 | 2017-01-02 14:58:03 |
+-------+------------+-----------+------------------+--------------+---------------------+---------------------+
2 rows in set (0.04 sec)
15
Repeated emails
SELECT * FROM users GROUP BY email HAVING count(*) > 1
mysql> SELECT * FROM `users` GROUP BY `email` HAVING count(*) > 1;
ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains
nonaggregated column ‘store.users.id’ which is not functionally dependent on columns
in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
5.7
16
Why?
"Reject queries for which the select list, HAVING condition, or ORDER BY list refer to
nonaggregated columns that are neither named in the GROUP BY clause nor are functionally
dependent on (uniquely determined by) GROUP BY columns."
17
Translation
• This happens because the query is grouping by email and MySQL is magically trying to
“guess” which field it should bring as a result to the other columns (id, first_name, etc.)
since there are more than one result per row.
• This mode is known as ONLY_FULL_GROUP_BY.
18
More Info
Fix
AGGREGATORS!
MySQL Aggregators
• min(column)
• max(column)
• group_concat(column)
• count(column), count(DISTINCT column) or count(*)
• sum(column)
21
More Info
How are we used to doing it
SELECT * FROM users GROUP BY email HAVING count(*) > 1
<?php
// Doctrine
$qb = $entityManager->createQueryBuilder();
$repeatedUsers = $qb->select('*')
    ->from('users', 'u')
    ->groupBy('u.email')
    ->having('count(*) > 1');
// Eloquent
$repeatedUsers = Capsule::table('users')
    ->select('*')
    ->groupBy('email')
    ->havingRaw('count(*) > 1');
22
How we should do it
1st step: expanding the query
24
SELECT
id,
first_name,
last_name,
email,
created_at,
updated_at
FROM users
GROUP BY email
HAVING COUNT(*) > 1;
5.6
5.7
Works
Doesn’t Work
2nd step: adding aggregators
25
SELECT
group_concat(id) AS all_users_id,
first_name,
last_name,
email,
created_at,
updated_at
FROM users
GROUP BY email
HAVING COUNT(*) > 1;
5.6
5.7
Works
Doesn’t Work
2nd step - continued
26
SELECT
group_concat(id) AS all_users_id,
min(first_name) AS first_name,
last_name,
email,
created_at,
updated_at
FROM users
GROUP BY email
HAVING COUNT(*) > 1;
5.6
5.7
Works
Doesn’t Work
3rd step
27
SELECT
group_concat(id) AS all_user_ids,
min(first_name) AS first_name,
min(last_name) AS last_name,
email,
min(created_at) AS first_record_at,
max(updated_at) AS recent_changed_at
FROM users
GROUP BY email
HAVING COUNT(*) > 1;
5.6
5.7
Works
Works
Execution result
28
+--------------+------------+------------+---------------------+---------------------+---------------------+
| all_user_ids | first_name | last_name | email | first_record_at | recent_changed_at |
+--------------+------------+------------+---------------------+---------------------+---------------------+
| 20559,18046 | Guillermo | Lebsack | cdoyle@gmail.com | 2017-01-02 14:57:59 | 2017-01-02 14:58:03 |
| 99426,69772 | Alayna | Mante | nkuhlman@gmail.com | 2017-01-02 14:59:34 | 2017-01-02 15:00:28 |
| 95937,66525 | Braxton | Cormier | qgottlieb@gmail.com | 2017-01-02 14:59:29 | 2017-01-02 15:00:21 |
| 8965,48487 | Alfred | Quitzon | yjacobson@yahoo.com | 2017-01-02 14:57:39 | 2017-01-02 14:58:56 |
| 93458,92577 | Christy | McLaughlin | zzieme@yahoo.com | 2017-01-02 15:00:14 | 2017-01-02 15:00:16 |
+--------------+------------+------------+---------------------+---------------------+---------------------+
5 rows in set (0.05 sec)
3rd step - alternative: ANY_VALUE()
29
SELECT
group_concat(id) AS all_user_ids,
any_value(first_name) AS any_first_name,
any_value(last_name) AS any_last_name,
email,
min(created_at) AS first_record_at,
max(updated_at) AS recent_changed_at
FROM users
GROUP BY email
HAVING COUNT(*) > 1;
5.6
5.7
Doesn’t Work
Works
Generated Columns
Virtual and Stored Columns
• Can be indexed
• Allows expressions to be used such as:
• Operators: * , /, -, +
• Built-in functions: string functions, date time functions
• Literals: "A", 2, first_name
33
Virtual Columns
• No disk space
• INPLACE creation
• Values are generated on demand and BEFORE triggers
34
Stored Columns
• Uses disk space
• Uses table COPY operation on ADD COLUMN and DROP COLUMN
• Value updated on INSERT and UPDATE
35
Limitations on Generated Columns
• NOT NULL by default
• You must specify a type and be aware of the limitations
• Specifically changing the value of a generated column causes an error
• Subqueries and custom functions ARE NOT allowed
36
Example
Table `users`
Field Type Null Key Default Extra
id int(10) unsigned NO PRI auto_increment
first_name varchar(255) NO NULL
last_name varchar(255) NO NULL
email varchar(255) NO NULL
twitter_info json YES
created_at datetime NO CURRENT_TIMESTAMP
updated_at datetime NO CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP
38
-- Virtual
ALTER TABLE `users`
ADD COLUMN `full_name` VARCHAR (500)
GENERATED ALWAYS
AS (CONCAT_WS(' ', `first_name`, `last_name`))
AFTER `last_name`;
-- Executed in 124ms
-- Stored
ALTER TABLE `users`
ADD COLUMN `full_name` VARCHAR (500)
GENERATED ALWAYS
AS (CONCAT_WS(' ', `first_name`, `last_name`)) STORED
AFTER `last_name`;
-- 10000 rows affected. Executed in 2s 356ms
39
Table `users`
Field Type Null Key Default Extra
id int(10) unsigned NO PRI auto_increment
first_name varchar(255) NO NULL
last_name varchar(255) NO NULL
full_name varchar(500) YES STORED GENERATED
email varchar(255) NO NULL
twitter_info json YES
created_at datetime NO CURRENT_TIMESTAMP
updated_at datetime NO CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP
40
Other expressions
GENERATED ALWAYS AS (
• price * quantity
• SUBSTRING_INDEX(email, '@', -1)
• FROM_UNIXTIME(created_at)
• JSON_UNQUOTE(JSON_EXTRACT(twitter_info, ‘$.location’))
• twitter_info->>’$.location’
)
41
JSON
JSON
• Stored as Binary, not TEXT
• Indexing is possible only through Generated Columns (Virtual or Stored)
• Accessible by $key->'$.path' (JSON_EXTRACT alias)
• Modified by $key->>'$.path' returning utf8mb4 (JSON_UNQUOTE alias)
43
Converting TEXT to JSON
• It is costly, uses table COPY operation
• All rows must be valid, else an error occur
• Use JSON_VALID() before
44
Generated Columns for
JSON
`users`.`twitter_info`
46
Virtual Column for JSON: extraction
47
ALTER TABLE `users`
ADD COLUMN `profile_img` TEXT
GENERATED ALWAYS AS
(`twitter_info`->>’$.profile_image_url’)
AFTER `email`;
Stored Column: creation & index for JSON
48
ALTER TABLE `users`
ADD COLUMN `location` VARCHAR (255)
GENERATED ALWAYS AS
(`twitter_info`->>'$.location') STORED
AFTER `email`,
ADD INDEX `ix_location` (`location`);
Table `users`
Field Type Null Key Default Extra
… … … …
email varchar(255) NO NULL
location varchar(255) YES INDEX STORED GENERATED
profile_img text YES VIRTUAL GENERATED
twitter_info json YES
… … … …
49
Table `users`
SELECT
id, first_name, last_name, full_name, location, profile_img
FROM `users`
LIMIT 5;
+----+------------+-----------+------------------+-------------------------+---------------------+
| id | first_name | last_name | full_name | location | profile_img |
+----+------------+-----------+------------------+-------------------------+---------------------+
| 1 | Davey | Shafik | Davey Shafik | Seattle, Washington | http://pbs.twimg... |
| 2 | Gabriela | D'Avila | Gabriela D'Avila | Vitória, Brazil | http://pbs.twimg... |
| 3 | Anthony | Ferrara | Anthony Ferrara | New Jersey, USA | http://pbs.twimg... |
| 4 | Frank | de Jonge | Frank de Jonge | Amsterdam, North Holland| http://pbs.twimg... |
| 5 | Amanda | Folson | Amanda Folson | Probably on a plane. | http://pbs.twimg... |
+----+------------+-----------+------------------+-------------------------+---------------------+
5 rows in set (0.00 sec)
50
MySQL `sys` schema
MySQL `sys` schema
• Needs to be installed on MySQL 5.6
• Installed by default on MySQL 5.7
• MySQL Workbench 6.3 ships with a client for it
• In production should be used in critical query analysis case
52
What can I do with it
55
*High Cost SQL statements select * from sys.`x$statement_analysis`
Top 5% slower queries select * from sys.`x$statements_with_runtimes_in_95th_percentile`
Use temporary tables select * from sys.`statements_with_temp_tables`
Unused Indexes select * from sys.`schema_unused_indexes`
Full table scans select * from sys.`schema_tables_with_full_table_scans`
*x$ is a prefix for a view of the same name on raw format, for instance time is in picoseconds (10-12s
)
More Info
Other changes
Other Changes
• Passwords can now have expiration date
• NO_ZERO_DATE, NO_ZERO_IN_DATE, ERROR_FOR_DIVISION_BY_ZERO are deprecated
and being default in STRICT mode in future versions (8.0?)
• Tables now support more than one trigger by event
• YEAR(2) was deprecated on 5.6, removed on 5.7 (no more YY-MM-DD!)
57
Thank you!
• Twitter: @gabidavila
• Blog: http://gabriela.io
• Freenode: gabidavila
• Feedback: https://joind.in/talk/cc6d1
• References: MySQL documentation
58

Mais conteúdo relacionado

Mais procurados

Apache CouchDB talk at Ontario GNU Linux Fest
Apache CouchDB talk at Ontario GNU Linux FestApache CouchDB talk at Ontario GNU Linux Fest
Apache CouchDB talk at Ontario GNU Linux Fest
Myles Braithwaite
 
Hd insight programming
Hd insight programmingHd insight programming
Hd insight programming
Casear Chu
 
MongoDB Performance Debugging
MongoDB Performance DebuggingMongoDB Performance Debugging
MongoDB Performance Debugging
MongoDB
 

Mais procurados (20)

Practical JSON in MySQL 5.7
Practical JSON in MySQL 5.7Practical JSON in MySQL 5.7
Practical JSON in MySQL 5.7
 
Practical JSON in MySQL 5.7 and Beyond
Practical JSON in MySQL 5.7 and BeyondPractical JSON in MySQL 5.7 and Beyond
Practical JSON in MySQL 5.7 and Beyond
 
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
 
Introduction to Elasticsearch
Introduction to ElasticsearchIntroduction to Elasticsearch
Introduction to Elasticsearch
 
High Performance XQuery Processing in PHP with Zorba by Vikram Vaswani
High Performance XQuery Processing in PHP with Zorba by Vikram VaswaniHigh Performance XQuery Processing in PHP with Zorba by Vikram Vaswani
High Performance XQuery Processing in PHP with Zorba by Vikram Vaswani
 
Back to Basics Webinar 5: Introduction to the Aggregation Framework
Back to Basics Webinar 5: Introduction to the Aggregation FrameworkBack to Basics Webinar 5: Introduction to the Aggregation Framework
Back to Basics Webinar 5: Introduction to the Aggregation Framework
 
Cassandra 2.2 & 3.0
Cassandra 2.2 & 3.0Cassandra 2.2 & 3.0
Cassandra 2.2 & 3.0
 
Fazendo mágica com ElasticSearch
Fazendo mágica com ElasticSearchFazendo mágica com ElasticSearch
Fazendo mágica com ElasticSearch
 
Apache CouchDB talk at Ontario GNU Linux Fest
Apache CouchDB talk at Ontario GNU Linux FestApache CouchDB talk at Ontario GNU Linux Fest
Apache CouchDB talk at Ontario GNU Linux Fest
 
PostgreSQL 9.4 JSON Types and Operators
PostgreSQL 9.4 JSON Types and OperatorsPostgreSQL 9.4 JSON Types and Operators
PostgreSQL 9.4 JSON Types and Operators
 
Inside MongoDB: the Internals of an Open-Source Database
Inside MongoDB: the Internals of an Open-Source DatabaseInside MongoDB: the Internals of an Open-Source Database
Inside MongoDB: the Internals of an Open-Source Database
 
Hd insight programming
Hd insight programmingHd insight programming
Hd insight programming
 
Indexing
IndexingIndexing
Indexing
 
CQL performance with Apache Cassandra 3.0 (Aaron Morton, The Last Pickle) | C...
CQL performance with Apache Cassandra 3.0 (Aaron Morton, The Last Pickle) | C...CQL performance with Apache Cassandra 3.0 (Aaron Morton, The Last Pickle) | C...
CQL performance with Apache Cassandra 3.0 (Aaron Morton, The Last Pickle) | C...
 
MongoDB Performance Debugging
MongoDB Performance DebuggingMongoDB Performance Debugging
MongoDB Performance Debugging
 
MySQL 8.0: not only good, it’s GREAT! - PHP UK 2019
MySQL 8.0: not only good, it’s GREAT! - PHP UK 2019MySQL 8.0: not only good, it’s GREAT! - PHP UK 2019
MySQL 8.0: not only good, it’s GREAT! - PHP UK 2019
 
Php forum2015 tomas_final
Php forum2015 tomas_finalPhp forum2015 tomas_final
Php forum2015 tomas_final
 
DPC18 - Making the most out of MySQL
DPC18 - Making the most out of MySQLDPC18 - Making the most out of MySQL
DPC18 - Making the most out of MySQL
 
Beyond PHP - It's not (just) about the code
Beyond PHP - It's not (just) about the codeBeyond PHP - It's not (just) about the code
Beyond PHP - It's not (just) about the code
 
MongoDB - Aggregation Pipeline
MongoDB - Aggregation PipelineMongoDB - Aggregation Pipeline
MongoDB - Aggregation Pipeline
 

Destaque

Ora mysql bothGetting the best of both worlds with Oracle 11g and MySQL Enter...
Ora mysql bothGetting the best of both worlds with Oracle 11g and MySQL Enter...Ora mysql bothGetting the best of both worlds with Oracle 11g and MySQL Enter...
Ora mysql bothGetting the best of both worlds with Oracle 11g and MySQL Enter...
Ivan Zoratti
 

Destaque (20)

Ora mysql bothGetting the best of both worlds with Oracle 11g and MySQL Enter...
Ora mysql bothGetting the best of both worlds with Oracle 11g and MySQL Enter...Ora mysql bothGetting the best of both worlds with Oracle 11g and MySQL Enter...
Ora mysql bothGetting the best of both worlds with Oracle 11g and MySQL Enter...
 
Exploring MongoDB & Elasticsearch: Better Together
Exploring MongoDB & Elasticsearch: Better TogetherExploring MongoDB & Elasticsearch: Better Together
Exploring MongoDB & Elasticsearch: Better Together
 
Sharding using MySQL and PHP
Sharding using MySQL and PHPSharding using MySQL and PHP
Sharding using MySQL and PHP
 
Coding like a girl - DjangoCon
Coding like a girl - DjangoConCoding like a girl - DjangoCon
Coding like a girl - DjangoCon
 
Inexpensive Datamasking for MySQL with ProxySQL — Data Anonymization for Deve...
Inexpensive Datamasking for MySQL with ProxySQL — Data Anonymization for Deve...Inexpensive Datamasking for MySQL with ProxySQL — Data Anonymization for Deve...
Inexpensive Datamasking for MySQL with ProxySQL — Data Anonymization for Deve...
 
Strip your TEXT fields
Strip your TEXT fieldsStrip your TEXT fields
Strip your TEXT fields
 
MySQL Sharding: Tools and Best Practices for Horizontal Scaling
MySQL Sharding: Tools and Best Practices for Horizontal ScalingMySQL Sharding: Tools and Best Practices for Horizontal Scaling
MySQL Sharding: Tools and Best Practices for Horizontal Scaling
 
MEAN Stack
MEAN StackMEAN Stack
MEAN Stack
 
Mongodb
MongodbMongodb
Mongodb
 
The MySQL Server Ecosystem in 2016
The MySQL Server Ecosystem in 2016The MySQL Server Ecosystem in 2016
The MySQL Server Ecosystem in 2016
 
Strip your TEXT fields - Exeter Web Feb/2016
Strip your TEXT fields - Exeter Web Feb/2016Strip your TEXT fields - Exeter Web Feb/2016
Strip your TEXT fields - Exeter Web Feb/2016
 
MySQL 5.7 - 
Tirando o Máximo Proveito
MySQL 5.7 - 
Tirando o Máximo ProveitoMySQL 5.7 - 
Tirando o Máximo Proveito
MySQL 5.7 - 
Tirando o Máximo Proveito
 
Building Scalable High Availability Systems using MySQL Fabric
Building Scalable High Availability Systems using MySQL FabricBuilding Scalable High Availability Systems using MySQL Fabric
Building Scalable High Availability Systems using MySQL Fabric
 
MySQL Enterprise Cloud
MySQL Enterprise Cloud MySQL Enterprise Cloud
MySQL Enterprise Cloud
 
20171104 hk-py con-mysql-documentstore_v1
20171104 hk-py con-mysql-documentstore_v120171104 hk-py con-mysql-documentstore_v1
20171104 hk-py con-mysql-documentstore_v1
 
[스마트스터디]MongoDB 의 역습
[스마트스터디]MongoDB 의 역습[스마트스터디]MongoDB 의 역습
[스마트스터디]MongoDB 의 역습
 
Laravel 5 and SOLID
Laravel 5 and SOLIDLaravel 5 and SOLID
Laravel 5 and SOLID
 
LAMP: Desenvolvendo além do trivial
LAMP: Desenvolvendo além do trivialLAMP: Desenvolvendo além do trivial
LAMP: Desenvolvendo além do trivial
 
LaravelSP - MySQL 5.7: introdução ao JSON Data Type
LaravelSP - MySQL 5.7: introdução ao JSON Data TypeLaravelSP - MySQL 5.7: introdução ao JSON Data Type
LaravelSP - MySQL 5.7: introdução ao JSON Data Type
 
MySQL Cluster Whats New
MySQL Cluster Whats NewMySQL Cluster Whats New
MySQL Cluster Whats New
 

Semelhante a SunshinePHP 2017 - Making the most out of MySQL

15 protips for mysql users pfz
15 protips for mysql users   pfz15 protips for mysql users   pfz
15 protips for mysql users pfz
Joshua Thijssen
 
Short Intro to PHP and MySQL
Short Intro to PHP and MySQLShort Intro to PHP and MySQL
Short Intro to PHP and MySQL
Jussi Pohjolainen
 
Applied Partitioning And Scaling Your Database System Presentation
Applied Partitioning And Scaling Your Database System PresentationApplied Partitioning And Scaling Your Database System Presentation
Applied Partitioning And Scaling Your Database System Presentation
Richard Crowley
 

Semelhante a SunshinePHP 2017 - Making the most out of MySQL (20)

php[tek] - Making the most out of MySQL
php[tek] - Making the most out of MySQLphp[tek] - Making the most out of MySQL
php[tek] - Making the most out of MySQL
 
Introduction To Lamp P2
Introduction To Lamp P2Introduction To Lamp P2
Introduction To Lamp P2
 
Mysql basics1
Mysql basics1Mysql basics1
Mysql basics1
 
What is MariaDB Server 10.3?
What is MariaDB Server 10.3?What is MariaDB Server 10.3?
What is MariaDB Server 10.3?
 
15 protips for mysql users pfz
15 protips for mysql users   pfz15 protips for mysql users   pfz
15 protips for mysql users pfz
 
MariaDB for Developers and Operators (DevOps)
MariaDB for Developers and Operators (DevOps)MariaDB for Developers and Operators (DevOps)
MariaDB for Developers and Operators (DevOps)
 
Short Intro to PHP and MySQL
Short Intro to PHP and MySQLShort Intro to PHP and MySQL
Short Intro to PHP and MySQL
 
Introduction into MySQL Query Tuning
Introduction into MySQL Query TuningIntroduction into MySQL Query Tuning
Introduction into MySQL Query Tuning
 
Need for Speed: MySQL Indexing
Need for Speed: MySQL IndexingNeed for Speed: MySQL Indexing
Need for Speed: MySQL Indexing
 
4. Data Manipulation.ppt
4. Data Manipulation.ppt4. Data Manipulation.ppt
4. Data Manipulation.ppt
 
5_MariaDB_What's New in MariaDB Server 10.2 and Big Data Analytics with Maria...
5_MariaDB_What's New in MariaDB Server 10.2 and Big Data Analytics with Maria...5_MariaDB_What's New in MariaDB Server 10.2 and Big Data Analytics with Maria...
5_MariaDB_What's New in MariaDB Server 10.2 and Big Data Analytics with Maria...
 
Why Use EXPLAIN FORMAT=JSON?
 Why Use EXPLAIN FORMAT=JSON?  Why Use EXPLAIN FORMAT=JSON?
Why Use EXPLAIN FORMAT=JSON?
 
Applied Partitioning And Scaling Your Database System Presentation
Applied Partitioning And Scaling Your Database System PresentationApplied Partitioning And Scaling Your Database System Presentation
Applied Partitioning And Scaling Your Database System Presentation
 
MySQL Kitchen : spice up your everyday SQL queries
MySQL Kitchen : spice up your everyday SQL queriesMySQL Kitchen : spice up your everyday SQL queries
MySQL Kitchen : spice up your everyday SQL queries
 
Getting started into mySQL
Getting started into mySQLGetting started into mySQL
Getting started into mySQL
 
Ten Reasons Why You Should Prefer PostgreSQL to MySQL
Ten Reasons Why You Should Prefer PostgreSQL to MySQLTen Reasons Why You Should Prefer PostgreSQL to MySQL
Ten Reasons Why You Should Prefer PostgreSQL to MySQL
 
DEF CON 27 -OMER GULL - select code execution from using sq lite
DEF CON 27 -OMER GULL - select code execution from using sq liteDEF CON 27 -OMER GULL - select code execution from using sq lite
DEF CON 27 -OMER GULL - select code execution from using sq lite
 
15 MySQL Basics #burningkeyboards
15 MySQL Basics #burningkeyboards15 MySQL Basics #burningkeyboards
15 MySQL Basics #burningkeyboards
 
Introduction to MySQL Query Tuning for Dev[Op]s
Introduction to MySQL Query Tuning for Dev[Op]sIntroduction to MySQL Query Tuning for Dev[Op]s
Introduction to MySQL Query Tuning for Dev[Op]s
 
Query Optimization with MySQL 5.6: Old and New Tricks
Query Optimization with MySQL 5.6: Old and New TricksQuery Optimization with MySQL 5.6: Old and New Tricks
Query Optimization with MySQL 5.6: Old and New Tricks
 

Mais de Gabriela Ferrara

Mais de Gabriela Ferrara (9)

GRONINGEN PHP - MySQL 8.0 , not only good, great
GRONINGEN PHP - MySQL 8.0 , not only good, greatGRONINGEN PHP - MySQL 8.0 , not only good, great
GRONINGEN PHP - MySQL 8.0 , not only good, great
 
Serverless and you @ Women Who Code London 2020
Serverless and you  @ Women Who Code London 2020Serverless and you  @ Women Who Code London 2020
Serverless and you @ Women Who Code London 2020
 
Serverless and you - where do i run my stateless code
Serverless and you  - where do i run my stateless codeServerless and you  - where do i run my stateless code
Serverless and you - where do i run my stateless code
 
PHPDay 2019 - MySQL 8, not only good, great!
PHPDay 2019 - MySQL 8, not only good, great!PHPDay 2019 - MySQL 8, not only good, great!
PHPDay 2019 - MySQL 8, not only good, great!
 
PyTexas - Machine learning APIs by Example
PyTexas - Machine learning APIs by ExamplePyTexas - Machine learning APIs by Example
PyTexas - Machine learning APIs by Example
 
Laracon EU 2018: OMG MySQL 8.0 is out! are we there yet?
Laracon EU 2018: OMG MySQL 8.0 is out! are we there yet?Laracon EU 2018: OMG MySQL 8.0 is out! are we there yet?
Laracon EU 2018: OMG MySQL 8.0 is out! are we there yet?
 
DPC18 - OMG MySQL 8.0 is out! are we there yet?
DPC18 - OMG MySQL 8.0 is out! are we there yet?DPC18 - OMG MySQL 8.0 is out! are we there yet?
DPC18 - OMG MySQL 8.0 is out! are we there yet?
 
Coding like a girl - Youtube presentation
Coding like a girl - Youtube presentationCoding like a girl - Youtube presentation
Coding like a girl - Youtube presentation
 
Coding like a Girl
Coding like a GirlCoding like a Girl
Coding like a Girl
 

Último

Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRLLucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
imonikaupta
 
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
Call Girls In Delhi Whatsup 9873940964 Enjoy Unlimited Pleasure
 
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Chandigarh Call girls 9053900678 Call girls in Chandigarh
 
💚😋 Bilaspur Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋
💚😋 Bilaspur Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋💚😋 Bilaspur Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋
💚😋 Bilaspur Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋
nirzagarg
 
( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...
( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...
( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...
nilamkumrai
 
📱Dehradun Call Girls Service 📱☎️ +91'905,3900,678 ☎️📱 Call Girls In Dehradun 📱
📱Dehradun Call Girls Service 📱☎️ +91'905,3900,678 ☎️📱 Call Girls In Dehradun 📱📱Dehradun Call Girls Service 📱☎️ +91'905,3900,678 ☎️📱 Call Girls In Dehradun 📱
📱Dehradun Call Girls Service 📱☎️ +91'905,3900,678 ☎️📱 Call Girls In Dehradun 📱
@Chandigarh #call #Girls 9053900678 @Call #Girls in @Punjab 9053900678
 

Último (20)

Busty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort ServiceBusty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort Service
 
Katraj ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For S...
Katraj ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For S...Katraj ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For S...
Katraj ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For S...
 
Al Barsha Night Partner +0567686026 Call Girls Dubai
Al Barsha Night Partner +0567686026 Call Girls  DubaiAl Barsha Night Partner +0567686026 Call Girls  Dubai
Al Barsha Night Partner +0567686026 Call Girls Dubai
 
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRLLucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
 
(+971568250507 ))# Young Call Girls in Ajman By Pakistani Call Girls in ...
(+971568250507  ))#  Young Call Girls  in Ajman  By Pakistani Call Girls  in ...(+971568250507  ))#  Young Call Girls  in Ajman  By Pakistani Call Girls  in ...
(+971568250507 ))# Young Call Girls in Ajman By Pakistani Call Girls in ...
 
APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53
 
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
 
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
 
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
 
Real Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirtReal Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirt
 
"Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency""Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency"
 
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls DubaiDubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
 
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
 
💚😋 Bilaspur Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋
💚😋 Bilaspur Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋💚😋 Bilaspur Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋
💚😋 Bilaspur Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋
 
Pirangut | Call Girls Pune Phone No 8005736733 Elite Escort Service Available...
Pirangut | Call Girls Pune Phone No 8005736733 Elite Escort Service Available...Pirangut | Call Girls Pune Phone No 8005736733 Elite Escort Service Available...
Pirangut | Call Girls Pune Phone No 8005736733 Elite Escort Service Available...
 
Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...
Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...
Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...
 
( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...
( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...
( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...
 
Yerawada ] Independent Escorts in Pune - Book 8005736733 Call Girls Available...
Yerawada ] Independent Escorts in Pune - Book 8005736733 Call Girls Available...Yerawada ] Independent Escorts in Pune - Book 8005736733 Call Girls Available...
Yerawada ] Independent Escorts in Pune - Book 8005736733 Call Girls Available...
 
📱Dehradun Call Girls Service 📱☎️ +91'905,3900,678 ☎️📱 Call Girls In Dehradun 📱
📱Dehradun Call Girls Service 📱☎️ +91'905,3900,678 ☎️📱 Call Girls In Dehradun 📱📱Dehradun Call Girls Service 📱☎️ +91'905,3900,678 ☎️📱 Call Girls In Dehradun 📱
📱Dehradun Call Girls Service 📱☎️ +91'905,3900,678 ☎️📱 Call Girls In Dehradun 📱
 
Nanded City ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready ...
Nanded City ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready ...Nanded City ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready ...
Nanded City ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready ...
 

SunshinePHP 2017 - Making the most out of MySQL

  • 1. Making the most out of MySQL Gabriela D’Avila
 @gabidavila Feb/2017
  • 2. Who? • Data Engineer • Lego Hoarder • @gabidavila • http://gabriela.io 2
  • 3. What to Expect? • DDL Changes • SQL Modes • Generated Columns • JSON Data Type • `sys` Schema 3
  • 4. DDL Changes on InnoDB
  • 5. DDL Changes • In Place (ALGORITHM=INPLACE) • Rename Index • VARCHAR from 1B to 255B • VARCHAR from 256B to 65353B* • Add a Virtual Column • Table-copy (ALGORITHM=COPY) • Add a Column • VARCHAR between 255B and >=256 • Decreasing VARCHAR size • Type Conversion 5 More Info
  • 7. SQL modes (SELECT @@GLOBAL.sql_mode;) 7 On MySQL 5.6: +--------------------------------------------+ | @@GLOBAL.sql_mode | +--------------------------------------------+ | STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION | +--------------------------------------------+ 1 row in set (0.00 sec) On MySQL 5.7: +-----------------------------------------------------------------------+ | @@GLOBAL.sql_mode | +-----------------------------------------------------------------------+ | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE, |
 | ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION | +-----------------------------------------------------------------------+ 1 row in set (0.00 sec)
  • 8. SQL modes comparison On MySQL 5.6.35: • STRICT_TRANS_TABLES (Bug #69743) • NO_ENGINE_SUBSTITUTION On MySQL 5.7.17: • STRICT_TRANS_TABLES • NO_ENGINE_SUBSTITUTION • ONLY_FULL_GROUP_BY • NO_ZERO_IN_DATE* • NO_ZERO_DATE* • ERROR_FOR_DIVISION_BY_ZERO* 8 Full list of sql_mode
  • 9. YYYY-MM-DD • NO_ZERO_DATE • 0000-00-00 👎 • 0000-00-00 00:00:00 👎 • NO_ZERO_IN_DATE • 2017-01-00 👎 • 2017-00-04 👎 • 0000-01-04 👍 9
  • 10. ERROR_FOR_DIVISION_BY_ZERO 10 mysql> UPDATE users SET id = id/0 WHERE id = 2; ERROR 1365 (22012): Division by 0 • Write Operation: UPDATE mysql> SELECT id, mod(id,0) FROM users WHERE id = 2; +----+-----------+ | id | mod(id,0) | +----+-----------+ | 2 | NULL | +----+-----------+ 1 row in set, 1 warning (0.00 sec) • Read Operation: SELECT mysql> SHOW WARNINGS; +---------+------+---------------+ | Level | Code | Message | +---------+------+---------------+ | Warning | 1365 | Division by 0 | +---------+------+---------------+ 1 row in set (0.00 sec)
  • 11.
  • 13. Table `users` Field Type Null Key Default Extra id int(10) unsigned NO PRI auto_increment first_name varchar(255) NO NULL last_name varchar(255) NO NULL email varchar(255) NO NULL twitter_info json YES created_at datetime NO CURRENT_TIMESTAMP updated_at datetime NO CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP 13
  • 14. Repeated emails SELECT * FROM users GROUP BY email HAVING count(*) > 1 +-------+------------+-----------+---------------------+-------------------+---------------------+---------------------+ | id | first_name | last_name | email | twitter_info | created_at | updated_at | +-------+------------+-----------+---------------------+-------------------+---------------------+---------------------+ | 8965 | Conor | Quitzon | yjacobson@yahoo.com | {"id": 100838242, | 2017-01-02 20:57:39 | 2017-01-02 20:57:39 | | 69772 | Junius | Mante | nkuhlman@gmail.com | {"id": 20039476, | 2017-01-02 20:59:34 | 2017-01-02 20:59:34 | | 66525 | Katelynn | Feil | qgottlieb@gmail.com | {"id": 111644778, | 2017-01-02 20:59:29 | 2017-01-02 20:59:29 | | 92577 | Tillman | Nienow | zzieme@yahoo.com | {"id": 1359073920,| 2017-01-02 21:00:14 | 2017-01-02 21:00:14 | | 18046 | Guillermo | Lebsack | cdoyle@gmail.com | {"id": 120064855, | 2017-01-02 20:57:59 | 2017-01-02 20:57:59 | +-------+------------+-----------+---------------------+-------------------+---------------------+---------------------+ 5 rows in set (0.65 sec) 5.6 14
  • 15. Repeated emails SELECT * FROM users WHERE email = 'cdoyle@gmail.com' +-------+------------+-----------+------------------+--------------+---------------------+---------------------+ | id | first_name | last_name | email | twitter_info | created_at | updated_at | +-------+------------+-----------+------------------+--------------+---------------------+---------------------+ | 18046 | Guillermo | Lebsack | cdoyle@gmail.com | NULL | 2017-01-02 14:57:59 | 2017-01-02 14:57:59 | | 20559 | Marietta | Quitzon | cdoyle@gmail.com | NULL | 2017-01-02 14:58:03 | 2017-01-02 14:58:03 | +-------+------------+-----------+------------------+--------------+---------------------+---------------------+ 2 rows in set (0.04 sec) 15
  • 16. Repeated emails SELECT * FROM users GROUP BY email HAVING count(*) > 1 mysql> SELECT * FROM `users` GROUP BY `email` HAVING count(*) > 1; ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘store.users.id’ which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by 5.7 16
  • 17. Why? "Reject queries for which the select list, HAVING condition, or ORDER BY list refer to nonaggregated columns that are neither named in the GROUP BY clause nor are functionally dependent on (uniquely determined by) GROUP BY columns." 17
  • 18. Translation • This happens because the query is grouping by email and MySQL is magically trying to “guess” which field it should bring as a result to the other columns (id, first_name, etc.) since there are more than one result per row. • This mode is known as ONLY_FULL_GROUP_BY. 18 More Info
  • 19. Fix
  • 21. MySQL Aggregators • min(column) • max(column) • group_concat(column) • count(column), count(DISTINCT column) or count(*) • sum(column) 21 More Info
  • 22. How are we used to doing it SELECT * FROM users GROUP BY email HAVING count(*) > 1 <?php // Doctrine $qb = $entityManager->createQueryBuilder(); $repeatedUsers = $qb->select('*')     ->from('users', 'u')     ->groupBy('u.email')     ->having('count(*) > 1'); // Eloquent $repeatedUsers = Capsule::table('users')     ->select('*')     ->groupBy('email')     ->havingRaw('count(*) > 1'); 22
  • 23. How we should do it
  • 24. 1st step: expanding the query 24 SELECT id, first_name, last_name, email, created_at, updated_at FROM users GROUP BY email HAVING COUNT(*) > 1; 5.6 5.7 Works Doesn’t Work
  • 25. 2nd step: adding aggregators 25 SELECT group_concat(id) AS all_users_id, first_name, last_name, email, created_at, updated_at FROM users GROUP BY email HAVING COUNT(*) > 1; 5.6 5.7 Works Doesn’t Work
  • 26. 2nd step - continued 26 SELECT group_concat(id) AS all_users_id, min(first_name) AS first_name, last_name, email, created_at, updated_at FROM users GROUP BY email HAVING COUNT(*) > 1; 5.6 5.7 Works Doesn’t Work
  • 27. 3rd step 27 SELECT group_concat(id) AS all_user_ids, min(first_name) AS first_name, min(last_name) AS last_name, email, min(created_at) AS first_record_at, max(updated_at) AS recent_changed_at FROM users GROUP BY email HAVING COUNT(*) > 1; 5.6 5.7 Works Works
  • 28. Execution result 28 +--------------+------------+------------+---------------------+---------------------+---------------------+ | all_user_ids | first_name | last_name | email | first_record_at | recent_changed_at | +--------------+------------+------------+---------------------+---------------------+---------------------+ | 20559,18046 | Guillermo | Lebsack | cdoyle@gmail.com | 2017-01-02 14:57:59 | 2017-01-02 14:58:03 | | 99426,69772 | Alayna | Mante | nkuhlman@gmail.com | 2017-01-02 14:59:34 | 2017-01-02 15:00:28 | | 95937,66525 | Braxton | Cormier | qgottlieb@gmail.com | 2017-01-02 14:59:29 | 2017-01-02 15:00:21 | | 8965,48487 | Alfred | Quitzon | yjacobson@yahoo.com | 2017-01-02 14:57:39 | 2017-01-02 14:58:56 | | 93458,92577 | Christy | McLaughlin | zzieme@yahoo.com | 2017-01-02 15:00:14 | 2017-01-02 15:00:16 | +--------------+------------+------------+---------------------+---------------------+---------------------+ 5 rows in set (0.05 sec)
  • 29. 3rd step - alternative: ANY_VALUE() 29 SELECT group_concat(id) AS all_user_ids, any_value(first_name) AS any_first_name, any_value(last_name) AS any_last_name, email, min(created_at) AS first_record_at, max(updated_at) AS recent_changed_at FROM users GROUP BY email HAVING COUNT(*) > 1; 5.6 5.7 Doesn’t Work Works
  • 30.
  • 31.
  • 33. Virtual and Stored Columns • Can be indexed • Allows expressions to be used such as: • Operators: * , /, -, + • Built-in functions: string functions, date time functions • Literals: "A", 2, first_name 33
  • 34. Virtual Columns • No disk space • INPLACE creation • Values are generated on demand and BEFORE triggers 34
  • 35. Stored Columns • Uses disk space • Uses table COPY operation on ADD COLUMN and DROP COLUMN • Value updated on INSERT and UPDATE 35
  • 36. Limitations on Generated Columns • NOT NULL by default • You must specify a type and be aware of the limitations • Specifically changing the value of a generated column causes an error • Subqueries and custom functions ARE NOT allowed 36
  • 38. Table `users` Field Type Null Key Default Extra id int(10) unsigned NO PRI auto_increment first_name varchar(255) NO NULL last_name varchar(255) NO NULL email varchar(255) NO NULL twitter_info json YES created_at datetime NO CURRENT_TIMESTAMP updated_at datetime NO CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP 38
  • 39. -- Virtual ALTER TABLE `users` ADD COLUMN `full_name` VARCHAR (500) GENERATED ALWAYS AS (CONCAT_WS(' ', `first_name`, `last_name`)) AFTER `last_name`; -- Executed in 124ms -- Stored ALTER TABLE `users` ADD COLUMN `full_name` VARCHAR (500) GENERATED ALWAYS AS (CONCAT_WS(' ', `first_name`, `last_name`)) STORED AFTER `last_name`; -- 10000 rows affected. Executed in 2s 356ms 39
  • 40. Table `users` Field Type Null Key Default Extra id int(10) unsigned NO PRI auto_increment first_name varchar(255) NO NULL last_name varchar(255) NO NULL full_name varchar(500) YES STORED GENERATED email varchar(255) NO NULL twitter_info json YES created_at datetime NO CURRENT_TIMESTAMP updated_at datetime NO CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP 40
  • 41. Other expressions GENERATED ALWAYS AS ( • price * quantity • SUBSTRING_INDEX(email, '@', -1) • FROM_UNIXTIME(created_at) • JSON_UNQUOTE(JSON_EXTRACT(twitter_info, ‘$.location’)) • twitter_info->>’$.location’ ) 41
  • 42. JSON
  • 43. JSON • Stored as Binary, not TEXT • Indexing is possible only through Generated Columns (Virtual or Stored) • Accessible by $key->'$.path' (JSON_EXTRACT alias) • Modified by $key->>'$.path' returning utf8mb4 (JSON_UNQUOTE alias) 43
  • 44. Converting TEXT to JSON • It is costly, uses table COPY operation • All rows must be valid, else an error occur • Use JSON_VALID() before 44
  • 47. Virtual Column for JSON: extraction 47 ALTER TABLE `users` ADD COLUMN `profile_img` TEXT GENERATED ALWAYS AS (`twitter_info`->>’$.profile_image_url’) AFTER `email`;
  • 48. Stored Column: creation & index for JSON 48 ALTER TABLE `users` ADD COLUMN `location` VARCHAR (255) GENERATED ALWAYS AS (`twitter_info`->>'$.location') STORED AFTER `email`, ADD INDEX `ix_location` (`location`);
  • 49. Table `users` Field Type Null Key Default Extra … … … … email varchar(255) NO NULL location varchar(255) YES INDEX STORED GENERATED profile_img text YES VIRTUAL GENERATED twitter_info json YES … … … … 49
  • 50. Table `users` SELECT id, first_name, last_name, full_name, location, profile_img FROM `users` LIMIT 5; +----+------------+-----------+------------------+-------------------------+---------------------+ | id | first_name | last_name | full_name | location | profile_img | +----+------------+-----------+------------------+-------------------------+---------------------+ | 1 | Davey | Shafik | Davey Shafik | Seattle, Washington | http://pbs.twimg... | | 2 | Gabriela | D'Avila | Gabriela D'Avila | Vitória, Brazil | http://pbs.twimg... | | 3 | Anthony | Ferrara | Anthony Ferrara | New Jersey, USA | http://pbs.twimg... | | 4 | Frank | de Jonge | Frank de Jonge | Amsterdam, North Holland| http://pbs.twimg... | | 5 | Amanda | Folson | Amanda Folson | Probably on a plane. | http://pbs.twimg... | +----+------------+-----------+------------------+-------------------------+---------------------+ 5 rows in set (0.00 sec) 50
  • 52. MySQL `sys` schema • Needs to be installed on MySQL 5.6 • Installed by default on MySQL 5.7 • MySQL Workbench 6.3 ships with a client for it • In production should be used in critical query analysis case 52
  • 53.
  • 54.
  • 55. What can I do with it 55 *High Cost SQL statements select * from sys.`x$statement_analysis` Top 5% slower queries select * from sys.`x$statements_with_runtimes_in_95th_percentile` Use temporary tables select * from sys.`statements_with_temp_tables` Unused Indexes select * from sys.`schema_unused_indexes` Full table scans select * from sys.`schema_tables_with_full_table_scans` *x$ is a prefix for a view of the same name on raw format, for instance time is in picoseconds (10-12s ) More Info
  • 57. Other Changes • Passwords can now have expiration date • NO_ZERO_DATE, NO_ZERO_IN_DATE, ERROR_FOR_DIVISION_BY_ZERO are deprecated and being default in STRICT mode in future versions (8.0?) • Tables now support more than one trigger by event • YEAR(2) was deprecated on 5.6, removed on 5.7 (no more YY-MM-DD!) 57
  • 58. Thank you! • Twitter: @gabidavila • Blog: http://gabriela.io • Freenode: gabidavila • Feedback: https://joind.in/talk/cc6d1 • References: MySQL documentation 58