未加星标

MySQL 8.0 Labs: JSON aggregation functions

字体大小 | |
[数据库(mysql) 所属分类 数据库(mysql) | 发布者 店小二03 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

In mysql 5.7 we introduced JSON functionality into the MySQL Server. This work included the introduction of a JSON data type, virtual columns and a set of approximately 20 SQL functions that allow you to manipulate and search JSON data on the server side.

The JSON functionality has been well received, and in MySQL 8.0 we have plans to improve it in a number of ways. This post outlines improvements to the SQL functions with the addition of aggregate functions.

TL;DR

Starting with MySQL 8.0 (lab release)* two newaggregationfunctions were added and can be used to combinedata into JSON arrays/objects:

JSON_ARRAYAGG() JSON_OBJECTAGG()

*(To download MySQL 8.0-labs release go to this link and choose “MySQL Server 8.0.0 Optimizer)

mysql> CREATETABLE `t1` ( ->`key`varchar(8) DEFAULT NULL, ->`grp` varchar(8) DEFAULT NULL, ->`val` varchar(8) -> ) ENGINE=InnoDBDEFAULT CHARSET=latin1; QueryOK, 0 rowsaffected (0,01 sec) mysql> mysql> INSERTINTOt1(`key`, `grp`, `val`) VALUES -> ("key1", "g1", "v1"), -> ("key2", "g1", "v2"), -> ("key3", "g2", "v3"); QueryOK, 3 rowsaffected (0,01 sec) Records: 3Duplicates: 0Warnings: 0 mysql> mysql> SELECTJSON_ARRAYAGG(`key`) AS `keys` FROMt1; +--------------------------+ | keys| +--------------------------+ | [ "key1", "key2", "key3" ] | +--------------------------+ 1 rowin set (0,00 sec) mysql> SELECTgrp, JSON_ARRAYAGG(`key`) AS `keys_grouped` FROMt1GROUPBYgrp; +------+------------------+ | grp| keys_grouped| +------+------------------+ | g1| ["key1", "key2"] | | g2| ["key3"]| +------+------------------+ 2 rowsin set (0,00 sec) mysql> SELECTJSON_OBJECTAGG(`key`, val) AS `key_val` FROMt1; +--------------------------------------------+ | key_val| +--------------------------------------------+ | { "key1": "v1", "key2": "v2", "key3": "v3" } | +--------------------------------------------+ 1 rowin set (0,00 sec) mysql> SELECTgrp, JSON_OBJECTAGG(`key`, val) AS `key_val_grouped` FROMt1GROUPBYgrp; +------+------------------------------+ | grp| key_val_grouped| +------+------------------------------+ | g1| {"key1": "v1", "key2": "v2"} | | g2| {"key3": "v3"}| +------+------------------------------+ 2 rowsin set (0,00 sec)

Now for the less impatient:

Let’s think about this scenario: you have a database which contains both structured and semi-structured data and you’ve decided to adopt the EAV model (Entity Attribute Value). The tables will look more or less like this:

You have a product table which contains the common attributes:

CREATETABLE `product` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(120) DEFAULT NULL, `manufacturer` varchar(120) DEFAULT NULL, `price` int(11) DEFAULT NULL, PRIMARYKEY (`id`) ) ENGINE=InnoDBDEFAULT CHARSET=latin1;

Then you have the attribute table containing all the non-common attributes that a product might have:

CREATETABLE `attribute` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(120) DEFAULT NULL, `description` varchar(256) DEFAULT NULL, PRIMARYKEY (`id`) ) ENGINE=InnoDBDEFAULT CHARSET=latin1;

With some possible entries:

INSERTINTOattribute(id, name) VALUES (1, "color"), (2, "material"), (3, "style"), (4, "bulb_type"), (5, "usage"), (6, "cpu_type"), (7, "cpu_speed"), (8, "weight"), (9, "battery_life"), (10, "fuel_type");

And finally the value table which combines the product key, the attribute key with the actual value.

CREATETABLE `value` ( `prod_id` int(11) NOT NULL, `attribute_id` int(11) NOT NULL, `value` text, PRIMARYKEY (`prod_id`,`attribute_id`) ) ENGINE=InnoDBDEFAULT CHARSET=latin1;

Now let’s insert a few products and theirattributes:

INSERTINTOproduct(id, name, manufacturer, price) VALUES (1, "LED Desk Lamp", "X", 26); INSERTINTOvalueVALUES (1, 1, "black"), (1, 2, "plastic"), (1, 3, "classic"), (1, 4, "LED"), (1, 5, "Indoor use only"); INSERTINTOproduct(id, name, manufacturer, price) VALUES (2, "Laptop", "Y", 800); INSERTINTOvalueVALUES (2, 1, "blue"), (2, 6, "quad core"), (2, 7, "3400 mhz"), (2, 8, "2,1 kg"), (2, 9, "9h"); INSERTINTOproduct(id, name, manufacturer, price) VALUES (3, "Grill", "Z", 300); INSERTINTOvalueVALUES (3, 1, "black"), (3, 8, "5 kg"), (3, 10, "gas");

If you need to select complete products that combinesall the attribute keys and values asJSON object , though combining the structured data (in product) and the semi-structured data (in attribute and invalue): you can use the JSON_OBJECTAGG aggregation function:

SELECT JSON_OBJECT("key", p.id, "title", p.name, "manufacturer", p.manufacturer, "price", p.price, "specifications", JSON_OBJECTAGG(a.name, v.value)) as product FROM product as p JOIN value as v ON p.id=v.prod_id JOIN attribute as a ON a.id=v.attribute_id GROUP BY v.prod_id; +---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | product| +---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | { "key": 1, "price": 26, "title": "LED Desk Lamp", "manufacturer": "X", "specifications": { "color": "black", "style": "classic", "usage": "Indoor use only", "material": "plastic", "bulb_type": "LED" } } | | { "key": 2, "price": 800, "title": "Laptop", "manufacturer": "Y", "specifications": { "color": "blue", "weight": "2,1 kg", "cpu_type": "quad core", "cpu_speed": "3400 mhz", "battery_life": "9h" } }| |{ "key": 3, "price": 300, "title": "Grill", "manufacturer": "Z", "specifications": { "color": "black", "weight": "5 kg", "fuel_type": "gas" } }| +---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 3 rows in set (0,01 sec)

To select an array containing all the possible attribute keys for each product JSON_ARRAYAGG can be used:

SELECT p.id, JSON_ARRAYAGG(a.name) as product_attributes FROM product as p JOIN value as v ON p.id=v.prod_id JOIN attribute as a ON a.id=v.attribute_id GROUP BY v.prod_id; +----+--------------------------------------------------------------+ | id | product_attributes| +----+--------------------------------------------------------------+ |1 | ["color", "style", "usage", "material", "bulb_type"]| |2 | ["cpu_type", "weight", "color", "cpu_speed", "battery_life"] | |3 | ["color", "fuel_type", "weight"]| +----+--------------------------------------------------------------+ 3 rows in set (0,01 sec)

The functions can also be used to help you migrate to a new schema having the semi-structured data stored in JSON columns:

CREATETABLEcomplete_productAS (SELECT p.id, p.name, p.manufactu

本文数据库(mysql)相关术语:navicat for mysql mysql workbench mysql数据库 mysql 存储过程 mysql安装图解 mysql教程 mysql 管理工具

主题: SQLMySQLInnoDBAUTIRIMRYUTAUTSQL Server
分页:12
转载请注明
本文标题:MySQL 8.0 Labs: JSON aggregation functions
本站链接:http://www.codesec.net/view/480205.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 数据库(mysql) | 评论(0) | 阅读(48)