看了网上很多排名很靠前的博客,发现好多都讲错了!我开始按照博客来,没有怀疑,直到自己试了一下才发现是错的。

file sort在面试中有时候会问到,这个其实挺能区分是不是真的了解order by的执行了。大部分人会以为file sort是文件排

序,其实不要看字面意思,并不是文件排序!只不过是表示排序没有用到索引。其实不自己试验,挺难想到的。

我这里使用mysql5.7试验了几种情况,供大家参考.

首先创建的表字段是 id, username, password, age, gender,其中id是自增的主键索引,(username, password, age)是联合索引

1. 第一种情况:查询语句不带where条件过滤

  1) select * from user_info order by username。使用了file sort,查询的字段不在username联合索引中或者也不是主键id。会产生file sort。

等价于 select gender from user_info order by username,也会产生file sort!那 select id from user_info order by username,会不会产生file sort呢?是不会的,

因为mysql的b+树叶子节点也是存储了主键信息的。

  2)那这样呢 select password from user_info order by username; 这个是不会产生file sort的!因为查询的字段在联合索引中。

   3) 上面两种情况都是order by后面的字段都是符合最左前缀原则,没有索引失效。没有索引失效,是看查的字段是不是在索引中!那索引失效会不会出现file sort呢?

    select username from user_info order by password, username;  //索引失效了

  

   

2. 第二种情况:查询条件带有where条件过滤

    其实带where条件也主要是看order by后面的字段用的索引。如果order by后面的字段不符合最左前缀原则,那么肯定是会产生file sort的。如果order by用到了索引

    则看select 出来的字段是否是在索引页中存储的信息,如果是索引中的信息则不会file sort。(这里有几个特殊情况,是mysql引擎的优化产生的)

    1)select * from user_info where username = '1' order by username;

   这个where条件是username,等值查询。再去order by username。没必要,所以mysql会把order by 去掉。order by都去掉了,肯定不会file sort了。

  

   2)select age from user_info where age = '1' order by username;

   这个是不会的,因为直接根据联合索引去查询 password = '1' 的,取出来的所有数据自然是根据username排序的(索引排好了序)。不需要file sort。

  3) select gender from user_info where age = 1 order by username; 因为gender字段不在order by使用的字段中,所以需要file sort。

4)select password from user_info where password = '10' order by username, age;

  首先看select出来的字段在不在order by所使用的索引中,这个是在的。所以排除第一种情况。继续分析。mysql先去这个索引中找出password 为 '10'的数据行,

极大情况有多条,在索引上取出的数据,看是不是符合先按username排序,再按age排序呢?是的,因为从索引上遍历取password='10'的数据时候,取出来的

  数据天然是按username先排序的(索引特性),password是等值的情况下,再按age排序的。所以取出来的数据已经排序了。

  

  这一块一定要注意啊!!!网上很多说mysql使用file sort看order by字段有没有符合最左匹配,是错的!主要是看mysql在where条件上是否使用

  索引(select (索引中的字段)where 索引中的字段),使用了索引的字段,会using index去查出数据。然后再看是否取出来的数据是否是排好序的。

  插曲,slect age from user_info where age = 1;会使用索引吗?答案是会的,这个不符合最左前缀匹配,但是select出来的字段是在索引中的。

  5)select password from user_info where age = 1 order by password, username;

  这个是会用到索引的,因为select password的字段在索引列中,并且where条件也是age也是在这个索引中的。

  但是在B+树索引上,根据age取出来的数据,看看是不是先password,再username排序呢?显然不是,所以会发生file sort。

  

总结:什么时候会发生file sort呢?

  首先,看这个是不是走索引,是不是在索引上查找数据。如果没有使用索引,那么会file sort。因为没有在索引上取数据,那么取出来的数据就是无序的。

  需要file sort。如果使用了索引。取出来的数据看是否是满足order by后面的排序字段要求,如果满足。则不需要file sort。如果不满足,则需要file sort。

mysql什么时候会发生file sort的更多相关文章

  1. Multiple MySQL running but PID file could not be found

    [root@tao Desktop]# service mysql start Starting MySQL SUCCESS! [root@tao Desktop]# service mysql st ...

  2. ubuntu系统mysql.h no such file or directory

    在Ubuntu系统中,你已经安装了mysql,即你使用sudo apt-get install mysql-server mysql-client然而使用C语言访问mysql数据库时,却发现出现了如下 ...

  3. MySQL报错: Character set ‘utf8mb4‘ is not a compiled character set and is not specified in the ‘/usr/share/mysql/charsets/Index.xml‘ file

    由于日常程序使用了字符集utf8mb4,为了避免每次更新时,set names utf8mb4,就把配置文件改了,如下: [root@~]# vim /etc/my.cnf #my.cnf [clie ...

  4. Linux MYSQL:dead but pid file exists

    MYSQL dead but pid file exists问题 - CSDN博客https://blog.csdn.net/shilian_h/article/details/38020567 Er ...

  5. ubuntu安装了mysql 但是编译报错 mysql.h: No such file or directory

    在Ubuntu体系中,已经安装了mysql,即应用sudo apt-get install mysql-server mysql-client 但是用C编译mysql数据库时,报错fatal erro ...

  6. fatal error: mysql.h: No such file or directory

    在ubuntu系统下安装mysql之后,和数据库连接的时候,出现如下错误:fatal error: mysql.h: No such file or directory 是因为缺少链接库,执行如下命名 ...

  7. Go丨语言package github.com/Go-SQL-Driver/MySQL: exec: "git": executable file not found in %PATH%解决方法

    Go语言在添加第三方MySQL驱动的时候报错: go: missing Git command. See https://golang.org/s/gogetcmd package github.co ...

  8. Centos 7.5源码编译安装zabbix4.0报fatal error: mysql.h: No such file or directory

    系统环境:CentOS 7.5是最小化安装的 编译信息 编译选项: root@Server01 zabbix-]# ./configure --prefix=/usr/share/applicatio ...

  9. Starting MySQL ** mysqld_safe Directory '/var/lib/mysql' for UNIX socket file don't exists

    本地虚拟机(CentOS6.8)启动MySQL(MySQL5.6.35)服务失败 [root@VMUest ~]# service mysql status ERROR! MySQL is not r ...

随机推荐

  1. 检测当前编译器支持c++版本

    if (__cplusplus > 201703L) std::cout << "C++2a\n"; else if (__cplusplus == 201703 ...

  2. 【建议收藏】Redis超详细入门教程大杂烩

    写在前边 Redis入门的整合篇.本篇也算是把2021年redis留下来的坑填上去,重新整合了一翻,点击这里,回顾我的2020与2021~一名大二后台练习生 NoSQL NoSQL(NoSQL = N ...

  3. NumPy 基础知识·翻译完成

    原文:Numpy Essentials 协议:CC BY-NC-SA 4.0 欢迎任何人参与和完善:一个人可以走的很快,但是一群人却可以走的更远. 在线阅读 ApacheCN 面试求职交流群 7241 ...

  4. 一个label 混搭不同颜色,不同字体的文字.. by 徐

    效果如图箭头所示,只需要一个label就可以做到不同颜色或不同字体的效果 1 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, ...

  5. Redis常用数据类型以及操作

    Redis常用数据类型以及操作 目录 Redis常用数据类型以及操作 一.String数据类型 1. SET/GET/APPEND/STRLEN 2. INCR/DECR/INCRBY/DECRBY ...

  6. 范数||x||(norm)笔记

    1. 范数的含义和定义 范数是具有"长度"概念的函数.在线性代数.泛函分析及相关领域,是一个函数,它为向量空间内的所有向量赋予非零的正的长度或大小.另一方面,半范数可以为非零的向量 ...

  7. Exception in thread "main" java.lang.UnsupportedClassVersionError: org/apache/zeppelin/server/ZeppelinServer : Unsupported major.minor version 52.0

    在启动Zeppelin时遇到了该问题: [root@quickstart bin]# ./zeppelin-daemon.sh restart Please specify HADOOP_CONF_D ...

  8. 帆软报表(finereport) 组合地图 保持系列名和值居中

    自定义JavaScript代码,使用HTML解析 function(){ var name = this.name; var total = '<div style="width:10 ...

  9. Spring系列14:IoC容器的扩展点

    Spring系列14:IoC容器的扩展点 回顾 知识需要成体系地学习,本系列文章前后有关联,建议按照顺序阅读.上一篇我们详细介绍了Spring Bean的生命周期和丰富的扩展点,没有阅读的强烈建议先阅 ...

  10. Solution -「NOI 2018」「洛谷 P4768」归程

    \(\mathcal{Description}\)   Link.   给定一个 \(n\) 个点 \(m\) 条边的无向连通图,边形如 \((u,v,l,a)\).每次询问给出 \(u,p\),回答 ...