Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | MySQL Document Store 概要編 MySQL Global Business Unit
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
MySQL Document Store 概要編
MySQL Global Business Unit
Copyright © 2016 Oracle and/or its affiliates. All rights reserved. |
Safe Harbor Statement The following is intended to outline our general product direcNon. It is intended for informaNon purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or funcNonality, and should not be relied upon in making purchasing decisions. The development, release, and Nming of any features or funcNonality described for Oracle’s products remains at the sole discreNon of Oracle.
Copyright © 2016 Oracle and/or its affiliates. All rights reserved. |
リレーショナルデータベース • データの完全性 – 正規化 – 制約 (外部キーなど)
• ACID: Atomicity, Consistency, IsolaNon, Durability – トランザクション
• SQL – 強力で多様な操作が可能なクエリ言語 – アプリ開発者が命令を出し、データベースが最も効率的なデータ探索を試みる
Copyright © 2016 Oracle and/or its affiliates. All rights reserved. |
加えて… • MySQLは1995年には市場に • 幅広いシステムでの利用例 • Webでのデファクトスタンダード • 高い拡張性 • 問題が発生しても、多くの既知の例やトラブルシューティング事例 • 小規模から超巨大規模システムでの利用ノウハウ
Copyright © 2016 Oracle and/or its affiliates. All rights reserved. |
ドキュメントデータベース • スキーマレス – スキーマ設計や正規化、外部キー、制約やデータ型不要 – 初期開発を迅速化
• データ構造の柔軟性 – 配列とオブジェクトのネスト構造 – 構造化できないデータやリレーショナルモデルでは非効率なデータ向き (階層構造、製品データベースなど)
– ORマッパーを使わずにオブジェクトを永続化
Copyright © 2016 Oracle and/or its affiliates. All rights reserved. |
ドキュメントデータベース (続き) • JSON – フロントエンドでの利用向き – JavaScriptネイティブ – Node.js や各種JavaScript
• 利用や学習が容易
Copyright © 2016 Oracle and/or its affiliates. All rights reserved. |
Rela%onal Tables
• Table, Column, and Rows
JSON Documents
• A collecNon of a^ribute–value pairs
7
Pre-‐defined data model vs Semi-‐structured data model
{ "name":"Classic Pizza", "price":400, "toppings":[ "Pepperoni", "Parmesan" ] } { "name":"Margherita Pizza", "price":500, "toppings":[ "Basil", "Mozzarella" ], "options":[ { "name":"Olive", "price":100 } ] }
mysql> SELECT * FROM pizza; +------+------------------+-------+ | code | name | price | +------+------------------+-------+ | CLA | Classic Pizza | 400 | | MAR | Margherita Pizza | 500 | +------+------------------+-------+ mysql> SELECT * FROM toppings; +--------+------------+ | p_code | name | +--------+------------+ | CLA | Pepperoni | | CLA | Parmesan | | MAR | Basil | | MAR | Mozzarella | +--------+------------+
Copyright © 2016 Oracle and/or its affiliates. All rights reserved. |
MySQL 5.7のJSONデータ型 mysql> CREATE TABLE employees (data JSON); mysql> INSERT INTO employees VALUES ('{"id": 1, "name": "Jane"}'), ('{"id": 2, "name": "Joe"}'); mysql> SELECT * FROM employees; +-------------------------------------+ | data | +-------------------------------------+ | {"id": 1, "name": "Jane"} | | {"id": 2, "name": "Joe"} | +-------------------------------------+
• INSERT時に検証 • SELECT時に再パース無
• 参照に最適化
• キーをディクショナリに格納
• JSON/SQLで比較可能
• JSON/SQLで変換可能
• JSONネイティブのデータ型をサポート
• 加えて日付、時刻、タイムスタンプをサポート
Copyright © 2016 Oracle and/or its affiliates. All rights reserved. |
MySQL 5.7のJSONデータ型 • ネイティブのJSONデータ型 • JSONの値を(オブジェクト、配列、値)MySQLのテーブルに格納 • バイナリJSON形式で格納 • JSONの値とSQLのネイティブ型で相互に変換可能 • JSONを操作する関数群 – コンテンツを展開 (JSON_EXTRACT, JSON_KEYS etc) – コンテンツを検証 (JSON_CONTAINS etc) – コンテンツを編集 (JSON_SET, JSON_INSERT, JSON_REMOVE etc) – 配列やオブジェクトを生成 (JSON_ARRAY, JSON_OBJECT) – オブジェクトを検索 (JSON_SEARCH)
Copyright © 2016 Oracle and/or its affiliates. All rights reserved. |
生成列
• 演算によって列を生成 • VIRTUAL: 参照時に演算、値は格納しない • STORED: 挿入/更新時に演算、値を格納する • 用途: – 関数インデックス – 複雑な検索条件の結果を生成してキャッシュ – SQL文の演算をシンプルにする
CREATE TABLE order_lines (orderno integer, lineno integer, price decimal(10,2), qty integer, sum_price decimal(10,2) GENERATED ALWAYS AS (qty * price) STORED );
Copyright © 2016 Oracle and/or its affiliates. All rights reserved. |
JSONデータに対するインデックス
• 関数インデックスの手法 • インラインのJSONパスまたはJSON_EXTRACTにてインデックスに入れるフ
ィールドを指定 • VIRTUALとSTOREDのどちらも利用可能
CREATE TABLE employees (data JSON);
ALTER TABLE employees ADD COLUMN name VARCHAR(30) AS (JSON_UNQUOTE(data->”$.name”)) VIRTUAL, ADD INDEX name_idx (name);
Copyright © 2016 Oracle and/or its affiliates. All rights reserved. |
X DevAPI X Plugin (MySQL) ⇔ X Protocol ⇔ X DevAPI (Driver)
• X Pluginを有効にする事で、X Protocol経由で通信可能 • ドキュメント及びテーブル共に処理可能 • NoSQLライクな構文でドキュメントに対しCRUD処理可能 • Fluent API – Connector/Node.js, Connector/J, Connector/.Net, MySQL Shell
12
prod = sess.getSchema("prod") res = prod.users. find("$.name = 'Milk'"). fields(["name", "properties"])
SELECT PLUGIN_NAME,PLUGIN_VERSION,PLUGIN_DESCRIPTION FROM plugins WHERE PLUGIN_NAME = 'mysqlx'; +-------------+----------------+--------------------+ | PLUGIN_NAME | PLUGIN_VERSION | PLUGIN_DESCRIPTION | +-------------+----------------+--------------------+ | mysqlx | 1.0 | X Plugin for MySQL | +-------------+----------------+--------------------+
Copyright © 2016 Oracle and/or its affiliates. All rights reserved. |
X Protocol
クエリ共通セット: • CRUD ==大量に小さなPKを処理 • 独立した複数のクエリーを処理 • 大量のデータを処理
n シャーディング
• 一般的なオペレーションに最適化 n 同期、非同期をサポート (X DevAPI) n メッセージサイズ削減
• パイプライン方式 n 応答を待たずにメッセージ送信可能 n ExpectaNonsによるエラー処理設定
• 警告・通知(global/local)
A NEW PROTOCOL
Pipelining messages is a core feature of the Mysqlx Protocol. It sends messages to the server without waiNng for a response to save latency. (no mandatory handshake) 参考) h^ps://github.com/mysql/mysql-‐server/blob/5.7/rapid/plugin/x/protocol/mysqlx_expect.proto h^ps://dev.mysql.com/doc/internals/en/x-‐protocol-‐messages-‐message-‐structure.html
13
The X Protocol focuses on: • extensibility • performance • security
Copyright © 2016 Oracle and/or its affiliates. All rights reserved. |
X Dev API: コレクションとスキーマの操作 • スキーマへのハンドラを取得 mydb = session.getSchema("mydb");
• コレクションの作成 mydb.createCollection("products");
• コレクションを変数に格納 products = mydb.getCollection("products");
Copyright © 2016 Oracle and/or its affiliates. All rights reserved. |
ドキュメントの追加
products.add({"name":"bananas", "color":"yellow"}).execute();
Copyright © 2016 Oracle and/or its affiliates. All rights reserved. |
ドキュメントの検索
products.find("color = 'yellow'").sort(["name"]).execute();
Copyright © 2016 Oracle and/or its affiliates. All rights reserved. |
ドキュメントの更新
products.modify("product_id = 123").set("color", "red").execute();
Copyright © 2016 Oracle and/or its affiliates. All rights reserved. |
ドキュメントの削除
products.remove("product_id = 123").execute();
Copyright © 2016 Oracle and/or its affiliates. All rights reserved. |
CRUD OperaNons – NoSQL/Document and SQL/RelaNonal
Opera%on Document Rela%onal Create CollecNon.add() Table.insert() Read CollecNon.find() Table.select() Update CollecNon.modify() Table.update() Delete CollecNon.remove() Table.delete()
Copyright © 2016 Oracle and/or its affiliates. All rights reserved. |
5.7.12 Development Preview Release • MySQL 5.7.12 with Document Store plugin • MySQL Shell 1.0.3 • Connector/J 7.0 • Connector/Net 7.0 • Connector/Node.js 1.0
• MySQL 5.7リファレンスマニュアル Chapter 3 Using MySQL as a Document Store – h^p://dev.mysql.com/doc/refman/5.7/en/document-‐store.html
Copyright © 2016 Oracle and/or its affiliates. All rights reserved. |
MySQL 5.7, Connectors, Drivers, and Protocols
MySQL
Plugins
X Protocol Plugin Memcached Plugin Core
MySQL Connectors and Drivers
X Protocol Std Protocol
Memcached driver
X Protocol 33060
Std Protocol 3306
SQL API CRUD and SQL APIs
Memcache Protocol
X and Std Protocols
MySQL Shell
Copyright © 2016 Oracle and/or its affiliates. All rights reserved. |
Resources Topic Link(s)
MySQL as a Document Database h^p://dev.mysql.com/doc/refman/5.7/en/document-‐database.html
MySQL Shell h^p://dev.mysql.com/doc/refman/5.7/en/mysql-‐shell.html h^p://dev.mysql.com/doc/refman/5.7/en/mysqlx-‐shell-‐tutorial-‐javascript.html h^p://dev.mysql.com/doc/refman/5.7/en/mysqlx-‐shell-‐tutorial-‐python.html
X Dev API h^p://dev.mysql.com/doc/x-‐devapi-‐userguide/en/
X Plugin h^p://dev.mysql.com/doc/refman/5.7/en/x-‐plugin.html
MySQL JSON h^p://mysqlserverteam.com/tag/json/ h^ps://dev.mysql.com/doc/refman/5.7/en/json.html h^ps://dev.mysql.com/doc/refman/5.7/en/json-‐funcNons.html
Blogs h^p://mysqlserverteam.com/category/docstore/