您好,登錄后才能下訂單哦!
小編給大家分享一下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 < 33;
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 > 10;
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------------+
| 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 > 10;
+----+-------------+----------+------------+-------+---------------+-----------+---------+------+------+----------+-------------+
| 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新特性有哪些”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。