未加星标

MySQL 8.0 Labs Descending Indexes in MySQL

字体大小 | |
[数据库(mysql) 所属分类 数据库(mysql) | 发布者 店小二03 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

Starting with the 8.0 optimizer labs release the mysql server now supports descending indexes. As I will detail in this post, this new feature can be used to eliminate the need for sorting results, and lead to performance improvements in a number of queries.

Introduction

Up until this release, all indexes were created in ascending order. While the syntax itself is parsed, the meta data is not preserved. For example in MySQL 5.7:

mysql 5.7> CREATETABLEt1 (a INT, b INT, INDEXa_desc_b_asc (a DESC, b ASC)); QueryOK, 0 rowsaffected (0.47 sec) mysql 5.7> SHOWCREATETABLEt1\G *************************** 1. row *************************** Table: t1 CreateTable: CREATETABLE `t1` ( `a` int(11) DEFAULT NULL, `b` int(11) DEFAULT NULL, KEY `a_desc_b_asc` (`a`,`b`) <-- Orderis not preserved ) ENGINE=InnoDBDEFAULT CHARSET=latin1 1 rowin set (0.00 sec)

While it should be noted that the MySQL 5.7 optimizer is able to scan an ascending index backwards (to give descending order), it comes at a higher cost. As shown further down, we can see forward index scans are ~15% better than backward index scans.

The primary limitation of not being able to support descending indexes is that the optimizer must resort to a filesort for a mixed order such as ORDER BY a DESC, b ASC .

Improvements in MySQL 8.0

With the introduction of descending indexes, InnoDB can now store entries in descending order and and the optimizer will take advantage of it when descending order is requested in the query. Repeating the above example, we can see that the index order information is correctly retained when creating a table:

mysql 8.0> CREATETABLEt1 (a INT, b INT, INDEXa_desc_b_asc (a DESC, b ASC)); QueryOK, 0 rowsaffected (0.47 sec) mysql 8.0> showcreatetablet1; +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | CreateTable | +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------+ | t1 | CREATETABLE `t1` ( `a` int(11) DEFAULT NULL, `b` int(11) DEFAULT NULL, KEY `a_desc_b_asc` (`a` DESC,`b`) ) ENGINE=InnoDBDEFAULT CHARSET=latin1 | +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 rowin set (0.00 sec)

The output of EXPLAIN has also been improved to differentiate between backward and forward index scans. In case of MySQL-5.7, we use backward index scans or filesort for all queries except Query 2 and Query 6 shown below as both these queries require only ascending order.

Query 1: SELECT * FROM t1 ORDER BY a DESC;

mysql 8.0> explainSELECT * FROMt1ORDERBY a DESC; +----+-------------+-------+------------+-------+---------------+--------------+---------+------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+-------+---------------+--------------+---------+------+------+----------+-------------+ | 1 | SIMPLE | t1 | NULL | index | NULL | a_desc_b_asc | 10 | NULL | 10 | 100.00 | Usingindex | +----+-------------+-------+------------+-------+---------------+--------------+---------+------+------+----------+-------------+ 1 rowin set, 1 warning (0.00 sec)

Query 2: SELECT * FROM t1 ORDER BY a ASC;

mysql 8.0> explainSELECT * FROMt1ORDERBY a ASC; +----+-------------+-------+------------+-------+---------------+--------------+---------+------+------+----------+----------------------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+-------+---------------+--------------+---------+------+------+----------+----------------------------------+ | 1 | SIMPLE | t1 | NULL | index | NULL | a_desc_b_asc | 10 | NULL | 10 | 100.00 | Backwardindexscan; Usingindex | +----+-------------+-------+------------+-------+---------------+--------------+---------+------+------+----------+----------------------------------+ 1 rowin set, 1 warning (0.00 sec)

Query 3: SELECT * FROM t1 ORDER BY a DESC, b ASC;

mysql 8.0> EXPLAINSELECT * FROMt1ORDERBY a DESC, b ASC; +----+-------------+-------+------------+-------+---------------+--------------+---------+------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+-------+---------------+--------------+---------+------+------+----------+-------------+ | 1 | SIMPLE | t1 | NULL | index | NULL | a_desc_b_asc | 10 | NULL | 10 | 100.00 | Usingindex | +----+-------------+-------+------------+-------+---------------+--------------+---------+------+------+----------+-------------+ 1 rowin set, 1 warning (0.00 sec)

Query 4: SELECT * FROM t1 ORDER BY a ASC, b DESC;

mysql 8.0> EXPLAINSELECT * FROMt1ORDERBY a ASC, b DESC; +----+-------------+-------+------------+-------+---------------+--------------+---------+------+------+----------+----------------------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+-------+---------------+--------------+---------+------+------+----------+----------------------------------+ | 1 | SIMPLE | t1 | NULL | index | NULL | a_desc_b_asc | 10 | NULL | 10 | 100.00 | Backwardindexscan; Usingindex | +----+-------------+-------+------------+-------+---------------+--------------+---------+------+------+----------+----------------------------------+ 1 rowin set, 1 warning (0.00 sec)

Query 5: SELECT * FROM t1 ORDER BY a DESC, b DESC;

mysql 8.0> EXPLAINSELECT * FROMt1ORDERBY a DESC, b DESC; +----+-------------+-------+------------+-------+---------------+--------------+---------+------+------+----------+-----------------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+-------+---------------+--------------+---------+------+------+----------+-----------------------------+ | 1 | SIMPLE | t1 | NULL | index | NULL | a_desc_b_asc | 10 | NULL | 10 | 100.00 | Usingindex; Usingfilesort | +----+-------------+-------+------------+-------+---------------+--------------+---------+------+------+----------+-----------------------------+ 1 rowin set, 1 warning (0.01 sec)

Query 5: SELECT * FROM t1 ORDER BY a ASC, b ASC;

mysql 8.0> EXPLAINSELECT * FROMt1ORDERBY a ASC, b ASC; +----+-------------+-------+------------+-------+---------------+--------------+---------+------+------+----------+-----------------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+-------+---------------+--------------+---------+------+------+----------+-----------------------------+ | 1 | SIMPLE | t1 | NULL | index | NULL | a_desc_b_asc | 10 | NULL | 10 | 100.00 | Usingindex; Usingfilesort | +----+-------------+-------+------------+-------+---------------+--------------+---------+------+------+----------+-----------------------------+ 1 rowin set, 1 warning (0.00 sec) Below are the performance numbers for all of the above 6 queries when table has one index a_desc_b_asc (a DESC, b ASC) . In MySQL-5.7 it is a_asc_b_asc(a ASC, b ASC)

本文数据库(mysql)相关术语:navicat for mysql mysql workbench mysql数据库 mysql 存储过程 mysql安装图解 mysql教程 mysql 管理工具

主题: SQLMySQLInnoDBAU
分页:12
转载请注明
本文标题:MySQL 8.0 Labs Descending Indexes in MySQL
本站链接:http://www.codesec.net/view/482872.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 数据库(mysql) | 评论(0) | 阅读(24)