source :blog.csdn.net/java_pfx/article/details/116594654
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 .
# 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]}}
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"}]
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 ?
# 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));
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);
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 :
SELECTuserId,JSON_UNQUOTE(JSON_EXTRACT(loginInfo,"$.cellphone")) cellphone,JSON_UNQUOTE(JSON_EXTRACT(loginInfo,"$.wxchat")) wxchatFROM UserLogin;+--------+-------------+--------------+| userId | cellphone | wxchat |+--------+-------------+--------------+| 1 | 11| Code the agriculture || 2 | 11| NULL |+--------+-------------+--------------+2 rows in set (0.01 sec)
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 :
SELECTuserId,loginInfo->>"$.cellphone" cellphone,loginInfo->>"$.wxchat" wxchatFROM UserLogin;
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);
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 UserLoginWHERE cellphone = '11'G*************************** 1. row ***************************id: 1select_type: SIMPLEtable: UserLoginpartitions: NULLtype: constpossible_keys: idx_cellphonekey: idx_cellphonekey_len: 1023ref: constrows: 1filtered: 100.00Extra: NULL1 row in set, 1 warning (0.00 sec)
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));
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 |+-------+--------------+
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 |+-------+---------------------------------------
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]');
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 UserTagADD INDEX idx_user_tags ((cast((userTags->"$") as unsigned array)));
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 UserTagWHERE 10 MEMBER OF(userTags->"$")G*************************** 1. row ***************************id: 1select_type: SIMPLEtable: UserTagpartitions: NULLtype: refpossible_keys: idx_user_tagskey: idx_user_tagskey_len: 9ref: constrows: 1filtered: 100.00Extra: Using where1 row in set, 1 warning (0.00 sec)SELECT * FROM UserTagWHERE 10 MEMBER OF(userTags->"$");+--------+---------------+| userId | userTags |+--------+---------------+| 1 | [2, 6, 8, 10] || 2 | [3, 10, 12] |+--------+---------------+2 rows in set (0.00 sec)
If you want to query the portrait 80 after , Users who often watch movies , You can use functions
JSON_CONTAINS:EXPLAIN SELECT * FROM UserTagWHERE JSON_CONTAINS(userTags->"$", '[2,10]')G*************************** 1. row ***************************id: 1select_type: SIMPLEtable: UserTagpartitions: NULLtype: rangepossible_keys: idx_user_tagskey: idx_user_tagskey_len: 9ref: NULLrows: 3filtered: 100.00Extra: Using where1 row in set, 1 warning (0.00 sec)SELECT * FROM UserTagWHERE JSON_CONTAINS(userTags->"$", '[2,10]');+--------+---------------+| userId | userTags |+--------+---------------+| 1 | [2, 6, 8, 10] |+--------+---------------+1 row in set (0.00 sec)
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 UserTagWHERE JSON_OVERLAPS(userTags->"$", '[2,3,10]')G*************************** 1. row ***************************id: 1select_type: SIMPLEtable: UserTagpartitions: NULLtype: rangepossible_keys: idx_user_tagskey: idx_user_tagskey_len: 9ref: NULLrows: 4filtered: 100.00Extra: Using where1 row in set, 1 warning (0.00 sec)SELECT * FROM UserTagWHERE JSON_OVERLAPS(userTags->"$", '[2,3,10]');+--------+---------------+| userId | userTags |+--------+---------------+| 1 | [2, 6, 8, 10] || 2 | [3, 10, 12] |+--------+---------------+2 rows in set (0.01 sec)
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 .
Technology exchange group
![]()
D Brother also built a technology group , It mainly aims at whether some new technologies and open source projects are worth studying and IDEA The use of “ Operation ”, Students who are interested in joining the group , Long scan area QR code can be used , Be sure to pay attention to : City + nickname + Technical direction , Note according to the format , Can quickly pass through .
▲ Long press scan
This article is from the official account of WeChat : Program IT circle
Original article , author : Stack leader , If you reprint , Please indicate the source :https://www.cxyquan.com/19823.html












