51
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.

Php forum2015 tomas_final

Embed Size (px)

Citation preview

Page 1: Php forum2015 tomas_final

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

MySQL 5.7 & JSON: New Opportunities for DevelopersTomas Ulin, VP MySQL Engineering, Oracle

Copyright © 2015, Oracle and/or its affiliates. All rights reserved.

Page 2: Php forum2015 tomas_final

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Safe Harbor StatementThe 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

Page 3: Php forum2015 tomas_final

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

Page 4: Php forum2015 tomas_final

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

Page 5: Php forum2015 tomas_final

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

Page 6: Php forum2015 tomas_final

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

8 16 32 64 128 256 512 1,0240

200,000400,000600,000800,000

1,000,0001,200,0001,400,0001,600,0001,800,000

MySQL 5.7: Sysbench OLTP Read Only (SQL Point Selects)

MySQL 5.7

MySQL 5.6

MySQL 5.5

Connections

Que

ries p

er S

econ

d

Intel(R) Xeon(R) CPU E7-8890 v34 sockets x 18 cores-HT (144 CPU threads)2.5 Ghz, 512GB RAMLinux kernel 3.16

6

Page 7: Php forum2015 tomas_final

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

MySQL 5.7: Connections per Second

1.7x Faster than MySQL 5.63x Faster than MySQL 5.5

100,000 Connections/Sec

MySQL 5.5 MySQL 5.6 MySQL 5.70

20,000

40,000

60,000

80,000

100,000

120,000

Conn

ectio

ns /

Seco

nd

Intel(R) Xeon(R) CPU E7-8890 v34 sockets x 18 cores-HT (144 CPU threads)2.5 Ghz, 512GB RAMLinux kernel 3.16

Page 8: Php forum2015 tomas_final

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

Page 9: Php forum2015 tomas_final

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

Page 10: Php forum2015 tomas_final

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

Page 11: Php forum2015 tomas_final

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

Page 12: Php forum2015 tomas_final

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

Page 13: Php forum2015 tomas_final

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

Page 14: Php forum2015 tomas_final

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

Page 15: Php forum2015 tomas_final

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Advantages over TEXT/VARCHAR1. Provides Document Validation:

2. Efficient Binary FormatAllows 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

Page 16: Php forum2015 tomas_final

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

Page 17: Php forum2015 tomas_final

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")

Page 18: Php forum2015 tomas_final

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Using Real Life Data• Via SF OpenData• 206K JSON objects

representing subdivisionparcels.

• 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

Page 19: Php forum2015 tomas_final

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

Page 20: Php forum2015 tomas_final

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

JSON Search

20

# Basic FindSELECT * FROM features WHERE feature->"$.properties.STREET" = 'MARKET'LIMIT 1\G************************* 1. row ************************* id: 12250feature: {"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

Page 21: Php forum2015 tomas_final

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

JSON Search

21

# Find where not existsSELECT * FROM features WHERE feature->"$.properties.STREET" IS NULLLIMIT 1\GEmpty set (0.39 sec)

Page 22: Php forum2015 tomas_final

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Naive Performance Comparison

22

# as JSON typeSELECT DISTINCT feature->"$.type" AS json_extractFROM features;+--------------+| json_extract |+--------------+| "Feature" |+--------------+1 row in set (1.25 sec)

Unindexed traversal of 206K documents

# as TEXT typeSELECT DISTINCT feature->"$.type" AS json_extractFROM 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.

Page 23: Php forum2015 tomas_final

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Introducing Generated Columnsid 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

Page 24: Php forum2015 tomas_final

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

Page 25: Php forum2015 tomas_final

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

Page 26: Php forum2015 tomas_final

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 InsertBottom Line: Unless you need a PRIMARY KEY, FULLTEXT or GIS index VIRTUAL is probably better.

26

Page 27: Php forum2015 tomas_final

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.

Page 28: Php forum2015 tomas_final

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Unquote JSON String

SELECT DISTINCT JSON_UNQUOTE(feature->"$.type") AS feature_typeFROM features;+-----------------+| feature_type |+-----------------+| Feature |+-----------------+1 row in set (1.22 sec)

28

Page 29: Php forum2015 tomas_final

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_pathFROM featuresWHERE id = 121254;+-----------------------+| extract_path |+-----------------------+| "$.properties.STREET" |+-----------------------+1 row in set (0.00 sec)

SELECTfeature->"$.properties.STREET" AS property_streetFROM featuresWHERE id = 121254;+-----------------+| property_street |+-----------------+| "MARKET" |+-----------------+1 row in set (0.00 sec)

Page 30: Php forum2015 tomas_final

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

JSON Array Creation

SELECT JSON_ARRAY(id, feature->"$.properties.STREET", feature->'$.type") AS json_arrayFROM 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

Page 31: Php forum2015 tomas_final

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_objectFROM 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

Page 32: Php forum2015 tomas_final

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

Page 33: Php forum2015 tomas_final

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

Page 34: Php forum2015 tomas_final

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

Page 35: Php forum2015 tomas_final

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

Page 36: Php forum2015 tomas_final

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

JSON or Column?• Up to you!• Advantages to both approaches

36

Page 37: Php forum2015 tomas_final

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

Page 38: Php forum2015 tomas_final

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

Page 39: Php forum2015 tomas_final

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

Page 40: Php forum2015 tomas_final

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

Page 41: Php forum2015 tomas_final

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

Page 42: Php forum2015 tomas_final

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

Page 43: Php forum2015 tomas_final

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

Page 44: Php forum2015 tomas_final

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

Page 45: Php forum2015 tomas_final

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

Page 46: Php forum2015 tomas_final

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

Page 47: Php forum2015 tomas_final

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

Page 48: Php forum2015 tomas_final

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

Page 49: Php forum2015 tomas_final

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

Page 50: Php forum2015 tomas_final

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

Page 51: Php forum2015 tomas_final