MySQL5.7 JSON型別列建立索引查詢一例
建立json型別的表test:
mysql> CREATE TABLE test(data JSON);
Query OK, 0 rows affected (0.47 sec)
mysql> insert into test values('{"name":"abc","sex":"nan","area":["1","2"]}');
Query OK, 1 row affected (0.39 sec)
mysql> insert into test values('{"name":"abc","sex":"nan","area":["2","3"]}');
Query OK, 1 row affected (0.39 sec)
mysql> insert into test values('{"name":"abc","sex":"nan","area":["3","4"]}');
Query OK, 1 row affected (0.39 sec)
mysql> select json_type(data) from test;
+-----------------+
| json_type(data) |
+-----------------+
| OBJECT |
+-----------------+
1 row in set (0.15 sec)
mysql> select * from test;
+---------------------------------------------------+
| data |
+---------------------------------------------------+
| {"sex": "nan", "area": ["1", "2"], "name": "abc"} |
+---------------------------------------------------+
1 row in set (0.10 sec)
mysql> select json_extract(data, '$.name' ) from test;
+-------------------------------+
| json_extract(data, '$.name' ) |
+-------------------------------+
| "abc" |
+-------------------------------+
1 row in set (0.00 sec)
mysql> select json_extract(data, '$.sex' ) from test;
+------------------------------+
| json_extract(data, '$.sex' ) |
+------------------------------+
| "nan" |
+------------------------------+
1 row in set (0.00 sec)
mysql> select json_extract(data, '$.area' ) from test;
+-------------------------------+
| json_extract(data, '$.area' ) |
+-------------------------------+
| ["1", "2"] |
+-------------------------------+
1 row in set (0.00 sec)
在data列上,對"area"建立虛擬列
mysql> ALTER TABLE test ADD data_idx varchar(128) GENERATED ALWAYS AS (json_extract(data,'$.area')) VIRTUAL;
Query OK, 0 rows affected (0.93 sec)
Records: 0 Duplicates: 0 Warnings: 0
如果要在JSON列上進行檢索,需要對檢索的key建立虛擬列,然後再虛擬列上建立索引。
mysql> alter table test add index idx_data(data_idx);
Query OK, 0 rows affected (0.67 sec)
Records: 0 Duplicates: 0 Warnings: 0
where條件需要使用虛擬列來進行檢索,執行計劃如下:
mysql> explain select * from test where data_idx='["3", "4"]';
+----+-------------+-------+------------+------+---------------+----------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+----------+---------+-------+------+----------+-------+
| 1 | SIMPLE | test | NULL | ref | idx_data | idx_data | 387 | const | 1 | 100.00 | NULL |
+----+-------------+-------+------------+------+---------------+----------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.04 sec)
發現走了索引
mysql> CREATE TABLE test(data JSON);
Query OK, 0 rows affected (0.47 sec)
mysql> insert into test values('{"name":"abc","sex":"nan","area":["1","2"]}');
Query OK, 1 row affected (0.39 sec)
mysql> insert into test values('{"name":"abc","sex":"nan","area":["2","3"]}');
Query OK, 1 row affected (0.39 sec)
mysql> insert into test values('{"name":"abc","sex":"nan","area":["3","4"]}');
Query OK, 1 row affected (0.39 sec)
mysql> select json_type(data) from test;
+-----------------+
| json_type(data) |
+-----------------+
| OBJECT |
+-----------------+
1 row in set (0.15 sec)
mysql> select * from test;
+---------------------------------------------------+
| data |
+---------------------------------------------------+
| {"sex": "nan", "area": ["1", "2"], "name": "abc"} |
+---------------------------------------------------+
1 row in set (0.10 sec)
mysql> select json_extract(data, '$.name' ) from test;
+-------------------------------+
| json_extract(data, '$.name' ) |
+-------------------------------+
| "abc" |
+-------------------------------+
1 row in set (0.00 sec)
mysql> select json_extract(data, '$.sex' ) from test;
+------------------------------+
| json_extract(data, '$.sex' ) |
+------------------------------+
| "nan" |
+------------------------------+
1 row in set (0.00 sec)
mysql> select json_extract(data, '$.area' ) from test;
+-------------------------------+
| json_extract(data, '$.area' ) |
+-------------------------------+
| ["1", "2"] |
+-------------------------------+
1 row in set (0.00 sec)
在data列上,對"area"建立虛擬列
mysql> ALTER TABLE test ADD data_idx varchar(128) GENERATED ALWAYS AS (json_extract(data,'$.area')) VIRTUAL;
Query OK, 0 rows affected (0.93 sec)
Records: 0 Duplicates: 0 Warnings: 0
如果要在JSON列上進行檢索,需要對檢索的key建立虛擬列,然後再虛擬列上建立索引。
mysql> alter table test add index idx_data(data_idx);
Query OK, 0 rows affected (0.67 sec)
Records: 0 Duplicates: 0 Warnings: 0
where條件需要使用虛擬列來進行檢索,執行計劃如下:
mysql> explain select * from test where data_idx='["3", "4"]';
+----+-------------+-------+------------+------+---------------+----------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+----------+---------+-------+------+----------+-------+
| 1 | SIMPLE | test | NULL | ref | idx_data | idx_data | 387 | const | 1 | 100.00 | NULL |
+----+-------------+-------+------------+------+---------------+----------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.04 sec)
發現走了索引
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/15498/viewspace-2148828/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Mysql索引型別建立錯誤導致SQL查詢緩慢MySql索引型別
- mysql 5.7 json 型別 json 陣列型別 普通字串型別 10w資料 查詢速度差異MySqlJSON型別陣列字串
- MySQL5.7 JSON型別使用介紹MySqlJSON型別
- laravel 中 in 多列特殊查詢型別解決方案Laravel型別
- ElasticSearch分片互動過程(建立索引、刪除索引、查詢索引)Elasticsearch索引
- MongoDB日期型別查詢MongoDB型別
- 媒體查詢中常用的媒體型別羅列型別
- 【索引】Oracle查詢指定索引提高查詢效率索引Oracle
- 筆記 mongo查詢慢日誌,建立索引筆記Go索引
- ibatis查詢date型別BAT型別
- 10分鐘掌握資料型別、索引、查詢的MySQL優化技巧資料型別索引MySql優化
- MySQL 的索引型別及如何建立維護MySql索引型別
- 如何利用mysql5.7提供的虛擬列來提高查詢效率MySql
- 建立自定義塊 - 型別檢查型別
- oracle查詢表資訊(索引,外來鍵,列等)Oracle索引
- 組合索引的前導列與查詢——ORACLE索引Oracle
- msyql千萬級別查詢優化之索引優化索引
- MySQL bit型別增加索引後查詢結果不正確案例淺析MySql型別索引
- C++ 變數型別查詢C++變數型別
- 初識TypeScript:查詢指定路徑下的檔案按型別生成jsonTypeScript型別JSON
- MySQL5.7 虛擬列實現表示式索引MySql索引
- RAW資料型別可以建立索引,但是不走索引測試(轉)資料型別索引
- MySQL通過通用列索引來提供一個JSON列索引MySql索引JSON
- SAP 物料移動型別查詢表型別
- elasticsearch之多索引查詢Elasticsearch索引
- Elasticsearch(三):索引查詢Elasticsearch索引
- 查詢索引 常用SQL索引SQL
- 查詢相似的索引索引
- MySQ索引操作命令總結(建立、重建、查詢和刪除索引命令詳解)索引
- Sqlserver自動查詢缺失索引及拼出建立索引的語句的指令碼SQLServer索引指令碼
- Laravel 對於 Mysql 欄位string型別查詢,當使用數字對這個欄位進行查詢,PHP弱型別語言導致索引失效LaravelMySql型別PHP索引
- TypeScript 索引型別TypeScript索引型別
- 索引的型別索引型別
- 6.5 建立一個masked型別的陣列型別陣列
- 資料列not null對索引影響一例Null索引
- 巧用函式索引解決資料傾斜列查詢函式索引
- oracle並行查詢一例薦Oracle並行
- Laravel 查詢資料庫欄位內容是 Json 陣列時的查詢語句Laravel資料庫JSON陣列