当前位置:网站首页>MySQL + JSON = King fried
MySQL + JSON = King fried
2022-07-01 17:43:00 【End code life】
Relational structured storage has some disadvantages , Because it needs to define all columns and their corresponding types in advance . But the business is developing , You may need to extend the description function of a single column , At this time , If you can use it well JSON data type , Then we can get through the boundary between the storage of relational and non relational data , Provide better architecture choices for business .
Of course , Many students are using JSON Various problems will be encountered in data types , One of the most common mistakes is to type JSON Simply understood as string type . But when you finish reading this article , Will really realize JSON The power of data types , So as to better store unstructured data in practical work .
1、JSON data type
JSON(JavaScript Object Notation) It is mainly used for data exchange between Internet application services .MySQL Support RFC 7159 Defined JSON standard , There are mainly JSON object and JSON Array Two types of . The following is JSON object , It is mainly used to store relevant information of pictures :
{
"Image": {
"Width": 800,
"Height": 600,
"Title": "View from 15th Floor",
"Thumbnail": {
"Url": "http://www.example.com/image/xx9943",
"Height": 125,
"Width": 100
},
"IDs": [116, 943, 234, 38793]
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
You can see from it that , JSON Type can well describe the relevant content of data , For example, the width of this picture 、 Height 、 Title, etc. ( The types used here are integers 、 String type ).
JSON Object in addition to supporting strings 、 integer 、 The date type ,JSON Embedded fields also support array types , As in the code above IDs Field .
Another kind JSON The data type is array type , Such as :
[
{
"precision": "zip",
"Latitude": 37.7668,
"Longitude": -122.3959,
"Address": "",
"City": "SAN FRANCISCO",
"State": "CA",
"Zip": "94107",
"Country": "US"
},
{
"precision": "zip",
"Latitude": 37.371991,
"Longitude": -122.026020,
"Address": "",
"City": "SUNNYVALE",
"State": "CA",
"Zip": "94085",
"Country": "US"
}
]
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
The above example demonstrates a JSON Array , Among them is 2 individual JSON object .
up to now , Maybe many students will put JSON As a large field string type , On the surface , No mistake. . But essentially ,JSON It's a new type , Has its own storage format , You can also create an index on each corresponding field , Do specific optimization , This cannot be achieved by traditional field strings .JSON Another benefit of types is that there is no need to predefine fields , Fields can be expanded indefinitely . The columns of traditional relational database need to be defined in advance , If you want to expand, you need to execute ALTER TABLE … ADD COLUMN … Such a heavy operation .
It's important to note that ,JSON Type is from MySQL 5.7 Version starts to support features , and 8.0 Version solves the problem of updating JSON Log performance bottleneck . If you want to use it in a production environment JSON data type , Highly recommended MySQL 8.0 edition .
So far , You have to JSON The basic concept of type has been understood , Next , We entered the actual combat link : How to make good use of JSON type ?
2、 Business table structure design practice
User login design
In the database ,JSON Type is more suitable for storing some less modified 、 Relatively static data , For example, the storage of user login information is as follows :
DROP TABLE IF EXISTS UserLogin;
CREATE TABLE UserLogin (
userId BIGINT NOT NULL,
loginInfo JSON,
PRIMARY KEY(userId)
);
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
Due to the more and more diversified login methods of current business , As the same account supports mobile phones 、 WeChat 、QQ To login , So you can use JSON Type stores login information .
next , Insert the data below :
SET @a = '
{
"cellphone" : "1",
"wxchat" : " Code the agriculture ",
"77" : "1"
}
';
INSERT INTO UserLogin VALUES (1,@a);
SET @b = '
{
"cellphone" : "1188"
}
';
INSERT INTO UserLogin VALUES (2,@b);
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
As you can see from the example above , user 1 There are three ways to log in : Mobile phone verification code login 、 Wechat login 、QQ Sign in , And users 2 Only the mobile phone verification code can log in .
And if you don't use JSON data type , You need to use the following method to create a table :
SELECT
userId,
JSON_UNQUOTE(JSON_EXTRACT(loginInfo,"$.cellphone")) cellphone,
JSON_UNQUOTE(JSON_EXTRACT(loginInfo,"$.wxchat")) wxchat
FROM UserLogin;
+--------+-------------+--------------+
| userId | cellphone | wxchat |
+--------+-------------+--------------+
| 1 | 11| Code the agriculture |
| 2 | 11| NULL |
+--------+-------------+--------------+
2 rows in set (0.01 sec)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
Yes, of course , Every time JSON_EXTRACT、JSON_UNQUOTE Very trouble ,MySQL It also provides ->> expression , And above SQL The effect is exactly the same :
SELECT
userId,
loginInfo->>"$.cellphone" cellphone,
loginInfo->>"$.wxchat" wxchat
FROM UserLogin;
- 1.
- 2.
- 3.
- 4.
- 5.
When JSON Very large amount of data , Users want to know more about JSON When data is effectively retrieved , You can use MySQL Of Function index Functional pair JSON Index a field in .
For example, in the user login example above , Suppose that the user must bind a unique mobile phone number , And hope to use mobile phone number for user retrieval in the future , You can create the following index :
ALTER TABLE UserLogin ADD COLUMN cellphone VARCHAR(255) AS (loginInfo->>"$.cellphone");
ALTER TABLE UserLogin ADD UNIQUE INDEX idx_cellphone(cellphone);
- 1.
- 2.
- 3.
Above SQL First, create a virtual column cellphone, This column is made up of functions loginInfo->>"$.cellphone" Calculated . Then create a unique index on this virtual column idx_cellphone. At this time, through the virtual column cellphone The query , You can see that the optimizer will use the newly created idx_cellphone Indexes :
EXPLAIN SELECT * FROM UserLogin
WHERE cellphone = '11'\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: UserLogin
partitions: NULL
type: const
possible_keys: idx_cellphone
key: idx_cellphone
key_len: 1023
ref: const
rows: 1
filtered: 100.00
Extra: NULL
1 row in set, 1 warning (0.00 sec)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
Of course , We can start by creating tables , Complete the creation of virtual columns and functional indexes . The columns created in the following table cellphone The corresponding is JSON The content in , It's a virtual column ;uk_idx_cellphone Is in the virtual column cellphone Index created on .
CREATE TABLE UserLogin (
userId BIGINT,
loginInfo JSON,
cellphone VARCHAR(255) AS (loginInfo->>"$.cellphone"),
PRIMARY KEY(userId),
UNIQUE KEY uk_idx_cellphone(cellphone)
);
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
User portrait design
Some businesses need user portraits ( That is to label users ), Then according to the user's label , Through data mining technology , Make corresponding product recommendations . such as :
- In the e-commerce industry , According to the user's dressing preferences , Recommend corresponding products ;
- In the music industry , According to the music style users like and the singers they often listen to , Recommend the corresponding song ;
- In the financial industry , According to the user's risk preference and investment experience , Recommend corresponding financial products .
Here , I strongly recommend that you use JSON Type stores user portrait information in the database , And combine JSON The characteristics of array type and multivalued index are used for efficient query . Suppose there is a picture definition table :
CREATE TABLE Tags (
tagId bigint auto_increment,
tagName varchar(255) NOT NULL,
primary key(tagId)
);
SELECT * FROM Tags;
+-------+--------------+
| tagId | tagName |
+-------+--------------+
| 1 | 70 after |
| 2 | 80 after |
| 3 | 90 after |
| 4 | 00 after |
| 5 | Love sports |
| 6 | Highly educated |
| 7 | Small endowment |
| 8 | A house |
| 9 | A car |
| 10 | I often go to the cinema |
| 11 | Love online shopping |
| 12 | Love takeout |
+-------+--------------+
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
You can see , surface Tags It's a picture definition table , Used to describe how many tags are currently defined , Then label each user , Such as user David, His label is 80 after 、 Highly educated 、 Small endowment 、 A house 、 I often go to the cinema ; user Tom,90 after 、 I often go to the cinema 、 Love takeout .
If not JSON Data type for label storage , The user tag is usually passed through a string , How to add a delimiter , Access all user tags in one field :
+-------+---------------------------------------+
| user | label |
+-------+---------------------------------------+
|David |80 after ; Highly educated ; Small endowment ; A house ; I often go to the cinema |
|Tom |90 after ; I often go to the cinema ; Love takeout |
+-------+---------------------------------------
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
The disadvantage of doing so is : Users who are not good at searching for specific portraits , In addition, the separator is also a self convention , In fact, other data can be stored arbitrarily in the database , Finally, dirty data is generated .
use JSON Data type can solve this problem well :
DROP TABLE IF EXISTS UserTag;
CREATE TABLE UserTag (
userId bigint NOT NULL,
userTags JSON,
PRIMARY KEY (userId)
);
INSERT INTO UserTag VALUES (1,'[2,6,8,10]');
INSERT INTO UserTag VALUES (2,'[3,10,12]');
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
among ,userTags The stored tag is the table Tags Those tag values that have been defined , Just use JSON Array type for storage .
MySQL 8.0.17 Version starting support Multi-Valued Indexes, Used in JSON Create index on array , And through the function member of、json_contains、json_overlaps To quickly retrieve index data . So you can watch UserTag To create a Multi-Valued Indexes:
ALTER TABLE UserTag
ADD INDEX idx_user_tags ((cast((userTags->"$") as unsigned array)));
- 1.
- 2.
- 3.
If you want to query the user portrait, it is a user who often watches movies , You can use functions MEMBER OF:
EXPLAIN SELECT * FROM UserTag
WHERE 10 MEMBER OF(userTags->"$")\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: UserTag
partitions: NULL
type: ref
possible_keys: idx_user_tags
key: idx_user_tags
key_len: 9
ref: const
rows: 1
filtered: 100.00
Extra: Using where
1 row in set, 1 warning (0.00 sec)
SELECT * FROM UserTag
WHERE 10 MEMBER OF(userTags->"$");
+--------+---------------+
| userId | userTags |
+--------+---------------+
| 1 | [2, 6, 8, 10] |
| 2 | [3, 10, 12] |
+--------+---------------+
2 rows in set (0.00 sec)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
If you want to query the portrait 80 after , Users who often watch movies , You can use functions
JSON_CONTAINS:
EXPLAIN SELECT * FROM UserTag
WHERE JSON_CONTAINS(userTags->"$", '[2,10]')\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: UserTag
partitions: NULL
type: range
possible_keys: idx_user_tags
key: idx_user_tags
key_len: 9
ref: NULL
rows: 3
filtered: 100.00
Extra: Using where
1 row in set, 1 warning (0.00 sec)
SELECT * FROM UserTag
WHERE JSON_CONTAINS(userTags->"$", '[2,10]');
+--------+---------------+
| userId | userTags |
+--------+---------------+
| 1 | [2, 6, 8, 10] |
+--------+---------------+
1 row in set (0.00 sec)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
If you want to query the portrait 80 after 、90 after , Users who often watch movies , You can use the function
JSON_OVERLAP:
EXPLAIN SELECT * FROM UserTag
WHERE JSON_OVERLAPS(userTags->"$", '[2,3,10]')\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: UserTag
partitions: NULL
type: range
possible_keys: idx_user_tags
key: idx_user_tags
key_len: 9
ref: NULL
rows: 4
filtered: 100.00
Extra: Using where
1 row in set, 1 warning (0.00 sec)
SELECT * FROM UserTag
WHERE JSON_OVERLAPS(userTags->"$", '[2,3,10]');
+--------+---------------+
| userId | userTags |
+--------+---------------+
| 1 | [2, 6, 8, 10] |
| 2 | [3, 10, 12] |
+--------+---------------+
2 rows in set (0.01 sec)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
JSON The type is MySQL 5.7 New data type in version , The use of good JSON Data type can effectively solve many practical problems in business . Last , Let me summarize today's highlights :
Use JSON data type , Recommend to use MySQL 8.0.17 Version above , Better performance , It also supports Multi-Valued Indexes
JSON The advantage of data types is that there is no need to define columns in advance , The data itself is well descriptive ;
Don't use data with obvious relationship JSON Storage , Such as user balance 、 User name 、 User ID card, etc , These are the data that every user must include ;
JSON The data type is recommended for static data storage that is not updated frequently .
PS: Prevent missing this article , You can like it , It's easy to browse and search
边栏推荐
- New 95 community system whole station source code
- Replace UUID, nanoid is faster and safer!
- Sword finger offer 20 String representing numeric value
- 中国锦纶长丝缝纫线发展预测与投资方向研究报告(2022版)
- 存在安全隐患 起亚召回部分K3新能源
- Vulnhub range hacker_ Kid-v1.0.1
- (27) Open operation, close operation, morphological gradient, top hat, black hat
- 目前炒期货在哪里开户最正规安全?怎么期货开户?
- Kia recalls some K3 new energy with potential safety hazards
- Report on research and investment prospects of China's silicon nitride ceramic substrate industry (2022 Edition)
猜你喜欢

vulnhub靶场-Hacker_Kid-v1.0.1
![[mathematical modeling] [matlab] implementation of two-dimensional rectangular packing code](/img/de/1f572c62a0d034da9a8acb5c2f9602.jpg)
[mathematical modeling] [matlab] implementation of two-dimensional rectangular packing code

Gold, silver and four job hopping, interview questions are prepared, and Ali becomes the champion

ACL 2022 | 分解的元学习小样本命名实体识别

Good looking UI mall source code has been scanned, no back door, no encryption
![Integer array merge [JS]](/img/0d/70535e0eb1c299bda25159b58c70d7.png)
Integer array merge [JS]
![[splishsplash] about how to receive / display user parameters, MVC mode and genparam on GUI and JSON](/img/83/9bd9ce7608ebfe7207ac008b9e8ab1.png)
[splishsplash] about how to receive / display user parameters, MVC mode and genparam on GUI and JSON

ACM mm 2022 video understanding challenge video classification track champion autox team technology sharing

ISO 27001 Information Security Management System Certification

Technical secrets of ByteDance data platform: implementation and optimization of complex query based on Clickhouse
随机推荐
Research Report on China's enzyme Market Forecast and investment strategy (2022 Edition)
Pyqt5, draw a histogram on the control
Mysql database - Advanced SQL statement (2)
The latest intelligent factory MES management system software solution
Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)
[Supplément linguistique c] déterminer quel jour est demain (date de demain)
Source code of new campus errand / campus task platform on mutual station
(十六)ADC转换实验
Heavy disclosure! Hundreds of important information systems have been invaded, and the host has become a key attack target
GameFramework食用指南
[C supplement] [string] display the schedule of a month by date
China biodegradable plastics market forecast and investment strategy report (2022 Edition)
两数之和c语言实现[通俗易懂]
Object. fromEntries()
[beauty detection artifact] come on, please show your unique skill (is this beauty worthy of the audience?)
Irradiance, Joule energy, exercise habits
PHP implements sensitive word filtering system "suggestions collection"
期货先锋这个软件正规吗安全吗?选择哪家期货公司更安全?
徽商期货是正规期货平台吗?在徽商期货开户安全吗?
字节跳动数据平台技术揭秘:基于 ClickHouse 的复杂查询实现与优化