当前位置:网站首页>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
边栏推荐
- Enter wechat applet
- Develop those things: easycvr cluster device management page function display optimization
- ACL 2022 | 分解的元学习小样本命名实体识别
- 为什么你要考虑使用Prisma
- Development cost of smart factory management system software platform
- 期货先锋这个软件正规吗安全吗?选择哪家期货公司更安全?
- Research Report on development prediction and investment direction of nylon filament sewing thread in China (2022 Edition)
- Smart factory digital management system software platform
- 线上开通ETF基金账户安全吗?有哪些步骤?
- 中国氮化硅陶瓷基板行业研究与投资前景报告(2022版)
猜你喜欢
Good looking UI mall source code has been scanned, no back door, no encryption
Penetration practice vulnhub range Tornado
Oom caused by improper use of multithreading
Penetration practice vulnhub range Nemesis
LeetCode中等题之TinyURL 的加密与解密
Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)
SQL injection vulnerability (MySQL and MSSQL features)
[C language foundation] 12 strings
ACM mm 2022 video understanding challenge video classification track champion autox team technology sharing
【Try to Hack】vulnhub DC4
随机推荐
中国PBAT树脂市场预测及战略研究报告(2022版)
反射型XSS漏洞
China metallocene polyethylene (MPE) Industry Research Report (2022 Edition)
PIP version problems: PIP problems still occur when installing akshare and using Tsinghua source and Douban source
Enter wechat applet
中国锦纶长丝缝纫线发展预测与投资方向研究报告(2022版)
Yyds dry inventory MySQL RC transaction isolation level implementation
Thinkphp6 - CMS multi wechat management system source code
Vulnhub range hacksudo Thor
Intel's open source deep learning tool library openvino will increase cooperation with local software and hardware parties and continue to open
(十六)ADC转换实验
SQL注入漏洞(Mysql与MSSQL特性)
Unity3d extended toolbar
Work and leisure suggestions of old programmers
(17) DAC conversion experiment
在MeterSphere接口测试中如何使用JMeter函数和MockJS函数
How to use JMeter function and mockjs function in metersphere interface test
【Try to Hack】vulnhub DC4
[C supplement] [string] display the schedule of a month by date
Maizeer: the two batches of products reported by the media have been taken off the shelves and sealed, and consumer appeals are accepted