Video: https://www.youtube.com/watch?v=DlVESXLNyAc
The new JSON fields are some of the most talking about new features in MySQL 5.7. But they are by no means the only awesome things this version has to offer. MySQL 5.7 is about a year old, so this talk won't be an introduction to this version. We will be digging into 5.7 to see how to make the most of the tools available in it.
Want to tackle important practical problem solving for your data, make your query performance analysis more efficient or look at how virtual columns can help you index data? This talk is for you!
5. • In Place (ALGORITHM=INPLACE)
• Rename Index
• VARCHAR from 1 to 255
• Add a Virtual Column
• Table-copy (ALGORITHM=COPY)
• VARCHAR from 256 to 65535
• Add a Column
• Type Conversion
Online DDL Changes
5
More Info
7. SQL modes (SELECT @@GLOBAL.sql_mode;)
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)
On MySQL 5.7:
On MySQL 5.6:
+--------------------------------------------+
| @@GLOBAL.sql_mode |
+--------------------------------------------+
| STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION |
+--------------------------------------------+
1 row in set (0.00 sec)
8. SQL modes comparison
On MySQL 5.6:
• STRICT_TRANS_TABLES
• NO_ENGINE_SUBSTITUTION
On MySQL 5.7
• 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
10. Users Table
10
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
created_at datetime NO CURRENT_TIMESTAMP
updated_at datetime NO CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP
12. Repeated emails
12
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
13. "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."
Why?
13
14. This happens because the query is grouping by email and
MySQL is magically trying to “discover” which field should it
bring to the result to the other values since there are more than
one result per row.
This mode is known as ONLY_FULL_GROUP_BY.
Translation
14
More Info
22. • No disk space
• INPLACE creation
• Value generated on demand and
• BEFORE triggers
Virtual Columns
22
23. • Uses disk space
• Value updated on INSERT and UPDATE
• Uses table COPY on creation and removal
Stored Columns
23
24. • Can be indexed
• Allows expressions to be used such as:
• Operators
• Built-in functions
• Literals
• Subqueries ARE NOT allowed
Virtual and Stored Columns
24
31. • Stored as a Binary not TEXT
• TEXT fields can be converted*
• Accessible by $key->value (JSON_EXTRACT alias)
JSON
31
32. • It is costly, it is a table COPY operation
• All rows must be valid, else an error occur
• Index is possible through Generated Columns
Converting TEXT to JSON
32
33. `twitter_users` structure
33
Field Type Null Key Default Extra
id int(11) NO PRI auto_increment
id_str varchar(255) NO UNI
screen_name varchar(255) NO INDEX
response json YES
created_at datetime NO CURRENT_TIMESTAMP
updated_at datetime NO CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP
36. DESCRIBE `users`;
36
Field Type Null Key Default Extra
id int(11) NO PRI auto_increment
id_str varchar(255) NO UNI
screen_name varchar(255) NO INDEX
location varchar(255) YES INDEX STORED GENERATED
profile_image_url varchar(255) YES VIRTUAL GENERATED
response json YES
created_at datetime NO CURRENT_TIMESTAMP
updated_at datetime NO CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP
39. • Needs to be installed on 5.6
• Installed by default on 5.7
• MySQL Workbench 6.3 ships with a client for it
• In production should be used in critical query analysis case
MySQL `sys` schema
39
40.
41. What I can do with it?
41
*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 actually a view duplicated from a table with a more user friendly format
44. • 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
• Triggers now supports more than one event per table
• YEAR(2) was deprecated on 5.6, removed on 5.7
Other Changes
44