当前位置:网站首页>How MySQL queries and modifies JSON data
How MySQL queries and modifies JSON data
2022-07-05 19:14:00 【1024 questions】
Yes json Data query and modification
Several related functions
Example
Inquire about
Let's see the modification
Delete
Insert
Mysql Handle json data
Yes json Data query and modificationUse Field ->'$.json attribute ' Query criteria
Use json_extract Function query ,json_extract( Field , "$.json attribute ")
according to json Array query , use JSON_CONTAINS( Field , JSON_OBJECT('json attribute ', " Content ")) : [{}] Query this form json Array
MySQL5.7 Above support JSON The operation of , And added JSON Storage type
General database storage JSON Type of data will use JSON Type or TEXT type
Several related functionsI didn't create it here json Field format of , It USES text Storage json .
Be careful : use JSON Type words 1)JSON Columns must store JSON Format data , Otherwise, an error will be reported .2)JSON There are no default values for data types .
Insert json Format data into this column :
{"age": "28", "pwd": "lisi", "name": " Li Si "}
Inquire about 1、
select * from `offcn_off_main` where json_extract(json_field,"$.name") = ' Li Si '
2、
select * from `offcn_off_main` where json_field->'$.name' = ' Li Si '
Use explain You can see that the index cannot be used .
So it needs to be modified :
mysql Native does not support json Attribute index in column , But we can pass mysql The virtual column of is indirectly json Create an index for some attributes in , The principle is to json Create virtual columns with attributes in , Then by indexing the virtual columns , Thus, the attribute is indirectly indexed .
stay MySQL 5.7 in , Two kinds of support Generated Column, namely Virtual Generated Column and Stored Generated Column, The former will only Generated Column In the data dictionary ( Table metadata ), It doesn't persist this column of data to disk ; The latter will Generated Column Persist to disk , It's not calculated every time it's read . Obviously , The latter stores data that can be calculated from existing data , Need more disk space , And Virtual Column There is no advantage in comparison ----( In fact, I think there are still advantages. After all, there will be less query and calculation )
therefore ,MySQL 5.7 in , Don't specify Generated Column The type of , The default is Virtual Column.
if necessary Stored Generated Golumn Words , May be in Virtual Generated Column It's more appropriate to index on , In general , All use Virtual Generated Column, This is also MySQL Default method .
The format is as follows :
fieldname <type> [ GENERATED ALWAYS ] AS ( <expression> ) [ VIRTUAL|STORED ] [ UNIQUE [KEY] ] [ [PRIMARY] KEY ] [ NOT NULL ] [ COMMENT <text> ]
So here I am :
ALTER TABLE 'off_main' `names_virtual` VARCHAR(20) GENERATED ALWAYS AS (`json_field` ->> '$.name') not null;
Note: Using operators -» To quote JSON In the field KEY. In this case, the field names_virtual For virtual fields , I define it as not null . In real work , We must set specific conditions . because JSON It is a data object with weak structure . In other words, its structure is not fixed .
Index Virtual fields :
CREATE INDEX `names` ON `off_main`(`names_virtual`);
Note that if the virtual field is not created, the table is added , It's added later , When adding an index, if the virtual field in some rows is null, But it is also set that it cannot be null, Then the index cannot be created successfully , Tips column can not be null.
After adding index explain You can see that the index is used , And the value of the virtual field will follow json The attribute of the field changes automatically .
Let's see the modificationupdate off_main set json_field = json_set(json_field,'$.phone', '132') WHERE id = 45 // Modify multiple at the same time UPDATE offcn_off_main set json_field = json_set(json_field,'$.name',456,'$.age','bbb') WHERE id = 45
json_set() Methods that exist will override , Nonexistent ones will be added .
DeleteUPDATE offcn_off_main set json_field = json_remove(json_field,'$.pwd','$.phone') WHERE id = 45
Insert UPDATE offcn_off_main set json_field = json_insert(json_field,'$.pwd','111') WHERE id = 45
insert And update The difference is insert What does not exist will increase , What exists will not cover
Mysql Handle json data1. If the data is small , take json Data is copied directly to mysql Of json Field , If the data is too large, you can pass java Wait for the backstage form to json Data analysis , Then write to the database .
Query operation
select *,json->'$.features[0].geometry.rings' as rings from JSON;
Read some data from one table and store it in another table ( A piece of data )
insert into DT_village(name, border) SELECT json->'$.features[0].attributes.CJQYMC',json->'$.features[0].geometry.rings'from JSON;
Read json Data and write to the database ( At this time, we use the form of defining function to execute the method , You can define stool volume )
# clear database TRUNCATE table DT_village;# Define stored procedures delimiter //DROP PROCEDURE IF EXISTS insert_test_val;##num_limit Number of data to insert ,rand_limit The largest random number CREATE PROCEDURE insert_test_val() BEGIN DECLARE i int default 0; DECLARE a,b varchar(5000); WHILE i<10 do set a=CONCAT('$.features[',i,'].attributes.CJQYMC'); set b=CONCAT('$.features[',i,'].geometry.rings'); insert into DT_village(name, border) select #json->'$.features[0].attributes.CJQYMC',json->'$.features[0].geometry.rings' # (json->a),(json->b) json_extract(json,a),json_extract(json,b) from JSON; set i = i + 1; END WHILE; END//# Calling stored procedure call insert_test_val();
Call the cursor to get jsosn A row in the data , And perform the insertion operation
delimiter //drop procedure if exists StatisticStore;CREATE PROCEDURE StatisticStore() BEGIN # Create variables to receive cursor data declare j json;# Storage json data DECLARE i int default 0; # Create a total variable , Record the number of executions , Control cycle DECLARE a,b,c varchar(5000);# Definition json The key value of a data in the array # Create an end flag variable declare done int default false; # Create cursors declare cur cursor for select json from JSON where name = '1'; # Specifies the return value at the end of the cursor loop declare continue HANDLER for not found set done = true; # Set initial value set a=CONCAT('$.features[',i,'].attributes.XZQDM'); set b=CONCAT('$.features[',i,'].attributes.XZQMC'); set c=CONCAT('$.features[',i,']'); # Open cursor open cur; # Start looping through the data in the cursor read_loop:loop # According to a piece of data that the cursor currently points to fetch cur into j; # Determine whether the cursor's loop ends if done then leave read_loop;# Jump out of the cursor loop end if; # Here you can do whatever you want WHILE i<11 do insert into dt_border(xzq_code,name,border) select json_extract(j,a),json_extract(j,b),json_extract(j,c) from JSON; set i = i + 1; END WHILE; # End cursor loop end loop; # Close cursor close cur; # Output results select j,i; END;# Calling stored procedure call StatisticStore();
The above is personal experience , I hope I can give you a reference , I also hope you can support the software development network .
边栏推荐
- Oracle 中文排序 Oracle 中文字段排序
- 机器学习基础(三)——KNN/朴素贝叶斯/交叉验证/网格搜索
- 公司破产后,黑石们来了
- ELK分布式日志分析系统部署(华为云)
- Debezium系列之:修改源码支持drop foreign key if exists fk
- CF: B. almost Ternary Matrix [symétrie + règles de recherche + Construction + I am Construction Waste]
- MySQL数据库索引教程(超详细)
- Debezium系列之:postgresql从偏移量加载正确的最后一次提交 LSN
- 2022 the most complete Tencent background automation testing and continuous deployment practice in the whole network [10000 words]
- 视频融合云平台EasyCVR增加多级分组,可灵活管理接入设备
猜你喜欢
跨境支付平台 XTransfer 的低代码实践:如何与其他中台融合是核心
PHP利用ueditor实现上传图片添加水印
Oracle fault handling: ora-10873:file * needs to be either taken out of backup or media recovered
Notion 类生产力工具如何选择?Notion 、FlowUs 、Wolai 对比评测
Tianyi cloud understands enterprise level data security in this way
Technology sharing | interface testing value and system
How to realize the Online timer and offline timer in the game
1亿单身男女撑起一个IPO,估值130亿
Password reset of MariaDB root user and ordinary user
5. Data access - entityframework integration
随机推荐
在线协作产品哪家强?微软 Loop 、Notion、FlowUs
After the company went bankrupt, the blackstones came
Ant group open source trusted privacy computing framework "argot": open and universal
The monthly list of Tencent cloud developer community videos was released in May 2022
RedHat7.4配置yum软件仓库(RHEL7.4)
泰山OFFICE技术讲座:由行的布局高度,谈绘制高度的高度溢出、高度缩水(全网首发)
鱼和熊掌可以兼得!天翼云弹性裸金属一招鲜!
Why can't Bi software do correlation analysis? Take you to analyze
Mysql database indexing tutorial (super detailed)
Django使用mysqlclient服务连接并写入数据库的操作过程
[today in history] July 5: the mother of Google was born; Two Turing Award pioneers born on the same day
一朵云开启智慧交通新未来
Debezium系列之:IDEA集成词法和语法分析ANTLR,查看debezium支持的ddl、dml等语句
Optimization of middle alignment of loading style of device player in easycvr electronic map
紧固件行业供应商绩效考核繁琐?选对工具才能轻松逆袭!
IFD-x 微型红外成像仪(模块)关于温度测量和成像精度的关系
Technology sharing | interface testing value and system
flume系列之:拦截器过滤数据
JAD installation, configuration and integration idea
Windows Oracle 开启远程连接 Windows Server Oracle 开启远程连接