mysql8.0新特性

一、 数据字典(Data dictionary)
1)合并了存储数据库对象信息的事务性数据字典;之前版本是存储于元数据文件和非事务表中 ;
二、原子数据定义语句(原子DDL)(Atomic Data Definition Statements (Atomic DDL))
三、安全性和账户管理(Security and account management)
1)A new caching_sha2_password authentication plugin is available(一个新的cachingsha2password认证插件是可用的)
2)MySQL now supports roles, which are named collections of privileges(MySQL支持角色,它们被命名为特权集合)
3)MySQL now maintains information about password history, enabling restrictions on reuse of previous passwords(MySQL现在维护关于密码历史的信息,允许对以前密码的重用进行限制)
4)MySQL now supports FIPS mode(MySQL现在支持FIPS模式)
四、资源管理(Resource management)
1)可以创建和管理资源组,并将服务器运行的线程分配给特定的组,让线程按照组可用的资源执行
五,InnoDB增强(InnoDB enhancements.)
1)自动增加计数写到redo日志,可让服务器重启时持久
2)当索引数破坏时,InnoDB会在redo 日志写入损坏标志
3)InnoDB memcached插件支持多个get操作(在单个memcached查询中取回多个键/值对)和范围查询
4)一个新的动态配置选项,可以控制InnoDB 死锁检测,可以通过它innodb_deadlock_detect禁用死锁检测;并依赖于 innodb_lock_wait_timeout 设置超时事务回滚,避免影响其他事务正常运行
5)INFORMATION_SCHEMA.INNODB_CACHED_INDEXES保存每一个索引的InnoDB缓冲池中缓存的索引页的数量
6)所有的InnoDB临时表都共享同一个临时表空间 ibtmp1.
7)InnoDB表空间加密特性支持重做日志和撤销日志数据的加密
8)InnoDB支持 NOWAIT and SKIP LOCKED 因为SHARE 或者UPDATE而锁定的读语句;NOWAIT表示遇到锁定立即返回;SKIP LOCKED表示条多锁定的行返回结果;
9)增加分区、删除分区,合并分区,重构分区,重建分区
10)mysql 存储引擎使用数据字典,而不是存储引擎自己的数据字典
11)mysql 数据字典和系统表创建在mysql 数据目录下的单个的InnoDB表空间文件mysql.ibd
六、字符集支持(Character set support)
1)The default character set has changed from latin1 to utf8mb4;默认字符集utf8mb4;
–UTF-8 编码
– 从MySQL 8 开始,数据库的缺省编码将改为utf8mb4,这个编码包含了所有emoji 字符。多少年来我们使用MySQL 都要在编码方面小心翼翼,
– 生怕忘了将缺省的latin 改掉而出现乱码问题。从此以后就不用担心了。
七、增加JSON (json enhancements)AND OpenGIS spatial types
1)mysql提供了很多json相关的函数和API接口
八、优化程序(Optimizer.)
1)MySQL now supports invisible indexes,支持隐藏索引,这个可以用来测试创建的索引是否具有优化作用
2)MySQL now supports descending indexes,增加降序索引,可以加快排序相关查询
/隐藏索引/
– 隐藏一个索引的语法是:
alter table100000003 alter indexix_10000003_1 invisible;
explain
select *from 100000003 whereT001=‘001’;
– 恢复显示该索引的语法是:
alter table100000003alter indexix_10000003_1 visible;
explain
select *from 100000003 whereT001=‘001’;
九、(Common table expressions)
with
r1 as (
select T001 from100000004 where T006=‘001001’
),
r2 as (
select T001 from100000004 where T006=‘001001’
)
select *from r1
union all
select *from r2
十、窗口函数(Window functions)
/开窗函数/
select T002,T007,count(T002) over(partition by T007) from 100000004 where T006=‘001001’;
select T002,rank()over(partition by T007 order by T002) as rank from100000004 where T006=‘001001’;
十一、正则表达式(Regular expression support)
十二、内部临时表(Internal temporary tables)
1)The TempTable storage engine replaces the MEMORY storage engine as the default engine for in-memory internal temporary tables;
十三、日志(logging)
1)可以通过log_error_services设置启用哪一些日志日志组件;错误日志被重写
十四、备份锁(Backup lock)
一种新型的备份锁在在线备份期间允许DML,同时防止可能导致不一致快照的操作
十五、在线参数更改

– MySQL 的设置可以在运行时通过SET GLOBAL 命令来更改,但是这种更改只会临时生效,到下次启动时数据库又会从配置文件中读取。
– MySQL 8 新增了SET PERSIST 命令,例如:
SET PERSIST max_connections = 500;
– MySQL 会将该命令的配置保存到数据目录下的mysqld-auto.cnf 文件中,下次启动时会读取该文件,用其中的配置来覆盖缺省的配置文件。

十六、降序索引
MySQL 8.0终于支持降序索引了。其实,从语法上,MySQL 4就支持了,但正如官方文档所言,“they are parsed but ignored”,实际创建的还是升序索引。

无图无真相,同一个建表语句,看看MySQL 5.7和8.0的区别。
create table slowtech.t1(c1 int,c2 int,index idx_c1_c2(c1,c2 desc));

MySQL 5.7

mysql> show create table slowtech.t1\G
*************************** 1. row ***************************
Table: t1
Create Table: CREATE TABLE t1 (
c1 int(11) DEFAULT NULL,
c2 int(11) DEFAULT NULL,
KEY idx_c1_c2 (c1,c2)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

虽然c2列指定了desc,但在实际的建表语句中还是将其忽略了。再来看看MySQL 8.0的结果。

mysql> show create table slowtech.t1\G
*************************** 1. row ***************************
Table: t1
Create Table: CREATE TABLE t1 (
c1 int(11) DEFAULT NULL,
c2 int(11) DEFAULT NULL,
KEY idx_c1_c2 (c1,c2 DESC)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)
c2列还是保留了desc子句。

降序索引的意义
如果一个查询,需要对多个列进行排序,且顺序要求不一致。在这种场景下,要想避免数据库额外的排序-“filesort”,只能使用降序索引。还是上面这张表,来看看有降序索引和没有的区别。

MySQL 5.7
mysql> explain select * from slowtech.t1 order by c1,c2 desc;
±—±------------±------±-----------±------±--------------±----------±--------±-----±-----±---------±----------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
±—±------------±------±-----------±------±--------------±----------±--------±-----±-----±---------±----------------------------+
| 1 | SIMPLE | t1 | NULL | index | NULL | idx_c1_c2 | 10 | NULL | 1 | 100.00 | Using index; Using filesort |
±—±------------±------±-----------±------±--------------±----------±--------±-----±-----±---------±----------------------------+
1 row in set, 1 warning (0.00 sec)

MySQL 8.0
mysql> explain select * from slowtech.t1 order by c1,c2 desc;
±—±------------±------±-----------±------±--------------±----------±--------±-----±-----±---------±------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
±—±------------±------±-----------±------±--------------±----------±--------±-----±-----±---------±------------+
| 1 | SIMPLE | t1 | NULL | index | NULL | idx_c1_c2 | 10 | NULL | 1 | 100.00 | Using index |
±—±------------±------±-----------±------±--------------±----------±--------±-----±-----±---------±------------+
1 row in set, 1 warning (0.00 sec)

两者的对比可以看出,MySQL 8.0因为降序索引的存在,避免了“filesort”。

这其实是降序索引的主要应用场景。如果只对单个列进行排序,降序索引的意义不是太大,无论是升序还是降序,升序索引完全可以应付。还是同样的表,看看下面的查询。

MySQL 5.7
mysql> explain select * from slowtech.t1 order by c1;
±—±------------±------±-----------±------±--------------±----------±--------±-----±-----±---------±------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
±—±------------±------±-----------±------±--------------±----------±--------±-----±-----±---------±------------+
| 1 | SIMPLE | t1 | NULL | index | NULL | idx_c1_c2 | 10 | NULL | 1 | 100.00 | Using index |
±—±------------±------±-----------±------±--------------±----------±--------±-----±-----±---------±------------+
1 row in set, 1 warning (0.00 sec)

mysql> explain select * from slowtech.t1 order by c1 desc;
±—±------------±------±-----------±------±--------------±----------±--------±-----±-----±---------±------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
±—±------------±------±-----------±------±--------------±----------±--------±-----±-----±---------±------------+
| 1 | SIMPLE | t1 | NULL | index | NULL | idx_c1_c2 | 10 | NULL | 1 | 100.00 | Using index |
±—±------------±------±-----------±------±--------------±----------±--------±-----±-----±---------±------------+
1 row in set, 1 warning (0.00 sec)

虽然c1是升序索引,但在第二个查询中,对其进行降序排列时,并没有进行额外的排序,使用的还是索引。在这里,大家容易产生误区,以为升序索引就不能用于降序排列,实际上,对于索引,MySQL不仅支持正向扫描,还可以反向扫描。反向扫描的性能同样不差。以下是官方对于降序索引的压测结果,测试表也只有两列(a,b),建了一个联合索引(a desc,b asc),感兴趣的童鞋可以看看,http://mysqlserverteam.com/mysql-8-0-labs-descending-indexes-in-mysql/

而在8.0中,对于反向扫描,有一个专门的词进行描述“Backward index scan”。

MySQL 8.0
mysql> explain select * from slowtech.t1 order by c1;
±—±------------±------±-----------±------±--------------±----------±--------±-----±-----±---------±------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
±—±------------±------±-----------±------±--------------±----------±--------±-----±-----±---------±------------+
| 1 | SIMPLE | t1 | NULL | index | NULL | idx_c1_c2 | 10 | NULL | 1 | 100.00 | Using index |
±—±------------±------±-----------±------±--------------±----------±--------±-----±-----±---------±------------+
1 row in set, 1 warning (0.00 sec)

mysql> explain select * from slowtech.t1 order by c1 desc;
±—±------------±------±-----------±------±--------------±----------±--------±-----±-----±---------±---------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
±—±------------±------±-----------±------±--------------±----------±--------±-----±-----±---------±---------------------------------+
| 1 | SIMPLE | t1 | NULL | index | NULL | idx_c1_c2 | 10 | NULL | 1 | 100.00 | Backward index scan; Using index |
±—±------------±------±-----------±------±--------------±----------±--------±-----±-----±---------±---------------------------------+
1 row in set, 1 warning (0.00 sec)

终于不再对group by进行隐式排序

由于降序索引的引入,MySQL 8.0再也不会对group by操作进行隐式排序。
下面看看MySQL 5.7和8中的测试情况
create table slowtech.t1(id int);
insert into slowtech.t1 values(2);
insert into slowtech.t1 values(3);
insert into slowtech.t1 values(1);

MySQL 5.7

mysql> select * from slowtech.t1 group by id;
±-----+
| id |
±-----+
| 1 |
| 2 |
| 3 |
±-----+
3 rows in set (0.00 sec)

mysql> explain select * from slowtech.t1 group by id;
±—±------------±------±-----------±-----±--------------±-----±--------±-----±-----±---------±--------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
±—±------------±------±-----------±-----±--------------±-----±--------±-----±-----±---------±--------------------------------+
| 1 | SIMPLE | t1 | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 100.00 | Using temporary; Using filesort |
±—±------------±------±-----------±-----±--------------±-----±--------±-----±-----±---------±--------------------------------+
1 row in set, 1 warning (0.00 sec)
“Using filesort”,代表查询中有排序操作,从结果上看,id列确实也是升序输出。

MySQL 8.0

mysql> select * from slowtech.t1 group by id;
±-----+
| id |
±-----+
| 2 |
| 3 |
| 1 |
±-----+
3 rows in set (0.00 sec)

mysql> explain select * from slowtech.t1 group by id;
±—±------------±------±-----------±-----±--------------±-----±--------±-----±-----±---------±----------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
±—±------------±------±-----------±-----±--------------±-----±--------±-----±-----±---------±----------------+
| 1 | SIMPLE | t1 | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 100.00 | Using temporary |
±—±------------±------±-----------±-----±--------------±-----±--------±-----±-----±---------±----------------+
1 row in set, 1 warning (0.01 sec)
不仅结果没有升序输出,执行计划中也没有“Using filesort”。

可见,MySQL 8.0对于group by操作确实不再进行隐式排序。

从5.7升级到8.0,依赖group by隐式排序的业务可要小心咯。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值