bgoug15: json support in mysql 5.7

25
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | JSON Support in MySQL 5.7 Georgi “Joro” Kodinov Team lead, MySQL server general team

Upload: georgi-kodinov

Post on 25-Jan-2017

3.405 views

Category:

Software


2 download

TRANSCRIPT

Page 1: BGOUG15: JSON support in MySQL 5.7

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

JSON Supportin MySQL 5.7

Georgi “Joro” KodinovTeam lead, MySQL server general team

Page 2: BGOUG15: JSON support in MySQL 5.7

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

Agenda

The new JSON data type

Inlined JSON path expressions

The new JSON functions

Indexing JSON data

Page 3: BGOUG15: JSON support in MySQL 5.7

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

CREATE TABLE employees (data JSON);INSERT INTO employees VALUES ('{"id": 1, "name": "Jane"}'), ('{"id": 2, "name": "Joe"}');SELECT * FROM employees;+-------------------------------------+| data |+-------------------------------------+| {"id": 1, "name": "Jane"} || {"id": 2, "name": "Joe"} |+-------------------------------------+

• Validation on INSERT

• No reparsing on SELECT

• Dictionary of fields

• Fields are sorted

• Can compare JSON/SQL

• Can convert JSON/SQL

• Supports all native JSON datatypes

• Also supports date, time, timestamp etc.

The New JSON Datatype

Page 4: BGOUG15: JSON support in MySQL 5.7

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

JSON vs TEXT columnsPros Cons

JSON• Validate once• Fast access• Can update in-place

• Slower to insert• Unreadable as is• Sets certain limitations on JSON

TEXT• Fast to insert• Human readable

• Requires manual validation• Requires manual parsing• Harder to update

Page 5: BGOUG15: JSON support in MySQL 5.7

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

Beware: SQL vs JSON comparisons !SQL JSON

create table t1 (data json); create table t2 ( id integer, data varchar(20));

insert into t1 values ('{ "id": 1, "data": "1" }'), ('{ "id": 2, "data": "3" }');

insert into t2 values (1, '1'), (2, '3');

select count(*) from t1 where data->'$.id' = data->'$.data';

select count(*) from t2 where id = data;

0 rows ! 1 row !

Page 6: BGOUG15: JSON support in MySQL 5.7

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

Agenda

The new JSON data type

Inlined JSON path expressions

The new JSON functions

Indexing JSON data

Page 7: BGOUG15: JSON support in MySQL 5.7

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

Inlined JSON Path Expressions• <field>->'<JSON path expression>'

e.g. data->'$.some.key[3].from.doc'• Syntax sugar over JSON_EXTRACT function• SELECT * FROM employees WHERE data->'$.id'= 2;• ALTER … ADD COLUMN id INT AS (data->'$.id') …• CREATE VIEW .. AS SELECT data->'$.id', data->'$.name' FROM …• UPDATE employees SET data->'$.name'=‘John' WHERE … Not

yet!

Page 8: BGOUG15: JSON support in MySQL 5.7

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

Limitations of Inlined JSON Path Expressions

Inlined JSON path JSON_EXTRACT()

Data source Field Any JSON value

Path expression SQL Constant SQL Expression

# of expressions One Multiple

Page 9: BGOUG15: JSON support in MySQL 5.7

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

Supported JSON Paths[[[database.]table.]column]$<path spec>

Expression Example[ [ [database.] table.] field]$ db.phonebook.data$

$ Current document’s root$.identifier $.user.address.street[array] $.user.addresses[2].street.* and [*] $.user.addresses[*].street** $.user**.phone

Not yet!

Page 10: BGOUG15: JSON support in MySQL 5.7

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

Agenda

The new JSON data type

Inlined JSON path expressions

The new JSON functions

Indexing JSON data

Page 11: BGOUG15: JSON support in MySQL 5.7

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

New functions to handle JSON data

Example ResultSELECT JSON_VALID('{ "a":1 }'); 1SELECT JSON_TYPE('[ 1, 2, 3 ]'); ARRAYSELECT JSON_KEYS('{ "a":1, "b": 2 }'); ["a", "b"]SELECT JSON_LENGTH('[ 1, 2, 3 ]'); 3SELECT JSON_DEPTH('{ "a":{ "c": 1 }, "b": 2 }'); 3

Information

Page 12: BGOUG15: JSON support in MySQL 5.7

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

New functions to handle JSON data

Example ResultSELECT JSON_REMOVE('{ "a":1, "b": 2 }', '$.a'); {"b": 2}SELECT JSON_ARRAY_APPEND('[1,[2,3],4]', '$[1]', 5); [1, [2, 3, 5], 4]SELECT JSON_SET('{ "a":1 }', '$.c', 3); {"a": 1, “c": 3}SELECT JSON_INSERT('{ "a":1 }', '$.b', 4); {"a": 1, "b": 4}SELECT JSON_REPLACE('{ "a":1, "b": 2 }', '$.b', 3); {"a": 1, "b": 3}SELECT JSON_MERGE('{ "a": 1 }', '{"b":2}'); {"a": 1, "b": 2}SELECT JSON_UNQUOTE('"abc"'); abc

Modification

Page 13: BGOUG15: JSON support in MySQL 5.7

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

New functions to handle JSON data

Example ResultSELECT JSON_ARRAY(1, '2', null, true); [1, "2", null, true]SELECT JSON_OBJECT(1, 2, '3', true); {"1": 2, "3": true}SELECT JSON_QUOTE('"null"'); "\"null\""

Create JSON objects

Page 14: BGOUG15: JSON support in MySQL 5.7

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

New functions to handle JSON data

Example ResultSELECT JSON_CONTAINS_PATH( '{ "a":{ "c": 1 }, "b": 2 }', 'one', '$.a.c');

1

SELECT JSON_CONTAINS( '{"a": 1, "b": "2" }', '1', '$.a'); 1SELECT JSON_EXTRACT('{"a": 1, "n": { "b": 2}}', '$.n'); {"b": 2}SELECT JSON_SEARCH( '{"a": "1", "b": "2" }', 'one', 2); "$.b"

Search in JSON data

Page 15: BGOUG15: JSON support in MySQL 5.7

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

New functions to handle JSON dataFurther reading: http://dev.mysql.com/doc/refman/5.7/en/

Page 16: BGOUG15: JSON support in MySQL 5.7

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

Agenda

The new JSON data type

Inlined JSON path expressions

The new JSON functions

Indexing JSON data

Page 17: BGOUG15: JSON support in MySQL 5.7

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

Indexing JSON Data• Use Functional Indexes– Both STORED and VIRTUAL types are supported

• Examples:– 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));

Page 18: BGOUG15: JSON support in MySQL 5.7

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

Indexing JSON: STORED vs VIRTUAL columnsPros Cons

STORED• Can be primary key too• All index types supported• Looks like a normal field

• Slow ALTER TABLE• Takes space on disk

VIRTUAL• Instant ALTER TABLE• Faster INSERT• Looks like a normal field

• Secondary key only • BTREE index only

Page 19: BGOUG15: JSON support in MySQL 5.7

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

How do you tell if an JSON index is used ?> EXPLAIN SELECT data FROM t1 WHERE JSON_EXTRACT(data,"$.series") BETWEEN 3 AND 5;+----+----------------+--------+---------------+--------+…+------------------------------+| id | select_type | table | partitions | type | | Extra |+----+----------------+--------+---------------+--------+…+------------------------------+| 1 | SIMPLE | t1 | NULL | range | | Using index condition |+----+----------------+--------+---------------+--------+…+------------------------------+

Page 20: BGOUG15: JSON support in MySQL 5.7

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

Or this way ….

Page 21: BGOUG15: JSON support in MySQL 5.7

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

Or maybe this way ?

21

ALTER TABLE features ADD feature_type VARCHAR(30) AS (feature->"$.type") VIRTUAL;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

Down from 1.25s !

Meta data change only (FAST). Does not need to touch table.

Online CREATE INDEX !No rows were

modified.

Page 22: BGOUG15: JSON support in MySQL 5.7

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

Roadmap• Online alter for virtual columns• Advanced JSON functions• In-place update of JSON/BLOB• Full text and GIS index on virtual columns• Improved performance through condition pushdown

Page 23: BGOUG15: JSON support in MySQL 5.7

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

Questions ? @gkodinov, [email protected] if you forget !

Page 24: BGOUG15: JSON support in MySQL 5.7

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

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.

Page 25: BGOUG15: JSON support in MySQL 5.7

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