Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
MySQL 5.7 & JSON: New
Opportunities for Developers
Tomas Ulin, VP MySQL Engineering, Oracle
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Safe Harbor Statement
The following is intended to outline our general product direction. It is intended for
information purposes only, and may not be incorporated into any contract. It is not a
commitment to deliver any material, code, or functionality, and should not be relied upon
in making purchasing decisions. The development, release, and timing of any features or
functionality described for Oracle’s products remains at the sole discretion of Oracle.
2
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Today’s Agenda
Introduction
Core New JSON Features
To JSON or !JSON?
Misc Supporting Features
1
2
3
4
3
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
A Year of Anniversaries!
20 Years: PHP
20 Years: MySQL
15 Years: AFUP
10 Years: Oracle stewardship of InnoDB
5 Years: Oracle stewardship of MySQL
4
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
MySQL 5.7 is GA!
Enhanced InnoDB: faster online & bulk
load operations
Replication Improvements (incl. multi-
source, multi-threaded slaves...)
New Optimizer Cost Model: greater user
control & better query performance
Performance Schema Improvements
MySQL SYS Schema
Performance & Scalability Manageability
3 X Faster than MySQL 5.6
Improved Security: safer initialization,
setup & management
Native JSON Support
And many more new features and enhancements. Learn more at: dev.mysql.com
5
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
MySQL 5.7 Sysbench Benchmark: SQL Point Selects
3x Faster than MySQL 5.6
1,600,000 QPS
0
200,000
400,000
600,000
800,000
1,000,000
1,200,000
1,400,000
1,600,000
1,800,000
8 16 32 64 128 256 512 1,024
QueriesperSecond
Connections
MySQL 5.7: Sysbench OLTP Read Only (SQL Point Selects)
MySQL 5.7
MySQL 5.6
MySQL 5.5
Intel(R) Xeon(R) CPU E7-8890 v3
4 sockets x 18 cores-HT (144 CPU threads)
2.5 Ghz, 512GB RAM
Linux kernel 3.16
6
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
MySQL 5.7: Connections per Second
1.7x Faster than MySQL 5.6
3x Faster than MySQL 5.5
100,000 Connections/Sec
0
20,000
40,000
60,000
80,000
100,000
120,000
MySQL 5.5 MySQL 5.6 MySQL 5.7
Connections/Second
Intel(R) Xeon(R) CPU E7-8890 v3
4 sockets x 18 cores-HT (144 CPU threads)
2.5 Ghz, 512GB RAM
Linux kernel 3.16
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
New! MySQL Router
• Intelligently routes MySQL connections & transactions for increased
performance & uptime (load balancing, failover...etc), so you can focus on
application development
• Provides cross-language support for MySQL Fabric, delivering High
Availability and Scalability through automated data sharding
Easier, Faster and Safer to Scale MySQL Applications
8
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Want to learn more about MySQL 5.7?
Join the MySQL Tech Tour in Paris, December 8
9
All information &
registration on mysql.fr
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Today’s Agenda
Introduction
Core New JSON Features
To JSON or !JSON?
Misc Supporting Features
1
2
3
2
1
4
10
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Core New JSON features in MySQL 5.7
• Native JSON datatype
• JSON Functions
• Generated Columns
11
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
The JSON Type
CREATE TABLE employees (data JSON);
INSERT INTO employees VALUES ('{"id": 1, "name": "Jane"}');
INSERT INTO employees VALUES ('{"id": 2, "name": "Joe"}');
SELECT * FROM employees;
+---------------------------+
| data |
+---------------------------+
| {"id": 1, "name": "Jane"} |
| {"id": 2, "name": "Joe"} |
+---------------------------+
2 rows in set (0,00 sec)
12
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
JSON Type Tech Specs
• utf8mb4 character set
• Optimized for read intensive workload
• Parse and validation on insert only
• Dictionary
• Sorted objects' keys
• Fast access to array cells by index
13
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
JSON Type Tech Specs (cont.)
• Supports all native JSON types
• Numbers, strings, bool
• Objects, arrays
• Extended
• Date, time, datetime, timestamp
• Other
14
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Advantages over TEXT/VARCHAR
1. Provides Document Validation:
2. Efficient Binary Format
Allows quicker access to object members and array elements
INSERT INTO employees VALUES ('some random text');
ERROR 3130 (22032): Invalid JSON text: "Expect a value here." at
position 0 in value (or column) 'some random text'.
15
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
JSON Functions
SET @document = '[10, 20, [30, 40]]';
SELECT JSON_EXTRACT(@document, '$[1]');
+---------------------------------+
| JSON_EXTRACT(@document, '$[1]') |
+---------------------------------+
| 20 |
+---------------------------------+
1 row in set (0.01 sec)
16
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
• Accepts a JSON Path, which is similar to a selector:
• JSON_EXTRACT also supports a short hand:
column_name->"$.type"
JSON_EXTRACT
17
$("#type")
JSON_EXTRACT(column_name, "$.type")
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Using Real Life Data
• Via SF OpenData
• 206K JSON objects
representing subdivision
parcels.
• Imported from https://github.com/zemirco/sf-city-lots-json + small tweaks
CREATE TABLE features (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
feature JSON NOT NULL
);
18
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
{
"type":"Feature",
"geometry":{
"type":"Polygon",
"coordinates":[
[
[-122.42200352825247,37.80848009696725,0],
[-122.42207601332528,37.808835019815085,0],
[-122.42110217434865,37.808803534992904,0],
[-122.42106256906727,37.80860105681814,0],
[-122.42200352825247,37.80848009696725,0]
]
]
},
"properties":{
"TO_ST":"0",
"BLKLOT":"0001001",
"STREET":"UNKNOWN",
"FROM_ST":"0",
"LOT_NUM":"001",
"ST_TYPE":null,
"ODD_EVEN":"E",
"BLOCK_NUM":"0001",
"MAPBLKLOT":"0001001"
}
}
19
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
JSON Search
20
# Basic Find
SELECT * FROM features
WHERE feature->"$.properties.STREET" = 'MARKET'
LIMIT 1G
************************* 1. row *************************
id: 12250
feature: {"type": "Feature", "geometry": {"type": "Polygon", "coordinates": [[[-
122.39836263491878, 37.79189388899312, 0], [-122.39845248797837,
37.79233030084018, 0], [-122.39768507706792, 37.7924280850133, 0], [-
122.39836263491878, 37.79189388899312, 0]]]}, "properties": {"TO_ST": "388",
"BLKLOT": "0265003", "STREET": "MARKET", "FROM_ST": "388", "LOT_NUM": "003",
"ST_TYPE": "ST", "ODD_EVEN": "E", "BLOCK_NUM": "0265", "MAPBLKLOT": "0265003"}}
1 row in set (0.02 sec)
Using short hand
for JSON_EXTRACT
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
JSON Search
21
# Find where not exists
SELECT * FROM features
WHERE feature->"$.properties.STREET" IS NULL
LIMIT 1G
Empty set (0.39 sec)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Naive Performance Comparison
22
# as JSON type
SELECT DISTINCT
feature->"$.type" AS json_extract
FROM features;
+--------------+
| json_extract |
+--------------+
| "Feature" |
+--------------+
1 row in set (1.25 sec)
Unindexed traversal of 206K documents
# as TEXT type
SELECT DISTINCT
feature->"$.type" AS json_extract
FROM features;
+--------------+
| json_extract |
+--------------+
| "Feature" |
+--------------+
1 row in set (12.85 sec)
Explanation: Binary format of JSON type is very efficient at searching. Storing as TEXT
performs over 10x worse at traversal.
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Introducing Generated Columns
id my_integer my_integer_plus_one
1 10 11
2 20 21
3 30 31
4 40 41
CREATE TABLE t1 (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
my_integer INT,
my_integer_plus_one INT AS (my_integer+1)
);
UPDATE t1 SET my_integer_plus_one = 10 WHERE id = 1;
ERROR 3105 (HY000): The value specified for generated column
'my_integer_plus_one' in table 't1' is not allowed.
Column automatically maintained based
on your specification.
Read-only of course
23
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Generated Columns Support Indexes!
ALTER TABLE features ADD feature_type VARCHAR(30) AS (feature->"$.type");
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
ALTER TABLE features ADD INDEX (feature_type);
Query OK, 0 rows affected (0.73 sec)
Records: 0 Duplicates: 0 Warnings: 0
SELECT DISTINCT feature_type FROM features;
+--------------+
| feature_type |
+--------------+
| "Feature" |
+--------------+
1 row in set (0.06 sec)
From table scan on 206K documents to index scan on 206K materialized values
24
Meta data change only (FAST).
Does not need to touch table.
Creates index only. Does
not modify table rows.
Down from 1.25 sec to 0.06 sec
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Generated Columns (cont.)
• Used for “functional index”
• Available as either VIRTUAL (default) or STORED:
• Both types of computed columns permit for indexes to be added.
ALTER TABLE features ADD feature_type VARCHAR(30) AS
(feature->"$.type") STORED;
Query OK, 206560 rows affected (4.70 sec)
Records: 206560 Duplicates: 0 Warnings: 0
25
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Indexing Options Available
STORED VIRTUAL
Primary and Secondary
BTREE, Fulltext, GIS
Mixed with fields
Requires table rebuild
Not Online
Secondary Only
BTREE Only
Mixed with fields
No table rebuild
INSTANT Alter
Faster Insert
Bottom Line: Unless you need a PRIMARY KEY, FULLTEXT or GIS index VIRTUAL is probably better.
26
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Virtual vs. Stored Performance
• Approximate worst case scenario via a table scan:
27
SELECT DISTINCT feature_type FROM features;
+--------------+
| feature_type |
+--------------+
| "Feature" |
+--------------+
VIRTUAL-TEXT (10 sec)
VIRTUAL-JSON (1 sec)
STORED-TEXT (0.2 sec)
STORED-JSON (0.2 sec)
Clarification: Since indexes are materialized (stored) themselves, the real-life case for STORED
is when generating the column is computationally expensive and you can not use indexes
effectively.
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Unquote JSON String
SELECT
DISTINCT JSON_UNQUOTE(feature->"$.type")
AS feature_type
FROM features;
+-----------------+
| feature_type |
+-----------------+
| Feature |
+-----------------+
1 row in set (1.22 sec)
28
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
JSON Path Search
• Return the first path ('one') where the word MARKET appears:
29
SELECT JSON_SEARCH(feature,
'one', 'MARKET') AS
extract_path
FROM features
WHERE id = 121254;
+-----------------------+
| extract_path |
+-----------------------+
| "$.properties.STREET" |
+-----------------------+
1 row in set (0.00 sec)
SELECT
feature->"$.properties.STREET"
AS property_street
FROM features
WHERE id = 121254;
+-----------------+
| property_street |
+-----------------+
| "MARKET" |
+-----------------+
1 row in set (0.00 sec)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
JSON Array Creation
SELECT JSON_ARRAY(id,
feature->"$.properties.STREET",
feature->'$.type") AS json_array
FROM features ORDER BY RAND() LIMIT 3;
+-------------------------------+
| json_array |
+-------------------------------+
| [65298, "10TH", "Feature"] |
| [122985, "08TH", "Feature"] |
| [172884, "CURTIS", "Feature"] |
+-------------------------------+
3 rows in set (2.66 sec)
30
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
JSON Object Creation
SELECT JSON_OBJECT('id', id,
'street', feature->"$.properties.STREET",
'type', feature->"$.type"
) AS json_object
FROM features ORDER BY RAND() LIMIT 3;
+--------------------------------------------------------+
| json_object |
+--------------------------------------------------------+
| {"id": 122976, "type": "Feature", "street": "RAUSCH"} |
| {"id": 148698, "type": "Feature", "street": "WALLACE"} |
| {"id": 45214, "type": "Feature", "street": "HAIGHT"} |
+--------------------------------------------------------+
3 rows in set (3.11 sec)
31
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
JSON_REPLACE
SELECT JSON_REPLACE(feature, '$.type', JSON_ARRAY('feature', 'bug'))
AS json_object FROM features LIMIT 1;
+--------------------------------------------------------+
| json_object |
+--------------------------------------------------------+
| {"type": ["feature", "bug"], "geometry": {"type": ..}} |
+--------------------------------------------------------+
32
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
• 5.7 supports functions to CREATE, SEARCH, MODIFY and RETURN JSON
values:
JSON Functions
JSON_ARRAY_APPEND()
JSON_ARRAY_INSERT()
JSON_ARRAY()
JSON_CONTAINS_PATH()
JSON_CONTAINS()
JSON_DEPTH()
JSON_EXTRACT()
JSON_INSERT()
JSON_KEYS()
JSON_LENGTH()
JSON_MERGE()
JSON_OBJECT()
JSON_QUOTE()
JSON_REMOVE()
JSON_REPLACE()
JSON_SEARCH()
JSON_SET()
JSON_TYPE()
JSON_UNQUOTE()
JSON_VALID()
https://dev.mysql.com/doc/refman/5.7/en/json-functions.html
33
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
JSON Comparator
34
SELECT CAST(1 AS JSON) = 1;
+---------------------+
| CAST(1 AS JSON) = 1 |
+---------------------+
| 1 |
+---------------------+
1 row in set (0.01 sec)
SELECT CAST('{"num": 1.1}' AS JSON) = CAST('{"num": 1.1}' AS JSON);
+-------------------------------------------------------------+
| CAST('{"num": 1.1}' AS JSON) = CAST('{"num": 1.1}' AS JSON) |
+-------------------------------------------------------------+
| 1 |
+-------------------------------------------------------------+
1 row in set (0.00 sec)
JSON value of 1 equals 1
JSON Objects Compare
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Today’s Agenda
Introduction
Core New JSON Features
To JSON or !JSON?
Misc Supporting Features
1
22
1
2
3
4
35
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
JSON or Column?
• Up to you!
• Advantages to both approaches
36
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Storing as a Column
• Easier to apply a schema to your application
• Schema may make applications easier to maintain over time, as change is
controlled;
• Do not have to expect as many permutations
• Allows some constraints over data
37
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Storing as JSON
• More flexible way to represent data that is hard to model in schema;
• Imagine you are a SaaS application serving many customers
• Strong use-case to support custom-fields
• Historically this may have used Entity–attribute–value model (EAV). Does
not always perform well
38
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
JSON (cont.)
• Easier denormalization; an optimization that is important in some specific
situations
• No painful schema changes*
• Easier prototyping
• Fewer types to consider
• No enforced schema, start storing values immediately
* MySQL 5.6 has Online DDL. This is not as large of an issue as it
was historically.
39
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Schema + Schemaless
SSDs have capacity_in_gb, CPUs have a core_count. These attributes are not consistent
across products.
CREATE TABLE pc_components (
id INT NOT NULL PRIMARY KEY,
description VARCHAR(60) NOT NULL,
vendor VARCHAR(30) NOT NULL,
serial_number VARCHAR(30) NOT NULL,
attributes JSON NOT NULL
);
40
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Today’s Agenda
Introduction
Core New JSON Features
To JSON or !JSON?
Misc Supporting Features
1
22
1
2
3
4
3
41
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
SSDs
• Compression is a great fit requirement for modern storage
• Allows negation of new constraint; lower capacity
Hard Drive SSD
Capacity High Low
IOPS Available Low High
Sequential IO Performance Good Very Good
Random IO Performance Bad Very Good
Lifetime Good Maximum Read/Write Cycles
42
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
JSON Documents
• May compress well
• Schemaless means that the keys are repeated in each of the documents
• Repetition improves compression performance
• MySQL 5.7 also supports 32KB and 64KB pages (improved compression)
43
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
MySQL 5.7 Page Compression
• InnoDB has had compression since 5.1-plugin
• MySQL 5.7 introduces a new, simpler version of page compression
• It relies on punch-hole support
44
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Compression Performance (16K Page)
SELECT name,((file_size-allocated_size)*100)/file_size AS
compressed_pct FROM information_schema.INNODB_SYS_TABLESPACES
WHERE name LIKE 'test/features';
+---------------+----------------+
| name | compressed_pct |
+---------------+----------------+
| test/features | 59.2634 |
+---------------+----------------+
1 row in set (0.01 sec)
45
Using real-life data set
from earlier
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Additional Features
• Views and triggers can be used migrate between JSON and top level
columns
• MySQL 5.7 supports multiple triggers per table event!
• 5.7 also supports server-side query rewrite
46
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
MySQL 5.7: Query Rewrite Plugin
• New pre and post parse query rewrite APIs
– Users can write their own plug-ins
• Provides a post-parse query plugin
– Rewrite problematic queries without the need to make application changes
– Add hints
– Modify join order
– Many more …
• Improve problematic queries from ORMs, third party apps, etc
• Eliminates many legacy use cases for proxies
47
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
• Replaced custom code with Boost.Geometry
– For spatial calculations
– For spatial analysis
– Enabling full OGC compliance
– We’re also Boost.Geometry contributors!
• InnoDB R-tree based spatial indexes
– Full ACID, MVCC, & transactional support
– Index records contain minimum bounding box
• GeoHash
• GeoJSON
• Helper functions such as ST_Distance_Sphere() and ST_MakeEnvelope()
MySQL 5.7: GIS Improvements
48
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
MySQL Repos
• Distributions
– Oracle, Red Hat, CentOS
– Fedora
– Ubuntu, Debian
– SUSE
• Official MySQL Docker Image from Oracle
• Coming Soon
– Preconfigured Containers
– Improved support for popular DevOps
deployment tools
https://dev.mysql.com/downloads/repo
MySQL on GitHub
• Git for MySQL Engineering
– Fast, flexible and great for a distributed team
– Great tooling
– Large and vibrant community
• GitHub for MySQL Community
– Easy and fast code availability to the community
and to downstream projects
– Pull Requests
https://github.com/mysql
49
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Resources
• Blog by Olivier Dasini: http://dasini.net/blog/2015/11/17/30-mins-avec-json-
en-mysql/
• http://mysqlserverteam.com/
• http://mysqlserverteam.com/tag/json/
• https://dev.mysql.com/doc/refman/5.7/en/mysql-nutshell.html
• http://dev.mysql.com/doc/relnotes/mysql/5.7/en/
• https://dev.mysql.com/doc/refman/5.7/en/json.html
• https://dev.mysql.com/doc/refman/5.7/en/json-functions.html
50
Php forum2015 tomas_final

Php forum2015 tomas_final

  • 1.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | MySQL 5.7 & JSON: New Opportunities for Developers Tomas Ulin, VP MySQL Engineering, Oracle Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
  • 2.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Safe Harbor Statement The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle. 2
  • 3.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Today’s Agenda Introduction Core New JSON Features To JSON or !JSON? Misc Supporting Features 1 2 3 4 3
  • 4.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | A Year of Anniversaries! 20 Years: PHP 20 Years: MySQL 15 Years: AFUP 10 Years: Oracle stewardship of InnoDB 5 Years: Oracle stewardship of MySQL 4
  • 5.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | MySQL 5.7 is GA! Enhanced InnoDB: faster online & bulk load operations Replication Improvements (incl. multi- source, multi-threaded slaves...) New Optimizer Cost Model: greater user control & better query performance Performance Schema Improvements MySQL SYS Schema Performance & Scalability Manageability 3 X Faster than MySQL 5.6 Improved Security: safer initialization, setup & management Native JSON Support And many more new features and enhancements. Learn more at: dev.mysql.com 5
  • 6.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | MySQL 5.7 Sysbench Benchmark: SQL Point Selects 3x Faster than MySQL 5.6 1,600,000 QPS 0 200,000 400,000 600,000 800,000 1,000,000 1,200,000 1,400,000 1,600,000 1,800,000 8 16 32 64 128 256 512 1,024 QueriesperSecond Connections MySQL 5.7: Sysbench OLTP Read Only (SQL Point Selects) MySQL 5.7 MySQL 5.6 MySQL 5.5 Intel(R) Xeon(R) CPU E7-8890 v3 4 sockets x 18 cores-HT (144 CPU threads) 2.5 Ghz, 512GB RAM Linux kernel 3.16 6
  • 7.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | MySQL 5.7: Connections per Second 1.7x Faster than MySQL 5.6 3x Faster than MySQL 5.5 100,000 Connections/Sec 0 20,000 40,000 60,000 80,000 100,000 120,000 MySQL 5.5 MySQL 5.6 MySQL 5.7 Connections/Second Intel(R) Xeon(R) CPU E7-8890 v3 4 sockets x 18 cores-HT (144 CPU threads) 2.5 Ghz, 512GB RAM Linux kernel 3.16
  • 8.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | New! MySQL Router • Intelligently routes MySQL connections & transactions for increased performance & uptime (load balancing, failover...etc), so you can focus on application development • Provides cross-language support for MySQL Fabric, delivering High Availability and Scalability through automated data sharding Easier, Faster and Safer to Scale MySQL Applications 8
  • 9.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Want to learn more about MySQL 5.7? Join the MySQL Tech Tour in Paris, December 8 9 All information & registration on mysql.fr
  • 10.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Today’s Agenda Introduction Core New JSON Features To JSON or !JSON? Misc Supporting Features 1 2 3 2 1 4 10
  • 11.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Core New JSON features in MySQL 5.7 • Native JSON datatype • JSON Functions • Generated Columns 11
  • 12.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | The JSON Type CREATE TABLE employees (data JSON); INSERT INTO employees VALUES ('{"id": 1, "name": "Jane"}'); INSERT INTO employees VALUES ('{"id": 2, "name": "Joe"}'); SELECT * FROM employees; +---------------------------+ | data | +---------------------------+ | {"id": 1, "name": "Jane"} | | {"id": 2, "name": "Joe"} | +---------------------------+ 2 rows in set (0,00 sec) 12
  • 13.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | JSON Type Tech Specs • utf8mb4 character set • Optimized for read intensive workload • Parse and validation on insert only • Dictionary • Sorted objects' keys • Fast access to array cells by index 13
  • 14.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | JSON Type Tech Specs (cont.) • Supports all native JSON types • Numbers, strings, bool • Objects, arrays • Extended • Date, time, datetime, timestamp • Other 14
  • 15.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Advantages over TEXT/VARCHAR 1. Provides Document Validation: 2. Efficient Binary Format Allows quicker access to object members and array elements INSERT INTO employees VALUES ('some random text'); ERROR 3130 (22032): Invalid JSON text: "Expect a value here." at position 0 in value (or column) 'some random text'. 15
  • 16.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | JSON Functions SET @document = '[10, 20, [30, 40]]'; SELECT JSON_EXTRACT(@document, '$[1]'); +---------------------------------+ | JSON_EXTRACT(@document, '$[1]') | +---------------------------------+ | 20 | +---------------------------------+ 1 row in set (0.01 sec) 16
  • 17.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | • Accepts a JSON Path, which is similar to a selector: • JSON_EXTRACT also supports a short hand: column_name->"$.type" JSON_EXTRACT 17 $("#type") JSON_EXTRACT(column_name, "$.type")
  • 18.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Using Real Life Data • Via SF OpenData • 206K JSON objects representing subdivision parcels. • Imported from https://github.com/zemirco/sf-city-lots-json + small tweaks CREATE TABLE features ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, feature JSON NOT NULL ); 18
  • 19.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | { "type":"Feature", "geometry":{ "type":"Polygon", "coordinates":[ [ [-122.42200352825247,37.80848009696725,0], [-122.42207601332528,37.808835019815085,0], [-122.42110217434865,37.808803534992904,0], [-122.42106256906727,37.80860105681814,0], [-122.42200352825247,37.80848009696725,0] ] ] }, "properties":{ "TO_ST":"0", "BLKLOT":"0001001", "STREET":"UNKNOWN", "FROM_ST":"0", "LOT_NUM":"001", "ST_TYPE":null, "ODD_EVEN":"E", "BLOCK_NUM":"0001", "MAPBLKLOT":"0001001" } } 19
  • 20.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | JSON Search 20 # Basic Find SELECT * FROM features WHERE feature->"$.properties.STREET" = 'MARKET' LIMIT 1G ************************* 1. row ************************* id: 12250 feature: {"type": "Feature", "geometry": {"type": "Polygon", "coordinates": [[[- 122.39836263491878, 37.79189388899312, 0], [-122.39845248797837, 37.79233030084018, 0], [-122.39768507706792, 37.7924280850133, 0], [- 122.39836263491878, 37.79189388899312, 0]]]}, "properties": {"TO_ST": "388", "BLKLOT": "0265003", "STREET": "MARKET", "FROM_ST": "388", "LOT_NUM": "003", "ST_TYPE": "ST", "ODD_EVEN": "E", "BLOCK_NUM": "0265", "MAPBLKLOT": "0265003"}} 1 row in set (0.02 sec) Using short hand for JSON_EXTRACT
  • 21.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | JSON Search 21 # Find where not exists SELECT * FROM features WHERE feature->"$.properties.STREET" IS NULL LIMIT 1G Empty set (0.39 sec)
  • 22.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Naive Performance Comparison 22 # as JSON type SELECT DISTINCT feature->"$.type" AS json_extract FROM features; +--------------+ | json_extract | +--------------+ | "Feature" | +--------------+ 1 row in set (1.25 sec) Unindexed traversal of 206K documents # as TEXT type SELECT DISTINCT feature->"$.type" AS json_extract FROM features; +--------------+ | json_extract | +--------------+ | "Feature" | +--------------+ 1 row in set (12.85 sec) Explanation: Binary format of JSON type is very efficient at searching. Storing as TEXT performs over 10x worse at traversal.
  • 23.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Introducing Generated Columns id my_integer my_integer_plus_one 1 10 11 2 20 21 3 30 31 4 40 41 CREATE TABLE t1 ( id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, my_integer INT, my_integer_plus_one INT AS (my_integer+1) ); UPDATE t1 SET my_integer_plus_one = 10 WHERE id = 1; ERROR 3105 (HY000): The value specified for generated column 'my_integer_plus_one' in table 't1' is not allowed. Column automatically maintained based on your specification. Read-only of course 23
  • 24.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Generated Columns Support Indexes! ALTER TABLE features ADD feature_type VARCHAR(30) AS (feature->"$.type"); Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 ALTER TABLE features ADD INDEX (feature_type); Query OK, 0 rows affected (0.73 sec) Records: 0 Duplicates: 0 Warnings: 0 SELECT DISTINCT feature_type FROM features; +--------------+ | feature_type | +--------------+ | "Feature" | +--------------+ 1 row in set (0.06 sec) From table scan on 206K documents to index scan on 206K materialized values 24 Meta data change only (FAST). Does not need to touch table. Creates index only. Does not modify table rows. Down from 1.25 sec to 0.06 sec
  • 25.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Generated Columns (cont.) • Used for “functional index” • Available as either VIRTUAL (default) or STORED: • Both types of computed columns permit for indexes to be added. ALTER TABLE features ADD feature_type VARCHAR(30) AS (feature->"$.type") STORED; Query OK, 206560 rows affected (4.70 sec) Records: 206560 Duplicates: 0 Warnings: 0 25
  • 26.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Indexing Options Available STORED VIRTUAL Primary and Secondary BTREE, Fulltext, GIS Mixed with fields Requires table rebuild Not Online Secondary Only BTREE Only Mixed with fields No table rebuild INSTANT Alter Faster Insert Bottom Line: Unless you need a PRIMARY KEY, FULLTEXT or GIS index VIRTUAL is probably better. 26
  • 27.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Virtual vs. Stored Performance • Approximate worst case scenario via a table scan: 27 SELECT DISTINCT feature_type FROM features; +--------------+ | feature_type | +--------------+ | "Feature" | +--------------+ VIRTUAL-TEXT (10 sec) VIRTUAL-JSON (1 sec) STORED-TEXT (0.2 sec) STORED-JSON (0.2 sec) Clarification: Since indexes are materialized (stored) themselves, the real-life case for STORED is when generating the column is computationally expensive and you can not use indexes effectively.
  • 28.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Unquote JSON String SELECT DISTINCT JSON_UNQUOTE(feature->"$.type") AS feature_type FROM features; +-----------------+ | feature_type | +-----------------+ | Feature | +-----------------+ 1 row in set (1.22 sec) 28
  • 29.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | JSON Path Search • Return the first path ('one') where the word MARKET appears: 29 SELECT JSON_SEARCH(feature, 'one', 'MARKET') AS extract_path FROM features WHERE id = 121254; +-----------------------+ | extract_path | +-----------------------+ | "$.properties.STREET" | +-----------------------+ 1 row in set (0.00 sec) SELECT feature->"$.properties.STREET" AS property_street FROM features WHERE id = 121254; +-----------------+ | property_street | +-----------------+ | "MARKET" | +-----------------+ 1 row in set (0.00 sec)
  • 30.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | JSON Array Creation SELECT JSON_ARRAY(id, feature->"$.properties.STREET", feature->'$.type") AS json_array FROM features ORDER BY RAND() LIMIT 3; +-------------------------------+ | json_array | +-------------------------------+ | [65298, "10TH", "Feature"] | | [122985, "08TH", "Feature"] | | [172884, "CURTIS", "Feature"] | +-------------------------------+ 3 rows in set (2.66 sec) 30
  • 31.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | JSON Object Creation SELECT JSON_OBJECT('id', id, 'street', feature->"$.properties.STREET", 'type', feature->"$.type" ) AS json_object FROM features ORDER BY RAND() LIMIT 3; +--------------------------------------------------------+ | json_object | +--------------------------------------------------------+ | {"id": 122976, "type": "Feature", "street": "RAUSCH"} | | {"id": 148698, "type": "Feature", "street": "WALLACE"} | | {"id": 45214, "type": "Feature", "street": "HAIGHT"} | +--------------------------------------------------------+ 3 rows in set (3.11 sec) 31
  • 32.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | JSON_REPLACE SELECT JSON_REPLACE(feature, '$.type', JSON_ARRAY('feature', 'bug')) AS json_object FROM features LIMIT 1; +--------------------------------------------------------+ | json_object | +--------------------------------------------------------+ | {"type": ["feature", "bug"], "geometry": {"type": ..}} | +--------------------------------------------------------+ 32
  • 33.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | • 5.7 supports functions to CREATE, SEARCH, MODIFY and RETURN JSON values: JSON Functions JSON_ARRAY_APPEND() JSON_ARRAY_INSERT() JSON_ARRAY() JSON_CONTAINS_PATH() JSON_CONTAINS() JSON_DEPTH() JSON_EXTRACT() JSON_INSERT() JSON_KEYS() JSON_LENGTH() JSON_MERGE() JSON_OBJECT() JSON_QUOTE() JSON_REMOVE() JSON_REPLACE() JSON_SEARCH() JSON_SET() JSON_TYPE() JSON_UNQUOTE() JSON_VALID() https://dev.mysql.com/doc/refman/5.7/en/json-functions.html 33
  • 34.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | JSON Comparator 34 SELECT CAST(1 AS JSON) = 1; +---------------------+ | CAST(1 AS JSON) = 1 | +---------------------+ | 1 | +---------------------+ 1 row in set (0.01 sec) SELECT CAST('{"num": 1.1}' AS JSON) = CAST('{"num": 1.1}' AS JSON); +-------------------------------------------------------------+ | CAST('{"num": 1.1}' AS JSON) = CAST('{"num": 1.1}' AS JSON) | +-------------------------------------------------------------+ | 1 | +-------------------------------------------------------------+ 1 row in set (0.00 sec) JSON value of 1 equals 1 JSON Objects Compare
  • 35.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Today’s Agenda Introduction Core New JSON Features To JSON or !JSON? Misc Supporting Features 1 22 1 2 3 4 35
  • 36.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | JSON or Column? • Up to you! • Advantages to both approaches 36
  • 37.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Storing as a Column • Easier to apply a schema to your application • Schema may make applications easier to maintain over time, as change is controlled; • Do not have to expect as many permutations • Allows some constraints over data 37
  • 38.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Storing as JSON • More flexible way to represent data that is hard to model in schema; • Imagine you are a SaaS application serving many customers • Strong use-case to support custom-fields • Historically this may have used Entity–attribute–value model (EAV). Does not always perform well 38
  • 39.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | JSON (cont.) • Easier denormalization; an optimization that is important in some specific situations • No painful schema changes* • Easier prototyping • Fewer types to consider • No enforced schema, start storing values immediately * MySQL 5.6 has Online DDL. This is not as large of an issue as it was historically. 39
  • 40.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Schema + Schemaless SSDs have capacity_in_gb, CPUs have a core_count. These attributes are not consistent across products. CREATE TABLE pc_components ( id INT NOT NULL PRIMARY KEY, description VARCHAR(60) NOT NULL, vendor VARCHAR(30) NOT NULL, serial_number VARCHAR(30) NOT NULL, attributes JSON NOT NULL ); 40
  • 41.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Today’s Agenda Introduction Core New JSON Features To JSON or !JSON? Misc Supporting Features 1 22 1 2 3 4 3 41
  • 42.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | SSDs • Compression is a great fit requirement for modern storage • Allows negation of new constraint; lower capacity Hard Drive SSD Capacity High Low IOPS Available Low High Sequential IO Performance Good Very Good Random IO Performance Bad Very Good Lifetime Good Maximum Read/Write Cycles 42
  • 43.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | JSON Documents • May compress well • Schemaless means that the keys are repeated in each of the documents • Repetition improves compression performance • MySQL 5.7 also supports 32KB and 64KB pages (improved compression) 43
  • 44.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | MySQL 5.7 Page Compression • InnoDB has had compression since 5.1-plugin • MySQL 5.7 introduces a new, simpler version of page compression • It relies on punch-hole support 44
  • 45.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Compression Performance (16K Page) SELECT name,((file_size-allocated_size)*100)/file_size AS compressed_pct FROM information_schema.INNODB_SYS_TABLESPACES WHERE name LIKE 'test/features'; +---------------+----------------+ | name | compressed_pct | +---------------+----------------+ | test/features | 59.2634 | +---------------+----------------+ 1 row in set (0.01 sec) 45 Using real-life data set from earlier
  • 46.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Additional Features • Views and triggers can be used migrate between JSON and top level columns • MySQL 5.7 supports multiple triggers per table event! • 5.7 also supports server-side query rewrite 46
  • 47.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | MySQL 5.7: Query Rewrite Plugin • New pre and post parse query rewrite APIs – Users can write their own plug-ins • Provides a post-parse query plugin – Rewrite problematic queries without the need to make application changes – Add hints – Modify join order – Many more … • Improve problematic queries from ORMs, third party apps, etc • Eliminates many legacy use cases for proxies 47
  • 48.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | • Replaced custom code with Boost.Geometry – For spatial calculations – For spatial analysis – Enabling full OGC compliance – We’re also Boost.Geometry contributors! • InnoDB R-tree based spatial indexes – Full ACID, MVCC, & transactional support – Index records contain minimum bounding box • GeoHash • GeoJSON • Helper functions such as ST_Distance_Sphere() and ST_MakeEnvelope() MySQL 5.7: GIS Improvements 48
  • 49.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | MySQL Repos • Distributions – Oracle, Red Hat, CentOS – Fedora – Ubuntu, Debian – SUSE • Official MySQL Docker Image from Oracle • Coming Soon – Preconfigured Containers – Improved support for popular DevOps deployment tools https://dev.mysql.com/downloads/repo MySQL on GitHub • Git for MySQL Engineering – Fast, flexible and great for a distributed team – Great tooling – Large and vibrant community • GitHub for MySQL Community – Easy and fast code availability to the community and to downstream projects – Pull Requests https://github.com/mysql 49
  • 50.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Resources • Blog by Olivier Dasini: http://dasini.net/blog/2015/11/17/30-mins-avec-json- en-mysql/ • http://mysqlserverteam.com/ • http://mysqlserverteam.com/tag/json/ • https://dev.mysql.com/doc/refman/5.7/en/mysql-nutshell.html • http://dev.mysql.com/doc/relnotes/mysql/5.7/en/ • https://dev.mysql.com/doc/refman/5.7/en/json.html • https://dev.mysql.com/doc/refman/5.7/en/json-functions.html 50

Editor's Notes

  • #3 This is a Safe Harbor Front slide, one of two Safe Harbor Statement slides included in this template. One of the Safe Harbor slides must be used if your presentation covers material affected by Oracle’s Revenue Recognition Policy To learn more about this policy, e-mail: Revrec-americasiebc_us@oracle.com For internal communication, Safe Harbor Statements are not required. However, there is an applicable disclaimer (Exhibit E) that should be used, found in the Oracle Revenue Recognition Policy for Future Product Communications. Copy and paste this link into a web browser, to find out more information.   http://my.oracle.com/site/fin/gfo/GlobalProcesses/cnt452504.pdf For all external communications such as press release, roadmaps, PowerPoint presentations, Safe Harbor Statements are required. You can refer to the link mentioned above to find out additional information/disclaimers required depending on your audience.
  • #5 2015 is a year with many anniversaries! MySQL has now indeed turned 20! Perhaps show of hands for years of MySQL usage.... Already 10 years of Oracle stewardship for InnoDB, MySQL’s main storage engine, which is significant. InnoDB has tremendously improved during that time. And already over 5 years, almost 6 actually, of Oracle stewardship for MySQL. We’ve made phenomenal progress during that time. I’ll come back to that in a minute. Thank you all for your contributions to MySQL during the past 20 years. Thank you to the MySQL community for investing your time and energy helping us to improve MySQL, and thank you to our customers, increasingly relying on our commercial offerings for their business critical applications.
  • #6 Very good example: MySQL 5.7 GA! Go through at high level, Tomas will dive into the details.
  • #7 Huge performance gains
  • #12 These are the three main features that I will be covering today.
  • #13 The type name is "JSON". It accepts valid JSON.
  • #14 JSON is utf8. Type is essentially read-optimized, small hit on insert for parse. Well amortized by any IO cost.
  • #16 Not necessarily any smaller, these are the two main advantages. In future there may be more (patching to avoid a full rewrite in storage.)
  • #18 Path also supports Wildcarding (*)
  • #20 The raw JSON objects in this example all look like this.
  • #25 Still has to examine full index, but now it is just an index of the values all extracted. It is much smaller.
  • #26 http://mysqlserverteam.com/virtual-columns-and-effective-functional-indexes-in-innodb/
  • #28 206K records
  • #30 Match can be one or all, similarish to regular expressions.
  • #31 Function accepts N arguments and returns an array.
  • #32 Function accepts even number of arguments. Odd values are key names, even are values.
  • #33 Function accepts a search path and a replacement value.
  • #34 Previously JSN_ now JSON_ Bold: Examples I demonstrated. 20 in total.
  • #38 Constraints: unique keys, numeric values for integers, enforced length.
  • #39 EAV repeats key values (bloat) and leads to many short records (bloat). Also poor query optimization in some cases. JSON is basically better for all cases of EAV.
  • #40 Could enforce schema in application, but is a layering problem. No enforced schema = No schema changes. Easier prototyping More flexible to change. MySQL 5.6+ features Online DDL, but still has some expense internally to modify tables. Makes denormalization easier May be a good optimization Highly situational dependent.
  • #41 Example of real life data set which has constant attributes and variable.
  • #46 65.42 pct as DYNAMIC row format w/64K pages.
  • #48  As you may know, MySQL supports a plugin API that enables creation of server components. Plugins can be loaded at server startup, or loaded and unloaded at runtime without restarting the server. In 5.7, we provide you two rewrite APIs so that you can write your own plugin. Pre parse API: interface is query text, you rewrite queries by replacing text with other text Post parse API: you have to modify the parse tree. May not be as difficult as it sounds. We offer a parser service through the plugin API. It contains methods to invoke parsing, get a normalized query text from parser tree, walk the parse tree. In addition to providing interfaces for writing your own plugin, we also provide a production quality query rewrite plugin which rewrites queries without the need to make application changes. You can use this plugin to add hints, mofify join order and more. Our query rewrite plugin uses the post parse plug-in interface, because it gives next to zero performanc overhead. I will explain how this works. The main point of choosing post parsing is that digest is computed during parsing, and we use the digest to match incoming query with rules.
  • #49 Started out as the Generic Geometry Library by OSGeo. Now it’s of course part of Boost.