Mysql 5.7 JSON 與 物聯網設備上數完美結合

MySQL 5.7.15 新特性一 (json)html


更多更濃參考官網mysql

https://dev.mysql.com/doc/refman/5.7/en/json.htmlsql


1、測試過程json


1.建立庫表ide

create database veh_history;
use veh_history;
CREATE TABLE `t_equipment_traffic` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '自增主鍵',
  `cn` char(17) NOT NULL COMMENT '設備號',
  `st` datetime NOT NULL  COMMENT '採集時間',
  `description` json,
  PRIMARY KEY (`id`,`st`),
  KEY `idx_cn_ct` (`cn`),
  KEY `idx_st` (`st`) 
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='歷史數據'
PARTITION BY RANGE (to_days(st))
(PARTITION p201601 VALUES LESS THAN (to_days('2016-12-01')),
 PARTITION p201612 VALUES LESS THAN (to_days('2017-01-01')),
 PARTITION p201701 VALUES LESS THAN (to_days('2017-02-01')),
 PARTITION p201702 VALUES LESS THAN (to_days('2017-03-01'))
 );

 

2.插入json類型測試數據 測試

insert into `t_equipment_traffic` (cn,st,description) values ('liaoa12345roiland',now(),
'{"time":"2015-01-01 13:00:00","speed":"100","result":"fail"}');
insert into `t_equipment_traffic` (cn,st,description) values ('liaoa12345roiland',now(),
'{"time":"2017-01-01 13:00:00","speed":"110","result":"fail"}');
insert into `t_equipment_traffic` (cn,st,description) values ('liaoa12345roiland',now(),
'{"time":"2017-01-02 13:00:00","speed":"110","result":"sucessed"}');


3.json簡單查詢方法大數據

select cn,description from t_equipment_traffic where cn='liaoa12345roiland';
select * from t_equipment_traffic where description ->'$.time' > '2016-12-31';
select cn,json_extract(description,'$.speed') AS 'speed',json_extract(description,'$.result') AS 'result' 
from t_equipment_traffic;


4.建立虛擬列ui

ALTER TABLE t_equipment_traffic  ADD description_speed int(10) 
GENERATED ALWAYS AS (description->'$.speed') VIRTUAL;



5.建立索引htm

alter table  t_equipment_traffic add index idx_speed (description_speed);


6.查看執行計劃索引

explain partitions select * from t_equipment_traffic where description_speed=100;


2、小結 

1.mysql 5.7 已開始支持json數據類型,存儲引擎使用Innodb

2.對於物聯網設備上數存儲徹底能夠採用json,大數據量,常常會變動字段的場景十分適合。

3.uuid+json+innodb+partition 解決數據手動同步可能致使的主鍵衝突,常常字段變動,數據的按期刪除。


###############################################

相關文章
相關標籤/搜索