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

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. Java 锁 概念介绍

    一   Java中的锁是什么? /* * 一 Java锁定义? * 在计算机科学中,锁(lock)或互斥(mutex)是一种同步机制,用于在有许多执行线程的环境中强制对资源的访问限制. * 锁旨在强制 ...

  2. FastDFS文件同步

    FastDFS同步相关文件: a)10.100.66.82_23000.mark 内容如下: binlog_index=0 binlog_offset=1334 need_sync_old=1 syn ...

  3. What Goes Up Must Come Down

    跳转链接 题目描述 给定一个序列, 求出将此序列变换为单调递增.单调递减 或者先增后减 样例1 输入 7 3 1 4 1 5 9 2 输出 3 样例2 输入 9 10 4 6 3 15 9 1 1 1 ...

  4. Kafka中非常值得学习的优秀设计

    一.Kafka基础 消息系统的作用 应该大部份小伙伴都清楚,用机油装箱举个例子 所以消息系统就是如上图我们所说的仓库,能在中间过程作为缓存,并且实现解耦合的作用. 引入一个场景,我们知道中国移动,中国 ...

  5. 【JS】函数提升变量提升以及函数声明和函数表达式的区别

    今天看js的变量提升问题,里面提到了函数提升.然后发现自己之前一直把函数声明和函数表达式弄错,导致函数提升出错 一.变量提升 console.log(a) var a=100 //undefined ...

  6. 编译PHP扩展的方式

    编译的两种方式其实很简单,这里记录只是为了以后遇到这种情况时不加思索地运用上,而不是花费一些时间去回忆. C/C++程序编译有两种方式:动态编译.静态编译.PHP 是使用 C/C++程序开发的一门脚本 ...

  7. Python重载比较运算符

    对象包含的内置方法 class MyObj(object): def __init__(self): self.value = 0 myObj = MyObj() print(dir(myObj)) ...

  8. Shell双重循环、图形排列及九九乘法表

    Shell双重循环.图形排列及九九乘法表 目录 Shell双重循环.图形排列及九九乘法表 一.双重循环 1. 双重循环概述 2. 双重循环结构 二.循环特殊操作 1. exit 2. break 3. ...

  9. Hadoop启动错误——ERROR: Attempting to operate on hdfs namenode as root but there is no HDFS_NAMENODE_USER defined. Aborting operation.

    错误如下所示; [root@localhost sbin]# start-all.sh Starting namenodes on [192.168.71.129] ERROR: Attempting ...

  10. [TJOI2013] 奖学金

    代码: #include<bits/stdc++.h> using namespace std; long long n,c,ff,ans; long long suma[200010], ...