Descending indexing and loose index scan

降序索引和减轻索引扫描

Comments to my previous posts, especially this oneby Gokhan inspired me to write a bit about descending indexes and about loose index scan, or what Gokhan calls “better range” support. None of these are actially related to Innodb tables in general - these are features MySQL should get for all storage engines at some point.

在我以前文章的评论中,尤其是 Gokhan 在这个中提到的,激发了我想写点关于降序索引和减轻索引扫描的东西,或者是 Gokhan 所谓的“改善的范围”支持。通常这些特性跟 Innodb 表都没有内在联系,它们将来在某些时候都能支持各种存储引擎。

Descending indexes - This is something MySQL does not have at this point, but it was not where for so long at large extent because it is less problem than many people think. First - if index is ascending it does not mean it can’t be scanned in reverse order and it will well be. This is how MySQL will optimize indexed ORDER BY col DESC queries for example. Reverse scan could be as fast as forward scan - this is however where storage engines and operating systems come in play. For example certain operation systems might not do backward read-ahead which may slow it down a bit. Or some storage engines, such as MyISAM (for packed indexes) may have reverse scan being much slower than forward scan.

降序索引 -- 现在 MySQL 还不支持这个功能,不过这比很多人想的那样,问题少多了。首先,如果索引是顺序的并不意味着它不能倒序扫描,实际上它表现得挺好的。这就是为什么 MySQL 能优化例如 ORDER BY col DESC 查询的索引。倒序扫描能和正序扫描一样快,不过这些是由存储引擎和操作系统来处理的。例如有些操作系统无法倒序读,这对速度有所降低。或者某些存储引擎,如 MyISAM (它压缩了索引) 倒序扫描时比正序扫描来的慢。

So when do you really need Descending indexes ? Most typical case is when you want to order by two colums in different directions: … ORDER BY price ASC, date DESC LIMIT 10If you have indexed on (price,date) in ascending order you will not be able to optimize this query well - external sort (”filesort”) will be needed. If you would be able to build index on price ASC, date DESC the same query could retrive data in aready sorted order.

那么什么时候才真的需要倒序索引呢?很多典型的情况是当你想要对两个字段作不同方向的排序时:… ORDER BY price ASC, date DESC LIMIT 10。如果已经有了对 (price,date) 的正序索引,则不能较好地优化这个查询 -- 需要用到外部排序(“filesort”)。如果能建立 price ASC, date DESC 的索引,那么这个查询就能按照已经排好的顺序取出数据了。

This is however something you can workaround by having something like “reverse_date” column and using it for sort. With MySQL 5.0 you even can use triggers to update it as real date updates so it becomes less ugly. In fact this is for example why you would see “reverse_timestamp” field in Wikipedia table structure.

然而,常见的变通办法是创建一个“倒序数据”字段,并且利用它来排序。在 MySQL 5.0 中你甚至可以使用触发器来更新真实的数据使得更合适。这就是为什在 Wikipedia 的表结构中有一个 “reverse_timestamp” 字段的缘故。

Loose index scan- Number of years ago when I just started using MySQL I thought it would have any optimization which could come to my mind. For example if I would have (A>0 and B>6)clause and index (A,B) I expected it would start looking at all values where A>0 instantly jumping to onces have B>6 by using index. It is possibe. So I was shocked and upset to find out it did not. And this optimization is still not implemented. This is very important item to remember when you designing your new applications or porting ones from other databases. Designing the indexes for MySQL you should only make sure queries use “=” for all keyparts in the last of index. Only last one is allowed to have “range” clauses, such as >, IN etc. All clauses which follow the range in the index will not use index for their operation.

减少索引扫描 -- 多年前当我刚开始使用 MySQL 时,我想它也许有些优化方法能让我记住。例如如果有一个 (A>0 and B>6) 分句和索引 (A,B),我期望能使用索引来查找所有 A>0 的值,并且能立刻跳转到 B>6 的记录上,我想这是可行的。不过令我郁闷的是竟然不支持,并且这种优化方法还未实现。在设计新的应用程序或者移植数据库时,记住这个特点很重要。设计 MySQL 索引时只需设计保证能让索引最后的所有索引部分都使用 “=” 查询。只有最后一个索引部分才支持 “range” 分句、IN 等。所有在范围索引后面的分句都不会使用到索引。

Let me give one more example KEY (A,B,C) A=5 and B>6 and C=7 In this case index will be used to check A=5 and B>6 cause. C=7 will not be using the index (rows with all C values will be retrieved from the index) and if this is not the index covered query you might rather shorten your key to KEY(A,B) to keep index smaller.

举几个例子吧,索引 (A,B,CP) 和 A=5 and B>6 and C=7分句的情况下,索引会检索 A=5 和 B>6 的条件,C=7 则不会用到索引(所有包含 C 的记录都会从索引中检索得到)。这个时候如果任何查询都无需使用完整的索引的话,就可以缩短索引为 KEY(A,B),这样能让索引变小。

The good news are Loose index scan implementation is finally on a way. In fact it was even implemented in MySQL 5.0, but only for very narrow case of aggregate queries.

一个好消息是,减少索引扫描终究会以某种方式实现。MySQL 5.0 中其实已经实现了,不过只适用于少数情况的聚合查询。

In general complete loose index scan implementation is one of my most wanted features for MySQL optimizer.

P.S If you post queries in your comments please also explain which indexes do you have on the table. SHOW CREATE TABLE is the best. Otherwise I can get you wrong.

常规意义上的完全减少索引扫描是我最想要实现的MySQL优化器特性。顺便提一下,如果你在我的帖子评论中贴上了查询语句,请顺便说明你的索引情况,最好是贴上 SHOW CREATE TABLE 的结果。

mysql reverse 索引_降序索引和减轻索引扫描相关推荐

  1. mysql按升序创建索引_MySQL 降序索引

    MySQL 降序索引 简介:在本教程中,您将了解MySQL降序索引以及如何利用它来提高查询性能. MySQL降序索引简介 降序索引是以降序存储键值的索引.在MySQL 8.0之前,您可以DESC在索引 ...

  2. 两千字揭密 MySQL 8.0.19 三大索引新功能:隐藏索引,降序索引,函数索引

    导读:本文详细介绍 MySQL 8.0.19 三大索引新功能,隐藏索引,降序索引,函数索引,结合其他同仁的技术应用案例,进一步进行验证改编,最后总结心得,希望对大家有帮助. MySQL 8.0 版本带 ...

  3. PHP中 如何将二位数组按某一个或多个字段值(升序/降序)排序?数字索引被重置,关联索引保持不变...

    如何将二位数组按某一个或多个字段值(升序/降序)排序?数字索引被重置,关联索引保持不变 1.$arr=array( 2. array('id'=>1,'name'=>'will','age ...

  4. android升序降序按钮,创建一个按钮,将排序MYSQL查询升序和降序

    我是一名学生编码器. 我想创建一个排序按钮,当按下时,按升序排序MYSQL查询.然后再次按下时,它将按降序排列.对,现在,它只是显示升序和降序表背靠背.创建一个按钮,将排序MYSQL查询升序和降序 形 ...

  5. mysql索引升序降序失效原因_关于联合索引的升序降序和order by关系以及失效问题...

    mysql 文档中对于索引定义中,ASC 和 DESC 的描述 MySQL < 8.0 A key_part specification can end with ASC or DESC. Th ...

  6. oracle用升序索引去降序查询,Oracle工作札记

    Oracle工作笔记 oracle_hint_使用说明示例 1. /*+ALL_ROWS*/ 表明对语句块选择基于开销的优化方法,并获得最佳吞吐量,使资源消耗最小化. 例如: SELECT /*+AL ...

  7. mysql创建非聚集索引_一文看懂聚集索引和非聚集索引的区别

    一.深入浅出理解索引结构 实际上,可以把索引理解为一种特殊的目录.微软的SQL SERVER提供了两种索引:聚集索引(clustered index,也称聚类索引.簇集索引)和非聚集索引(nonclu ...

  8. mysql分表后怎么索引_分库分表后的索引问题

    摘要 最近遇到一个慢sql,在排查过程中发现和分库分表后的索引设置有关系,总结了下问题. 问题 在进行应用健康度盘点时,发现有个慢sql 如下 select brandgoodid from bran ...

  9. mongo备份索引_【MongoDB学习之四】索引 聚合 备份与恢复 监控

    环境 MongoDB 4.0 CentOS 6.5_x64 一.索引 语法 ensureIndex()方法基本语法格式如下所示: >db.COLLECTION_NAME.ensureIndex( ...

最新文章

  1. kotlin设置CORS跨域资源共享,java设置允许跨域,服务端如何设置 springboot中设置跨域资源共享
  2. python编程难吗-Python、C+这些编程语言难吗?十岁女孩告诉你答案
  3. (转) oc static extern 和const
  4. Java配置多数据源access,java联接MS ACCESS,无需配置数据源
  5. 成功解决CondaError: Error reading file, file should be a text file containing packages conda create --he
  6. Spring Boot详细学习地址转载
  7. [转]浅析Tomcat、JBOSS、WebSphere、WebLogic、Apache
  8. es6 类的私有属性_JavaScript ES6类中的私有属性
  9. $.ajax的标准写法
  10. SpringBoot整合redisson分布式锁
  11. markdown的第一次使用
  12. nyoj 谁是最好的Coder
  13. 洛谷——P1739 表达式括号匹配
  14. python从入门到精通-python从入门到精通视频(大全60集)
  15. enctype=multipart/form-data 文件上传
  16. BZOJ 1717: [Usaco2006 Dec]Milk Patterns 产奶的模式( 二分答案 + 后缀数组 )
  17. 为你的Intel(R) HD Graphics 显卡安装适合Premiere的驱动并解决“无法为此计算机验证正在安装的驱动程序“问题
  18. 2022年微信权重号养号方法
  19. windows7无声音,提示扬声器、耳机未插入的解决办法!电脑右下角喇叭显示未插入扬声器或耳机怎么解决!
  20. JavaFX中嵌入谷歌Chromium内核

热门文章

  1. Pandas常见的数据过滤方法、通过列条件筛选行数据
  2. Android通过Chrome Inspect调试WebView
  3. 胡想——对机器人控制体系的一些想法
  4. 破译“生命天书”20年
  5. 图数据库Titan安装与部署
  6. 十、分享一道LeetCode较为简单的单链表题,但是却能激发起练习算法的极大的兴趣
  7. Linux 问题解决 :/lib/systemd/systemd-journald 占用内存过高
  8. python创建一个简单的服务
  9. mysql数据库会同时执行sql吗_mysql 一次执行多条sql语句
  10. 频率分布直方图组距如何确定_QC七大手法之直方图法,快快转发、收藏!