您好,登录后才能下订单哦!
小编给大家分享一下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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。