共計 5727 個字符,預計需要花費 15 分鐘才能閱讀完成。
丸趣 TV 小編給大家分享一下 MySQL5.7 新特性有哪些,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
6.1 優化 (工具方面) 增強
5.7 版本中如果一個會話正在執行 sql,且該 sql 是支持 explain 的,那么我們可以通過指定會話 id,查看該 sql 的執行計劃。
EXPLAIN [options] FOR CONNECTION connection_id
該功能可以在一個會話里面查看另外一個會話中正在執行的長查詢。
mysql show processlist;
+—-+————-+—————–+——+———+——+——————————————————–+——————+
| Id | User | Host | db | Command | Time | State | Info |
+—-+————-+—————–+——+———+——+——————————————————–+——————+
| 1 | system user | | NULL | Connect | 78 | Connecting to master | NULL |
| 2 | system user | | NULL | Connect | 78 | Slave has read all relay log; waiting for more updates | NULL |
| 3 | system user | | NULL | Connect | 78 | Waiting for an event from Coordinator | NULL |
| 4 | system user | | NULL | Connect | 78 | Waiting for an event from Coordinator | NULL |
| 5 | system user | | NULL | Connect | 78 | Waiting for an event from Coordinator | NULL |
| 6 | system user | | NULL | Connect | 78 | Waiting for an event from Coordinator | NULL |
| 8 | root | localhost:47896 | NULL | Query | 0 | starting | show processlist |
| 9 | root | localhost:47897 | NULL | Query | 3 | User sleep | select sleep(10) |
+—-+————-+—————–+——+———+——+——————————————————–+——————+
8 rows in set (0.00 sec)
mysql explain FOR CONNECTION 9;
+—-+————-+——-+————+——+—————+——+———+——+——+———-+—————-+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+—-+————-+——-+————+——+—————+——+———+——+——+———-+—————-+
| 1 | SIMPLE | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | No tables used |
+—-+————-+——-+————+——+—————+——+———+——+——+———-+—————-+
1 row in set (0.00 sec)
6.2 hint 功能增強
相比于 MySQL5.6 版本的 hint 主要是 index 級別的 hint 和控制表 join 順序的 hint,5.7.7 之后,MySQL 增加了優化器 hint, 來控制 sql 執行的方式,因為目前 MySQL 支持 nest loop join,故暫時無 hint 來修改 sql 的 join 方式。熟悉 Oracle 的朋友是否會發現 MySQL 和 Oracle 在功能上越來越近了。話說回來 5.7 的 hint (先別和 index hint 比較) 的用法,和 oracle 的類似:
SELECT /*+ NO_RANGE_OPTIMIZATION(t3 PRIMARY, f2_idx) */ f1 FROM t3 WHERE f1 30 AND f1
SELECT /*+ BKA(t1) NO_BKA(t2) */ * FROM t1 INNER JOIN t2 WHERE …;
SELECT /*+ NO_ICP(t1, t2) */ * FROM t1 INNER JOIN t2 WHERE …;
SELECT /*+ SEMIJOIN(FIRSTMATCH, LOOSESCAN) */ * FROM t1 …;
EXPLAIN SELECT /*+ NO_ICP(t1) */ * FROM t1 WHERE …
優化器級別的 hint 分四種類型
Global: The hint affects the entire statement
Query block: The hint affects a particular query block within a statement,什么是 query block
(SELECT …) UNION (SELECT /*+ … */ …) – 后面的括號里面的稱為 query block。
Table-level: The hint affects a particular table within a query block
Index-level: The hint affects a particular index within a table
6.3 觸發器功能增強
5.7 版本之前一個表 對于每種 action(INSERT,UPDATE, DELETE) 和時機(BEFORE or AFTER) 只能支持一種類型的觸發器。新版本可以針對同一個 action 支持多個觸發器。
6.4 syslog 功能
之前的版本,*nix 系統上的 MySQL 支持將錯誤日志發送到 syslog 是通過 mysqld_safe 捕獲錯誤輸出然后傳遞到 syslog 來實現的。新的版本原生支持將錯誤日志輸出到 syslog, 且適用于 windows 系統,只需要通過簡單的參數(log_syslog 等) 配置即可。參考 官方文檔
MySQL 支持–syslog 選項,可將在交互式模式下執行過的命令輸出到 syslog 中(*nix 系統下一般是.mysql_history)。對于匹配“ignore”過濾規則(可通過 –histignore 選項或者 MYSQL_HISTIGNORE 環境變量進行設置)的語句不會被記入。關于 mysql 客戶端的日志使用參見:官方文檔
6.5 虛擬列
在 MySQL 5.7 中,支持兩種 Generated Column,
1 Virtual Generated Column : 只將 Generated Column 保存在數據字典中表的元數據,每次讀取該列時進行計算,并不會將這一列數據持久化到磁盤上;
注意:MySQL 5.7.8 以前 虛擬列字段不支持創建索引。5.7.8 之后 Innodb 支持在虛擬列創建輔助索引。
2 Stored Generated Column : 將 Column 持久化到存儲,會占用一定的存儲空間。與 Virtual Column 相比并沒有明顯的優勢,因此,MySQL 5.7 中,不指定 Generated Column 的類型,默認是 Virtual Column。
創建虛擬列語法:
col_name data_type [GENERATED ALWAYS] AS (expression)
[VIRTUAL | STORED] [UNIQUE [KEY]] [COMMENT comment]
[[NOT] NULL] [[PRIMARY] KEY]
具體的例子
CREATE TABLE triangle (
id int(10) not null primary key auto_increment,
sidea DOUBLE,
sideb DOUBLE,
sidec DOUBLE AS (SQRT(sidea * sidea + sideb * sideb))
);
INSERT INTO triangle (sidea, sideb) VALUES(1,1),(3,4),(6,8),(12,16);
mysql select * from triangle;
+—-+——-+——-+——————–+
| id | sidea | sideb | sidec |
+—-+——-+——-+——————–+
| 1 | 1 | 1 | 1.4142135623730951 |
| 2 | 3 | 4 | 5 |
| 3 | 6 | 8 | 10 |
| 4 | 12 | 16 | 20 |
+—-+——-+——-+——————–+
4 rows in set (0.00 sec)
mysql explain select * from triangle where sidec
+—-+————-+———-+————+——+—————+——+———+——+——+———-+————-+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+—-+————-+———-+————+——+—————+——+———+——+——+———-+————-+
| 1 | SIMPLE | triangle | NULL | ALL | NULL | NULL | NULL | NULL | 4 | 33.33 | Using where |
+—-+————-+———-+————+——+—————+——+———+——+——+———-+————-+
1 row in set, 1 warning (0.00 sec)
mysql alter table triangle add key idx_sidec(sidec);
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql explain select * from triangle where sidec
+—-+————-+———-+————+——-+—————+———–+———+——+——+———-+————-+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+—-+————-+———-+————+——-+—————+———–+———+——+——+———-+————-+
| 1 | SIMPLE | triangle | NULL | range | idx_sidec | idx_sidec | 9 | NULL | 1 | 100.00 | Using where |
+—-+————-+———-+————+——-+—————+———–+———+——+——+———-+————-+
1 row in set, 1 warning (0.00 sec)
看到這個例子, 熟悉 oracle 的朋友可能會和函數索引作比較,兩者比較類似. 使用虛擬列達到函數索引或者解決業務上的設計缺陷,但是個人不建議使用類似的功能,因為虛擬列在一定程度上也會給后期運維帶來潛在的風險和復雜度。網絡上的例子基本都是使用虛擬列解決業務邏輯上的問題,違背了數據庫只存儲數據的初衷,思考一下 MVC 框架的基本邏輯, 業務邏輯要放到 C 層或者 V 層,M 層只存放數據即可。
以上是“MySQL5.7 新特性有哪些”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注丸趣 TV 行業資訊頻道!