Post on 10-Feb-2017
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
MySQL Rises with JSON Support
MySQL 5.7
Okcan Yasin Saygılı
yasin.x.saygili@oracle.com
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 2
Agenda
1.The new JSON data type
2.Inlined JSON path expressions
3.Indexing JSON data
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 3
JSON Datatype
MySQL supports solid JSON data type and enables effect access to data in JSON documents. The JSONdata type support these advantages over storing JSON-format strings ,
•Automatic validation of JSON documents stored in JSON columns.
•Optimized storage format. JSON documents stored in JSON columns are converted to an internal format that permits quick read access to document elements.
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 4
JSON Datatype
•The size of JSON documents stored in JSON columns is limited to the value of the max allowed packet system variable
•JSON columns cannot have a default value.
•JSON columns cannot be indexed
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 5
• mysql> SELECT JSON_ARRAY(1, "abc", NULL, TRUE, CURTIME());• +---------------------------------------------+ • | JSON_ARRAY(1, "abc", NULL, TRUE, CURTIME()) |• +---------------------------------------------+ • | [1, "abc", null, true, "11:30:24.000000"] | • +---------------------------------------------+
The New JSON DatatypeEvaluates a (possibly empty) list of values and returns a JSON array containing those values.
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 6
insert a value into a JSON
mysql> CREATE TABLE t1 (jdoc JSON); Query OK, 0 rows affected (0.20 sec) mysql> INSERT INTO t1 VALUES('{"key1": "value1", "key2": "value2"}');
Query OK, 1 row affected (0.01 sec)
Attempting to insert a value into a JSON column succeeds if the value is a valid JSON value, but fails if it is not:
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 7
Insert Value
mysql> SELECT JSON_INSERT(@j, '$[1].b[0]', 1, '$[2][2]', 2); +-----------------------------------------------+ | JSON_INSERT(@j, '$[1].b[0]', 1, '$[2][2]', 2) | +-----------------------------------------------+ | ["a", {"b": [true, false]}, [10, 20, 2]] | +-----------------------------------------------+
JSON_INSERT() adds new values but does not replace existing values:
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 8
User DefinedJSON values can be assigned to user-defined variables:
mysql> SET @j = JSON_OBJECT('key', 'value'); mysql> SELECT @j; +------------------+ | @j | +------------------+ | {"key": "value"} | +------------------+
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 9
JSON ReplaceJSON_REPLACE replaces existing values and ignores new values:
mysql> SELECT JSON_REPLACE(@j, '$[1].b[0]', 1, '$[2][2]', 2); +------------------------------------------------+ | JSON_REPLACE(@j, '$[1].b[0]', 1, '$[2][2]', 2) | +------------------------------------------------+ | ["a", {"b": [1, false]}, [10, 20]] | +------------------------------------------------+
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 10
JSON RemoveJSON_REMOVE() takes a JSON document and one or more paths that specify
values to be removed from the document
mysql> SELECT JSON_REMOVE(@j, '$[2]', '$[1].b[1]', '$[1].b[1]'); +---------------------------------------------------+ | JSON_REMOVE(@j, '$[2]', '$[1].b[1]', '$[1].b[1]') | +---------------------------------------------------+ | ["a", {"b": [true]}] | +---------------------------------------------------+
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 11
Agenda
1.The new JSON data type
2.Inlined JSON path expressions
3.Indexing JSON data
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Inlined JSON Path Expressions
mysql> CREATE TABLE employees (data JSON);Query OK, 0 rows affected (0,01 sec)
mysql> INSERT INTO employees VALUES ('{"id": 1, "name": "Jane"}');Query OK, 1 row affected (0,00 sec)
MySQL 5.7.9 has a new feature, that simplifies queries that deal with JSON data and makes more human-readable: inlined JSON path expressions ,
Copyright © 2015, Oracle and/or its affiliates. All rights reserved.
Inlined JSON Path Expressionsmysql> INSERT INTO employees VALUES ('{"id": 2, "name": "Joe"}');Query OK, 1 row affected (0,00 sec)mysql> SELECT * FROM employees WHERE data->'$.id'= 2;+--------------------------+| data |+--------------------------+| {"id": 2, "name": "Joe"} |+--------------------------+1 row in set (0,01 sec)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 14
Agenda
1.The new JSON data type
2.Inlined JSON path expressions
3.Indexing JSON data
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 15
Indexing JSON Data
• CREATE TABLE t1 ( data JSON, id INTEGER AS (JSON_EXTRACT(data,"$.id")) STORED, PRIMARY KEY(id));– CREATE TABLE t2 (
data JSON, id INTEGER AS (JSON_EXTRACT(data,"$.id")) VIRTUAL, KEY(id));
Use Functional IndexesBoth STORED and VIRTUAL types are supported
Examples:
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 16
References• http://mysqlserverteam.com/inline-json-path-expressions-in-mysql-5-7/• https://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html• https://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html• https://dev.mysql.com/doc/refman/5.7/en/json-functions.html• https://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html• http://www.slideshare.net/GeorgiKodinov/bgoug-2014-developing-using-my-sql• http://www.slideshare.net/gkodinov/bgoug15-json-support-in-mysql-57?qid
=409fcf82-1139-498e-9f14-c4176d26212a&v=default&b=&from_search=1
And more …
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 17
Questions ?
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 18
Safe Harbor StatementThe preceding 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.
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 19