mysql索引

<div class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; font-family: 'Helvetica Neue', Helvetica, 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;"><h3 id="h" style="line-height: inherit; margin: 1.5em 0px; font-weight: bold; font-size: 1.3em; margin-bottom: 2em; margin-right: 5px; padding: 8px 15px; letter-spacing: 2px; background-image: linear-gradient(to right bottom, rgb(0, 188, 212), rgb(63, 81, 181)); background-color: rgb(63, 81, 181); color: rgb(255, 255, 255); border-left: 10px solid rgb(51, 51, 51); border-radius: 5px; text-shadow: rgb(102, 102, 102) 1px 1px 1px; box-shadow: rgb(102, 102, 102) 1px 1px 2px;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">索引与外键</span></h3>
<blockquote style="line-height: inherit; display: block; padding: 15px 15px 15px 1rem; font-size: 0.9em; margin: 1em 0px; color: rgb(129, 145, 152); border-left: 6px solid rgb(220, 230, 240); background: rgb(242, 247, 251); overflow: auto; overflow-wrap: normal; word-break: normal;">
  <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 0px;">普通索引:</p>
</blockquote>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">这是最基本的索引,它没有任何索引,只是为了加快查询速度。</p>
<blockquote style="line-height: inherit; display: block; padding: 15px 15px 15px 1rem; font-size: 0.9em; margin: 1em 0px; color: rgb(129, 145, 152); border-left: 6px solid rgb(220, 230, 240); background: rgb(242, 247, 251); overflow: auto; overflow-wrap: normal; word-break: normal;">
  <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 0px;">唯一索引</p>
</blockquote>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">它与前面的普通索引类似,不同的就是:索引列的值必须唯一,但允许有空值。如果是组合索引,则列值的组合必须唯一。</p>
<blockquote style="line-height: inherit; display: block; padding: 15px 15px 15px 1rem; font-size: 0.9em; margin: 1em 0px; color: rgb(129, 145, 152); border-left: 6px solid rgb(220, 230, 240); background: rgb(242, 247, 251); overflow: auto; overflow-wrap: normal; word-break: normal;">
  <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 0px;">主键索引</p>
</blockquote>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">它是一种特殊的唯一索引,不允许有空值。一般是在建表的时候同时创建主键索引</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">要求:<br>    <code style="font-size: inherit; line-height: inherit; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: rgb(233, 105, 0); background: rgb(248, 248, 248);">一张表只允许一个主键</code><br>    <code style="font-size: inherit; line-height: inherit; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: rgb(233, 105, 0); background: rgb(248, 248, 248);">主键要求自增</code></p>
<blockquote style="line-height: inherit; display: block; padding: 15px 15px 15px 1rem; font-size: 0.9em; margin: 1em 0px; color: rgb(129, 145, 152); border-left: 6px solid rgb(220, 230, 240); background: rgb(242, 247, 251); overflow: auto; overflow-wrap: normal; word-break: normal;">
  <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 0px;">组合索引</p>
</blockquote>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">多个字段建立索引。</p>
<blockquote style="line-height: inherit; display: block; padding: 15px 15px 15px 1rem; font-size: 0.9em; margin: 1em 0px; color: rgb(129, 145, 152); border-left: 6px solid rgb(220, 230, 240); background: rgb(242, 247, 251); overflow: auto; overflow-wrap: normal; word-break: normal;">
  <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 0px;">全文索引</p>
</blockquote>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">fulltext<br>MyISAM引擎支持。</p>
<blockquote style="line-height: inherit; display: block; padding: 15px 15px 15px 1rem; font-size: 0.9em; margin: 1em 0px; color: rgb(129, 145, 152); border-left: 6px solid rgb(220, 230, 240); background: rgb(242, 247, 251); overflow: auto; overflow-wrap: normal; word-break: normal;">
  <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 0px;">外键</p>
</blockquote>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">注意:<br>1、引擎必须一样。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">2、字段类型必须一样</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">3、长度必须一样</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">4、存储范围也必须一样</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">5、约束字段必须在被引用的字段中存在。</p>
<blockquote style="line-height: inherit; display: block; padding: 15px 15px 15px 1rem; font-size: 0.9em; margin: 1em 0px; color: rgb(129, 145, 152); border-left: 6px solid rgb(220, 230, 240); background: rgb(242, 247, 251); overflow: auto; overflow-wrap: normal; word-break: normal;">
  <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 0px;">索引的不足之处</p>
</blockquote>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">上面都在说使用索引的好处,但过多的使用索引将会造成滥用。因此索引也会有它的缺点:</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">◆虽然索引大大提高了查询速度,同时却会降低更新表的速度,如对表进行INSERT、UPDATE和DELETE。因为更新表时,MySQL不仅要保存数据,还要保存一下索引文件。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">◆建立索引会占用磁盘空间的索引文件。一般情况这个问题不太严重,但如果你在一个大表上创建了多种组合索引,索引文件的会膨胀很快。<br>索引只是提高效率的一个因素,如果你的MySQL有大数据量的表,就需要花时间研究建立最优秀的索引,或优化查询语句。</p>
<blockquote style="line-height: inherit; display: block; padding: 15px 15px 15px 1rem; font-size: 0.9em; margin: 1em 0px; color: rgb(129, 145, 152); border-left: 6px solid rgb(220, 230, 240); background: rgb(242, 247, 251); overflow: auto; overflow-wrap: normal; word-break: normal;">
  <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 0px;">使用索引的注意事项</p>
</blockquote>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">使用索引时,有以下一些技巧和注意事项:</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">◆索引不会包含有NULL值的列</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">只要列中包含有NULL值都将不会被包含在索引中,复合索引中只要有一列含有NULL值,那么这一列对于此复合索引就是无效的。所以我们在数据库设计时不要让字段的默认值为NULL。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">◆使用短索引</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">对串列进行索引,如果可能应该指定一个前缀长度。例如,如果有一个CHAR(255)的列,如果在前10个或20个字符内,多数值是惟一的,那么就不要对整个列进行索引。短索引不仅可以提高查询速度而且可以节省磁盘空间和I/O操作。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">◆索引列排序</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">MySQL查询只使用一个索引,因此如果where子句中已经使用了索引的话,那么order by中的列是不会使用索引的。因此数据库默认排序可以符合要求的情况下不要使用排序操作;尽量不要包含多个列的排序,如果需要最好给这些列创建复合索引。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">◆like语句操作</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">一般情况下不鼓励使用like操作,如果非使用不可,如何使用也是一个问题。like “%aaa%” 不会使用索引而like “aaa%”可以使用索引。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">◆不要在列上进行运算</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">1.select * from users where YEAR(adddate)&lt;2007; <br>将在每个行上进行运算,这将导致索引失效而进行全表扫描,因此我们可以改成<br>1.select * from users where adddate&lt;‘2007-01-01’;  </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">◆不使用NOT IN和&lt;&gt;操作</p>
<h3 id="hmysql" style="line-height: inherit; margin: 1.5em 0px; font-weight: bold; font-size: 1.3em; margin-bottom: 2em; margin-right: 5px; padding: 8px 15px; letter-spacing: 2px; background-image: linear-gradient(to right bottom, rgb(0, 188, 212), rgb(63, 81, 181)); background-color: rgb(63, 81, 181); color: rgb(255, 255, 255); border-left: 10px solid rgb(51, 51, 51); border-radius: 5px; text-shadow: rgb(102, 102, 102) 1px 1px 1px; box-shadow: rgb(102, 102, 102) 1px 1px 2px;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">深入了解mysql索引与优化</span></h3>
<h5 id="h-1" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px; font-weight: bold; font-size: 1em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">索引目的</span></h5>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">索引的目的在于提高查询效率,可以类比字典,如果要查“mysql”这个单词,我们肯定需要定位到m字母,然后从下往下找到y字母,再找到剩下的sql。如果没有索引,那么你可能需要把所有单词看一遍才能找到你想要的,如果我想找到m开头的单词呢?或者L开头的单词呢?是不是觉得如果没有索引,这个事情根本无法完成?</p>
<h5 id="h-2" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px; font-weight: bold; font-size: 1em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">索引原理</span></h5>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">除了词典,生活中随处可见索引的例子,如火车站的车次表、图书的目录等。它们的原理都是一样的,<code style="font-size: inherit; line-height: inherit; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: rgb(233, 105, 0); background: rgb(248, 248, 248);">通过不断的缩小想要获得数据的范围来筛选出最终想要的结果,同时把随机的事件变成顺序的事件,也就是我们总是通过同一种查找方式来锁定数据。</code></p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">数据库也是一样,但显然要复杂许多,因为不仅面临着等值查询,还有范围查询<code style="font-size: inherit; line-height: inherit; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: rgb(233, 105, 0); background: rgb(248, 248, 248);">(&gt;、&lt;、between、in)</code>、模糊查询<code style="font-size: inherit; line-height: inherit; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: rgb(233, 105, 0); background: rgb(248, 248, 248);">(like)</code>、并集查询<code style="font-size: inherit; line-height: inherit; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: rgb(233, 105, 0); background: rgb(248, 248, 248);">(or)</code>等等。数据库应该选择怎么样的方式来应对所有的问题呢?我们回想字典的例子,能不能把数据分成段,然后分段查询呢?最简单的如果1000条数据,1到100分成第一段,101到200分成第二段,201到300分成第三段……这样查第250条数据,只要找第三段就可以了,一下子去除了90%的无效数据。但如果是1千万的记录呢,分成几段比较好?稍有算法基础的同学会想到搜索树,其平均复杂度是lgN,具有不错的查询性能。但这里我们忽略了一个关键的问题,复杂度模型是基于每次相同的操作成本来考虑的,数据库实现比较复杂,数据保存在磁盘上,而为了提高性能,每次又可以把部分数据读入内存来计算,因为我们知道访问磁盘的成本大概是访问内存的十万倍左右,所以简单的搜索树难以满足复杂的应用场景。</p>
<h5 id="hio" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px; font-weight: bold; font-size: 1em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">磁盘IO与预读</span></h5>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">前面提到了访问磁盘,那么这里先简单介绍一下磁盘IO和预读,磁盘读取数据靠的是机械运动,每次读取数据花费的时间可以分为寻道时间、旋转延迟、传输时间三个部分,寻道时间指的是磁臂移动到指定磁道所需要的时间,主流磁盘一般在5ms以下;旋转延迟就是我们经常听说的磁盘转速,比如一个磁盘7200转,表示每分钟能转7200次,也就是说1秒钟能转120次,旋转延迟就是1/120/2 = 4.17ms;传输时间指的是从磁盘读出或将数据写入磁盘的时间,一般在零点几毫秒,相对于前两个时间可以忽略不计。那么访问一次磁盘的时间,即一次磁盘IO的时间约等于5+4.17 = 9ms左右,听起来还挺不错的,但要知道一台500 -MIPS的机器每秒可以执行5亿条指令,因为指令依靠的是电的性质,换句话说执行一次IO的时间可以执行40万条指令,数据库动辄十万百万乃至千万级数据,每次9毫秒的时间,显然是个灾难。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">考虑到磁盘IO是非常高昂的操作,计算机操作系统做了一些优化,当一次IO时,不光把当前磁盘地址的数据,而是把相邻的数据也都读取到内存缓冲区内,因为局部预读性原理告诉我们,当计算机访问一个地址的数据的时候,与其相邻的数据也会很快被访问到。每一次IO读取的数据我们称之为一页(page)。具体一页有多大数据跟操作系统有关,一般为4k或8k,也就是我们读取一页内的数据时候,实际上才发生了一次IO,这个理论对于索引的数据结构设计非常有帮助。</p>
<h5 id="hmysql-1" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px; font-weight: bold; font-size: 1em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">MySQL索引原理及慢查询优化</span></h5>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">MySQL凭借着出色的性能、低廉的成本、丰富的资源,已经成为绝大多数互联网公司的首选关系型数据库。虽然性能出色,但所谓“好马配好鞍”,如何能够更好的使用它,已经成为开发工程师的必修课,我们经常会从职位描述上看到诸如<code style="font-size: inherit; line-height: inherit; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: rgb(233, 105, 0); background: rgb(248, 248, 248);">“精通MySQL”、“SQL语句优化”、“了解数据库原理”</code>等要求。我们知道一般的应用系统,<code style="font-size: inherit; line-height: inherit; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: rgb(233, 105, 0); background: rgb(248, 248, 248);">读写比例在10:1左右</code>,而且插入操作和一般的更新操作很少出现性能问题,遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,所以<code style="font-size: inherit; line-height: inherit; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: rgb(233, 105, 0); background: rgb(248, 248, 248);">查询语句的优化显然是重中之重</code>。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">多数情况下,我们知道索引能够提高查询效率,但应该如何建立索引?索引的顺序如何?许多人却只知道大概。其实理解这些概念并不难,而且索引的原理远没有想象的那么复杂。</p>
<blockquote style="line-height: inherit; display: block; padding: 15px 15px 15px 1rem; font-size: 0.9em; margin: 1em 0px; color: rgb(129, 145, 152); border-left: 6px solid rgb(220, 230, 240); background: rgb(242, 247, 251); overflow: auto; overflow-wrap: normal; word-break: normal;">
  <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 0px;">MySQL索引原理</p>
</blockquote>
<h5 id="h-3" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px; font-weight: bold; font-size: 1em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">索引的数据结构</span></h5>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">前面讲了生活中索引的例子,索引的基本原理,数据库的复杂性,又讲了操作系统的相关知识,目的就是让大家了解,任何一种数据结构都不是凭空产生的,一定会有它的背景和使用场景,我们现在总结一下,我们需要这种数据结构能够做些什么,其实很简单,那就是:<code style="font-size: inherit; line-height: inherit; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: rgb(233, 105, 0); background: rgb(248, 248, 248);">每次查找数据时把磁盘IO次数控制在一个很小的数量级,最好是常数数量级。</code>那么我们就想到如果一个高度可控的多路搜索树是否能满足需求呢?就这样,<code style="font-size: inherit; line-height: inherit; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: rgb(233, 105, 0); background: rgb(248, 248, 248);">b+树</code>应运而生。</p>
<blockquote style="line-height: inherit; display: block; padding: 15px 15px 15px 1rem; font-size: 0.9em; margin: 1em 0px; color: rgb(129, 145, 152); border-left: 6px solid rgb(220, 230, 240); background: rgb(242, 247, 251); overflow: auto; overflow-wrap: normal; word-break: normal;">
  <p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 0px;">慢查询优化</p>
</blockquote>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">关于MySQL索引原理是比较枯燥的东西,大家只需要有一个感性的认识,并不需要理解得非常透彻和深入。我们回头来看看一开始我们说的慢查询,了解完索引原理之后,大家是不是有什么想法呢?先总结一下索引的几大基本原则<br>建索引的几大原则</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">1.最左前缀匹配原则,非常重要的原则,mysql会一直向右匹配直到遇到范围查询(&gt;、&lt;、between、like)就停止匹配,比如a = 1 and b = 2 and c &gt; 3 and d = 4 如果建立(a,b,c,d)顺序的索引,d是用不到索引的,如果建立(a,b,d,c)的索引则都可以用到,a,b,d的顺序可以任意调整。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">2.=和in可以乱序,比如a = 1 and b = 2 and c = 3 建立(a,b,c)索引可以任意顺序,mysql的查询优化器会帮你优化成索引可以识别的形式。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">3.尽量选择区分度高的列作为索引,区分度的公式是count(distinct col)/count(*),表示字段不重复的比例,比例越大我们扫描的记录数越少,唯一键的区分度是1,而一些状态、性别字段可能在大数据面前区分度就是0,那可能有人会问,这个比例有什么经验值吗?使用场景不同,这个值也很难确定,一般需要join的字段我们都要求是0.1以上,即平均1条扫描10条记录</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">4.索引列不能参与计算,保持列“干净”,比如from_unixtime(create_time) = ’2014-05-29’就不能使用到索引,原因很简单,b+树中存的都是数据表中的字段值,但进行检索时,需要把所有元素都应用函数才能比较,显然成本太大。所以语句应该写成create_time = unix_timestamp(’2014-05-29’);<br>5.尽量的扩展索引,不要新建索引。比如表中已经有a的索引,现在要加(a,b)的索引,那么只需要修改原来的索引即可</p>
<h3 id="hexplain" style="line-height: inherit; margin: 1.5em 0px; font-weight: bold; font-size: 1.3em; margin-bottom: 2em; margin-right: 5px; padding: 8px 15px; letter-spacing: 2px; background-image: linear-gradient(to right bottom, rgb(0, 188, 212), rgb(63, 81, 181)); background-color: rgb(63, 81, 181); color: rgb(255, 255, 255); border-left: 10px solid rgb(51, 51, 51); border-radius: 5px; text-shadow: rgb(102, 102, 102) 1px 1px 1px; box-shadow: rgb(102, 102, 102) 1px 1px 2px;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">查询优化神器 – explain命令</span></h3>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">关于explain命令相信大家并不陌生,具体用法和字段含义可以参考官网HYPERLINK<br>"http://dev.mysql.com/doc/refman/5.5/en/explain-output.html"explain-output,这里需要强调rows是核心指标,绝大部分rows小的语句执行一定很快(有例外,下面会讲到)。所以优化语句基本上都是在优化rows。</p>
<h5 id="h-4" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px; font-weight: bold; font-size: 1em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">慢查询优化基本步骤</span></h5>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">0.先运行看看是否真的很慢,注意设置SQL_NO_CACHE</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">1.where条件单表查,锁定最小返回记录表。这句话的意思是把查询语句的where都应用到表中返回的记录数最小的表开始查起,单表每个字段分别查询,看哪个字段的区分度最高</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">2.explain查看执行计划,是否与1预期一致(从锁定记录较少的表开始查询)</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">3.order by limit 形式的sql语句让排序的表优先查</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">4.了解业务方使用场景</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">5.加索引时参照建索引的几大原则</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">6.观察结果,不符合预期继续从0分析<br>几个慢查询案例<br>下面几个例子详细解释了如何分析和优化慢查询<br>复杂语句写法<br>很多情况下,我们写SQL只是为了实现功能,这只是第一步,不同的语句书写方式对于效率往往有本质的差别,这要求我们对mysql的执行计划和索引原则有非常清楚的认识,请看下面的语句</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">MySQL</p>
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="hljs sql" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; padding: 0.5em; background: rgb(40, 40, 40); color: rgb(235, 219, 178); display: block !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important;"><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 1</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">select</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 2</span>&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">distinct</span>&nbsp;cert.emp_id&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 3</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">from</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 4</span>&nbsp;&nbsp;&nbsp;cm_log&nbsp;cl&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 5</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">inner</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">join</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 6</span>&nbsp;&nbsp;&nbsp;(<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 7</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">select</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 8</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;emp.id&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">as</span>&nbsp;emp_id,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 9</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;emp_cert.id&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">as</span>&nbsp;cert_id&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">10</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">from</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">11</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;employee&nbsp;emp&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">12</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">left</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">join</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">13</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;emp_certificate&nbsp;emp_cert&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">14</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">on</span>&nbsp;emp.id&nbsp;=&nbsp;emp_cert.emp_id&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">15</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">where</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">16</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;emp.is_deleted=<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">0</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">17</span>&nbsp;&nbsp;&nbsp;)&nbsp;cert&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">18</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">on</span>&nbsp;(<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">19</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cl.ref_table=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 187, 38); word-wrap: inherit !important; word-break: inherit !important;">'Employee'</span>&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">20</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">and</span>&nbsp;cl.ref_oid=&nbsp;cert.emp_id<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">21</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">22</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">or</span>&nbsp;(<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">23</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cl.ref_table=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 187, 38); word-wrap: inherit !important; word-break: inherit !important;">'EmpCertificate'</span>&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">24</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">and</span>&nbsp;cl.ref_oid=&nbsp;cert.cert_id<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">25</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">26</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">where</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">27</span>&nbsp;&nbsp;&nbsp;cl.last_upd_date&nbsp;&gt;=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 187, 38); word-wrap: inherit !important; word-break: inherit !important;">'2013-11-07&nbsp;15:03:00'</span>&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">28</span>&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">and</span>&nbsp;cl.last_upd_date&lt;=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 187, 38); word-wrap: inherit !important; word-break: inherit !important;">'2013-11-08&nbsp;16:00:00'</span>;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">29</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">30</span>&nbsp;&nbsp;&nbsp;53&nbsp;rows&nbsp;in&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">set</span>&nbsp;(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">1.87</span>&nbsp;sec)<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">先运行一下,53条记录 1.87秒,又没有用聚合语句,比较慢</p>
<h5 id="hexplain-1" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px; font-weight: bold; font-size: 1em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">explain</span></h5>
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="hljs ruby" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; padding: 0.5em; background: rgb(40, 40, 40); color: rgb(235, 219, 178); display: block !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important;"><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">1</span>+----+-------------+------------+-------+---------------------------------+-----------------------+---------+-------------------+-------+--------------------------------+<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">2</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;id&nbsp;|</span>&nbsp;select_type&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;table&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;type&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;possible_keys&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;key&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;key_len&nbsp;|</span>&nbsp;ref&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;rows&nbsp;&nbsp;|</span>&nbsp;Extra&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">3</span>+----+-------------+------------+-------+---------------------------------+-----------------------+---------+-------------------+-------+--------------------------------+<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">4</span>|</span>&nbsp;&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">1</span>&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;PRIMARY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;cl&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;range&nbsp;|</span>&nbsp;cm_log_cls_id,idx_last_upd_date&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;idx_last_upd_date&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">8</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;&nbsp;&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">379</span>&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;Using&nbsp;where;&nbsp;Using&nbsp;temporary&nbsp;&nbsp;&nbsp;|</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">5</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;&nbsp;1&nbsp;|</span>&nbsp;PRIMARY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;&amp;lt;derived2&amp;gt;&nbsp;|</span>&nbsp;ALL&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;NULL&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;63727&nbsp;|</span>&nbsp;Using&nbsp;where;&nbsp;Using&nbsp;join&nbsp;buffer&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">6</span>|</span>&nbsp;&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">2</span>&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;DERIVED&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;emp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;ALL&nbsp;&nbsp;&nbsp;|</span>&nbsp;NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;NULL&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">13317</span>&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;Using&nbsp;where&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">7</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;&nbsp;2&nbsp;|</span>&nbsp;DERIVED&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;emp_cert&nbsp;&nbsp;&nbsp;|</span>&nbsp;ref&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;emp_certificate_empid&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;emp_certificate_empid&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;meituanorg.emp.id&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;|</span>&nbsp;Using&nbsp;index&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">8</span>+----+-------------+------------+-------+---------------------------------+-----------------------+---------+-------------------+-------+--------------------------------+<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">9</span></span><br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">简述一下执行计划,首先mysql根据idx_last_upd_date索引扫描cm_log表获得379条记录;然后查表扫描了63727条记录,分为两部分,derived表示构造表,也就是不存在的表,可以简单理解成是一个语句形成的结果集,后面的数字表示语句的ID。derived2表示的是ID = 2的查询构造了虚拟表,并且返回了63727条记录。我们再来看看ID = 2的语句究竟做了写什么返回了这么大量的数据,首先全表扫描employee表13317条记录,然后根据索引emp_certificate_empid关联emp_certificate表,rows = 1表示,每个关联都只锁定了一条记录,效率比较高。获得后,再和cm_log的379条记录根据规则关联。从执行过程上可以看出返回了太多的数据,返回的数据绝大部分cm_log都用不到,因为cm_log只锁定了379条记录。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">如何优化呢?可以看到我们在运行完后还是要和cm_log做join,那么我们能不能之前和cm_log做join呢?仔细分析语句不难发现,其基本思想是如果cm_log的ref_table是EmpCertificate就关联emp_certificate表,如果ref_table是Employee就关联employee表,我们完全可以拆成两部分,并用union连接起来,注意这里用union,而不用union all是因为原语句有“distinct”来得到唯一的记录,而union恰好具备了这种功能。如果原语句中没有distinct不需要去重,我们就可以直接使用union all了,因为使用union需要去重的动作,会影响SQL性能。<br>优化过的语句如下</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">MySQL</p>
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="hljs sql" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; padding: 0.5em; background: rgb(40, 40, 40); color: rgb(235, 219, 178); display: block !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important;"><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 1</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">select</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 2</span>&nbsp;&nbsp;&nbsp;emp.id&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 3</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">from</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 4</span>&nbsp;&nbsp;&nbsp;cm_log&nbsp;cl&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 5</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">inner</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">join</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 6</span>&nbsp;&nbsp;&nbsp;employee&nbsp;emp&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 7</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">on</span>&nbsp;cl.ref_table&nbsp;=&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 187, 38); word-wrap: inherit !important; word-break: inherit !important;">'Employee'</span>&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 8</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">and</span>&nbsp;cl.ref_oid&nbsp;=&nbsp;emp.id&nbsp;&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 9</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">where</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">10</span>&nbsp;&nbsp;&nbsp;cl.last_upd_date&nbsp;&gt;=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 187, 38); word-wrap: inherit !important; word-break: inherit !important;">'2013-11-07&nbsp;15:03:00'</span>&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">11</span>&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">and</span>&nbsp;cl.last_upd_date&lt;=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 187, 38); word-wrap: inherit !important; word-break: inherit !important;">'2013-11-08&nbsp;16:00:00'</span>&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">12</span>&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">and</span>&nbsp;emp.is_deleted&nbsp;=&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">0</span>&nbsp;&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">13</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">union</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">14</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">select</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">15</span>&nbsp;&nbsp;&nbsp;emp.id&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">16</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">from</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">17</span>&nbsp;&nbsp;&nbsp;cm_log&nbsp;cl&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">18</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">inner</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">join</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">19</span>&nbsp;&nbsp;&nbsp;emp_certificate&nbsp;ec&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">20</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">on</span>&nbsp;cl.ref_table&nbsp;=&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 187, 38); word-wrap: inherit !important; word-break: inherit !important;">'EmpCertificate'</span>&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">21</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">and</span>&nbsp;cl.ref_oid&nbsp;=&nbsp;ec.id&nbsp;&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">22</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">inner</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">join</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">23</span>&nbsp;&nbsp;&nbsp;employee&nbsp;emp&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">24</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">on</span>&nbsp;emp.id&nbsp;=&nbsp;ec.emp_id&nbsp;&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">25</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">where</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">26</span>&nbsp;&nbsp;&nbsp;cl.last_upd_date&nbsp;&gt;=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 187, 38); word-wrap: inherit !important; word-break: inherit !important;">'2013-11-07&nbsp;15:03:00'</span>&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">27</span>&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">and</span>&nbsp;cl.last_upd_date&lt;=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(184, 187, 38); word-wrap: inherit !important; word-break: inherit !important;">'2013-11-08&nbsp;16:00:00'</span>&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">28</span>&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">and</span>&nbsp;emp.is_deleted&nbsp;=&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">0</span><br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">不需要了解业务场景,只需要改造的语句和改造之前的语句保持结果一致</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">现有索引可以满足,不需要建索引</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">用改造后的语句实验一下,只需要10ms 降低了近200倍!</p>
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="hljs ruby" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; padding: 0.5em; background: rgb(40, 40, 40); color: rgb(235, 219, 178); display: block !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important;"><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 1</span>+----+--------------+------------+--------+---------------------------------+-------------------+---------+-----------------------+------+-------------+<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 2</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;id&nbsp;|</span>&nbsp;select_type&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;table&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;type&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;possible_keys&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;key&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;key_len&nbsp;|</span>&nbsp;ref&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;rows&nbsp;|</span>&nbsp;Extra&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 3</span>+----+--------------+------------+--------+---------------------------------+-------------------+---------+-----------------------+------+-------------+<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 4</span>|</span>&nbsp;&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">1</span>&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;PRIMARY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;cl&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;range&nbsp;&nbsp;|</span>&nbsp;cm_log_cls_id,idx_last_upd_date&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;idx_last_upd_date&nbsp;|</span>&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">8</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">379</span>&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;Using&nbsp;where&nbsp;|</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 5</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;&nbsp;1&nbsp;|</span>&nbsp;PRIMARY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;emp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;eq_ref&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;PRIMARY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;PRIMARY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;meituanorg.cl.ref_oid&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;|</span>&nbsp;Using&nbsp;where&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 6</span>|</span>&nbsp;&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">2</span>&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;UNION&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;cl&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;range&nbsp;&nbsp;|</span>&nbsp;cm_log_cls_id,idx_last_upd_date&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;idx_last_upd_date&nbsp;|</span>&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">8</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">379</span>&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;Using&nbsp;where&nbsp;|</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 7</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;&nbsp;2&nbsp;|</span>&nbsp;UNION&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;ec&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;eq_ref&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;PRIMARY,emp_certificate_empid&nbsp;&nbsp;&nbsp;|</span>&nbsp;PRIMARY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;meituanorg.cl.ref_oid&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;|</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 8</span>|</span>&nbsp;&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">2</span>&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;UNION&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;emp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;eq_ref&nbsp;|</span>&nbsp;PRIMARY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;PRIMARY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">4</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;meituanorg.ec.emp_id&nbsp;&nbsp;|</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">1</span>&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;Using&nbsp;where&nbsp;|</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 9</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;NULL&nbsp;|</span>&nbsp;UNION&nbsp;RESULT&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;&amp;lt;union1,2&amp;gt;&nbsp;|</span>&nbsp;ALL&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;NULL&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;NULL&nbsp;|</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">10</span>+----+--------------+------------+--------+---------------------------------+-------------------+---------+-----------------------+------+-------------+<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">11</span></span><br></code></pre>
<h5 id="h-5" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px; font-weight: bold; font-size: 1em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">明确应用场景</span></h5>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">举这个例子的目的在于颠覆我们对列的区分度的认知,一般上我们认为区分度越高的列,越容易锁定更少的记录,但在一些特殊的情况下,这种理论是有局限性的</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">MySQL</p>
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="hljs sql" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; padding: 0.5em; background: rgb(40, 40, 40); color: rgb(235, 219, 178); display: block !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important;"><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 1</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">select</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 2</span>&nbsp;&nbsp;&nbsp;*&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 3</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">from</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 4</span>&nbsp;&nbsp;&nbsp;stage_poi&nbsp;sp&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 5</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">where</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 6</span>&nbsp;&nbsp;&nbsp;sp.accurate_result=<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">1</span>&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 7</span>&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">and</span>&nbsp;(<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 8</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sp.sync_status=<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">0</span>&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 9</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">or</span>&nbsp;sp.sync_status=<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">2</span>&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">10</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">or</span>&nbsp;sp.sync_status=<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">4</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">11</span>&nbsp;&nbsp;&nbsp;);<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">12</span>&nbsp;&nbsp;&nbsp;951&nbsp;rows&nbsp;in&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">set</span>&nbsp;(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">6.22</span>&nbsp;sec)<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">先看看运行多长时间,951条数据6.22秒,真的很慢</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">先explain,rows达到了361万,type = ALL表明是全表扫描</p>
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="hljs ruby" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; padding: 0.5em; background: rgb(40, 40, 40); color: rgb(235, 219, 178); display: block !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important;"><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">1</span>+----+-------------+-------+------+---------------+------+---------+------+---------+-------------+<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">2</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;id&nbsp;|</span>&nbsp;select_type&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;table&nbsp;|</span>&nbsp;type&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;possible_keys&nbsp;|</span>&nbsp;key&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;key_len&nbsp;|</span>&nbsp;ref&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;rows&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;Extra&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">3</span>+----+-------------+-------+------+---------------+------+---------+------+---------+-------------+<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">4</span>|</span>&nbsp;&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">1</span>&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;SIMPLE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;sp&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;ALL&nbsp;&nbsp;|</span>&nbsp;NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;NULL&nbsp;|</span>&nbsp;NULL&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;NULL&nbsp;|</span>&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">3613155</span>&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;Using&nbsp;where&nbsp;|</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">5</span>+----+-------------+-------+------+---------------+------+---------+------+---------+-------------+<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">所有字段都应用查询返回记录数,因为是单表查询 0已经做过了951条</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">让explain的rows 尽量逼近951<br>看一下accurate_result = 1的记录数</p>
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="hljs ruby" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; padding: 0.5em; background: rgb(40, 40, 40); color: rgb(235, 219, 178); display: block !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important;"><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">1</span>select&nbsp;count(*),accurate_result&nbsp;from&nbsp;stage_poi&nbsp;&nbsp;group&nbsp;by&nbsp;accurate_result;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">2</span>+----------+-----------------+<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">3</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;count(*)&nbsp;|</span>&nbsp;accurate_result&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">4</span>+----------+-----------------+<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">5</span>|</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">1023</span>&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-1&nbsp;|</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">6</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;&nbsp;2114655&nbsp;|</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">0</span>&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">7</span>|</span>&nbsp;&nbsp;&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">972815</span>&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;|</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">8</span>+----------+-----------------+<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">我们看到accurate_result这个字段的区分度非常低,整个表只有-1,0,1三个值,加上索引也无法锁定特别少量的数据<br>再看一下sync_status字段的情况</p>
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="hljs cs" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; padding: 0.5em; background: rgb(40, 40, 40); color: rgb(235, 219, 178); display: block !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important;"><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">1</span><span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(142, 192, 124); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(142, 192, 124); word-wrap: inherit !important; word-break: inherit !important;">select</span>&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(131, 165, 152); word-wrap: inherit !important; word-break: inherit !important;">count</span>(<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">*</span>),sync_status&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(142, 192, 124); word-wrap: inherit !important; word-break: inherit !important;">from</span>&nbsp;stage_poi&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(142, 192, 124); word-wrap: inherit !important; word-break: inherit !important;">group</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(142, 192, 124); word-wrap: inherit !important; word-break: inherit !important;">by</span>&nbsp;sync_status</span>;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">2</span>+----------+-------------+<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">3</span>|&nbsp;count(*)&nbsp;|&nbsp;sync_status&nbsp;|<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">4</span>+----------+-------------+<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">5</span>|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">3080</span>&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">0</span>&nbsp;|<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">6</span>|&nbsp;&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">3085413</span>&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">3</span>&nbsp;|<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">7</span>+----------+-------------+<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">同样的区分度也很低,根据理论,也不适合建立索引<br>问题分析到这,好像得出了这个表无法优化的结论,两个列的区分度都很低,即便加上索引也只能适应这种情况,很难做普遍性的优化,比如当sync_status 0、3分布的很平均,那么锁定记录也是百万级别的</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">找业务方去沟通,看看使用场景。业务方是这么来使用这个SQL语句的,每隔五分钟会扫描符合条件的数据,处理完成后把sync_status这个字段变成1,五分钟符合条件的记录数并不会太多,1000个左右。了解了业务方的使用场景后,优化这个SQL就变得简单了,因为业务方保证了数据的不平衡,如果加上索引可以过滤掉绝大部分不需要的数据</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">根据建立索引规则,使用如下语句建立索引</p>
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="hljs sql" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; padding: 0.5em; background: rgb(40, 40, 40); color: rgb(235, 219, 178); display: block !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important;"><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">1</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">alter</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">table</span>&nbsp;stage_poi&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">add</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">index</span>&nbsp;idx_acc_status(accurate_result,sync_status);<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">2</span>952&nbsp;rows&nbsp;in&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">set</span>&nbsp;(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">0.20</span>&nbsp;sec)<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">观察预期结果,发现只需要200ms,快了30多倍。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">我们再来回顾一下分析问题的过程,单表查询相对来说比较好优化,大部分时候只需要把where条件里面的字段依照规则加上索引就好,如果只是这种“无脑”优化的话,显然一些区分度非常低的列,不应该加索引的列也会被加上索引,这样会对插入、更新性能造成严重的影响,同时也有可能影响其它的查询语句。所以我们第4步调差SQL的使用场景非常关键,我们只有知道这个业务场景,才能更好地辅助我们更好的分析和优化查询语句。</p>
<h5 id="h-6" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px; font-weight: bold; font-size: 1em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">无法优化的语句</span></h5>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">MySQL</p>
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="hljs sql" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; padding: 0.5em; background: rgb(40, 40, 40); color: rgb(235, 219, 178); display: block !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important;"><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 1</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">select</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 2</span>&nbsp;&nbsp;&nbsp;c.id,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 3</span>&nbsp;&nbsp;&nbsp;c.name,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 4</span>&nbsp;&nbsp;&nbsp;c.position,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 5</span>&nbsp;&nbsp;&nbsp;c.sex,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 6</span>&nbsp;&nbsp;&nbsp;c.phone,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 7</span>&nbsp;&nbsp;&nbsp;c.office_phone,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 8</span>&nbsp;&nbsp;&nbsp;c.feature_info,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 9</span>&nbsp;&nbsp;&nbsp;c.birthday,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">10</span>&nbsp;&nbsp;&nbsp;c.creator_id,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">11</span>&nbsp;&nbsp;&nbsp;c.is_keyperson,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">12</span>&nbsp;&nbsp;&nbsp;c.giveup_reason,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">13</span>&nbsp;&nbsp;&nbsp;c.status,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">14</span>&nbsp;&nbsp;&nbsp;c.data_source,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">15</span>&nbsp;&nbsp;&nbsp;from_unixtime(c.created_time)&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">as</span>&nbsp;created_time,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">16</span>&nbsp;&nbsp;&nbsp;from_unixtime(c.last_modified)&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">as</span>&nbsp;last_modified,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">17</span>&nbsp;&nbsp;&nbsp;c.last_modified_user_id&nbsp;&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">18</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">from</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">19</span>&nbsp;&nbsp;&nbsp;contact&nbsp;c&nbsp;&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">20</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">inner</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">join</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">21</span>&nbsp;&nbsp;&nbsp;contact_branch&nbsp;cb&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">22</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">on</span>&nbsp;&nbsp;c.id&nbsp;=&nbsp;cb.contact_id&nbsp;&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">23</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">inner</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">join</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">24</span>&nbsp;&nbsp;&nbsp;branch_user&nbsp;bu&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">25</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">on</span>&nbsp;&nbsp;cb.branch_id&nbsp;=&nbsp;bu.branch_id&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">26</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">and</span>&nbsp;bu.status&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">in</span>&nbsp;(<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">27</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">1</span>,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">28</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">2</span>)&nbsp;&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">29</span>&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">inner</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">join</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">30</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;org_emp_info&nbsp;oei&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">31</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">on</span>&nbsp;&nbsp;oei.data_id&nbsp;=&nbsp;bu.user_id&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">32</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">and</span>&nbsp;oei.node_left&nbsp;&gt;=&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">2875</span>&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">33</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">and</span>&nbsp;oei.node_right&nbsp;&lt;=&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">10802</span>&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">34</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">and</span>&nbsp;oei.org_category&nbsp;=&nbsp;-&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">1</span>&nbsp;&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">35</span>&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">order</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">by</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">36</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c.created_time&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">desc</span>&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">limit</span>&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">0</span>&nbsp;,<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">10</span>;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">37</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;10&nbsp;rows&nbsp;in&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">set</span>&nbsp;(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">13.06</span>&nbsp;sec)<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">还是几个步骤<br>先看语句运行多长时间,10条记录用了13秒,已经不可忍受</p>
<h5 id="hexplain-2" style="color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px; font-weight: bold; font-size: 1em;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">explain</span></h5>
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="hljs ruby" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; padding: 0.5em; background: rgb(40, 40, 40); color: rgb(235, 219, 178); display: block !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important;"><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">1</span>+----+-------------+-------+--------+-------------------------------------+-------------------------+---------+--------------------------+------+----------------------------------------------+<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">2</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;id&nbsp;|</span>&nbsp;select_type&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;table&nbsp;|</span>&nbsp;type&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;possible_keys&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;key&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;key_len&nbsp;|</span>&nbsp;ref&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;rows&nbsp;|</span>&nbsp;Extra&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">3</span>+----+-------------+-------+--------+-------------------------------------+-------------------------+---------+--------------------------+------+----------------------------------------------+<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">4</span>|</span>&nbsp;&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">1</span>&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;SIMPLE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;oei&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;ref&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;idx_category_left_right,idx_data_id&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;idx_category_left_right&nbsp;|</span>&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">5</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;const&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">8849</span>&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;Using&nbsp;where;&nbsp;Using&nbsp;temporary;&nbsp;Using&nbsp;filesort&nbsp;|</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">5</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;&nbsp;1&nbsp;|</span>&nbsp;SIMPLE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;bu&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;ref&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;PRIMARY,idx_userid_status&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;idx_userid_status&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;meituancrm.oei.data_id&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;&nbsp;&nbsp;76&nbsp;|</span>&nbsp;Using&nbsp;where;&nbsp;Using&nbsp;index&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">6</span>|</span>&nbsp;&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">1</span>&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;SIMPLE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;cb&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;ref&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;idx_branch_id,idx_contact_branch_id&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;idx_branch_id&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">4</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;meituancrm.bu.branch_id&nbsp;&nbsp;|</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">1</span>&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">7</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;&nbsp;1&nbsp;|</span>&nbsp;SIMPLE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;c&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;eq_ref&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;PRIMARY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;PRIMARY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;108&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|</span>&nbsp;meituancrm.cb.contact_id&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;|</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(250, 189, 47); word-wrap: inherit !important; word-break: inherit !important;">|<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">8</span>+----+-------------+-------+--------+-------------------------------------+-------------------------+---------+--------------------------+------+----------------------------------------------+<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">9</span></span><br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">从执行计划上看,mysql先查org_emp_info表扫描8849记录,再用索引idx_userid_status关联branch_user表,再用索引idx_branch_id关联contact_branch表,最后主键关联contact表。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">rows返回的都非常少,看不到有什么异常情况。我们在看一下语句,发现后面有order by + limit组合,会不会是排序量太大搞的?于是我们简化SQL,去掉后面的order by 和 limit,看看到底用了多少记录来排序</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">MySQL</p>
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="hljs sql" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; padding: 0.5em; background: rgb(40, 40, 40); color: rgb(235, 219, 178); display: block !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important;"><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 1</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">select</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 2</span>&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">count</span>(*)<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 3</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">from</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 4</span>&nbsp;&nbsp;&nbsp;contact&nbsp;c&nbsp;&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 5</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">inner</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">join</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 6</span>&nbsp;&nbsp;&nbsp;contact_branch&nbsp;cb&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 7</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">on</span>&nbsp;&nbsp;c.id&nbsp;=&nbsp;cb.contact_id&nbsp;&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 8</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">inner</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">join</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 9</span>&nbsp;&nbsp;&nbsp;branch_user&nbsp;bu&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">10</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">on</span>&nbsp;&nbsp;cb.branch_id&nbsp;=&nbsp;bu.branch_id&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">11</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">and</span>&nbsp;bu.status&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">in</span>&nbsp;(<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">12</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">1</span>,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">13</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">2</span>)&nbsp;&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">14</span>&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">inner</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">join</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">15</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;org_emp_info&nbsp;oei&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">16</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">on</span>&nbsp;&nbsp;oei.data_id&nbsp;=&nbsp;bu.user_id&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">17</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">and</span>&nbsp;oei.node_left&nbsp;&gt;=&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">2875</span>&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">18</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">and</span>&nbsp;oei.node_right&nbsp;&lt;=&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">10802</span>&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">19</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">and</span>&nbsp;oei.org_category&nbsp;=&nbsp;-&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">1</span>&nbsp;&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">20</span>+<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(146, 131, 116); font-style: italic; word-wrap: inherit !important; word-break: inherit !important;">----------+</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">21</span>|&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">count</span>(*)&nbsp;|<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">22</span>+<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(146, 131, 116); font-style: italic; word-wrap: inherit !important; word-break: inherit !important;">----------+</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">23</span>|&nbsp;&nbsp;&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">778878</span>&nbsp;|<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">24</span>+<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(146, 131, 116); font-style: italic; word-wrap: inherit !important; word-break: inherit !important;">----------+</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">25</span><span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">1</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">row</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">in</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">set</span>&nbsp;(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">5.19</span>&nbsp;sec)<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">发现排序之前居然锁定了778878条记录,如果针对70万的结果集排序,将是灾难性的,怪不得这么慢,那我们能不能换个思路,先根据contact的created_time排序,再来join会不会比较快呢?<br>于是改造成下面的语句,也可以用straight_join来优化</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">MySQL</p>
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="hljs sql" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; padding: 0.5em; background: rgb(40, 40, 40); color: rgb(235, 219, 178); display: block !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important;"><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 1</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">select</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 2</span>c.id,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 3</span>c.name,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 4</span>c.position,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 5</span>c.sex,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 6</span>c.phone,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 7</span>c.office_phone,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 8</span>c.feature_info,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 9</span>c.birthday,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">10</span>c.creator_id,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">11</span>c.is_keyperson,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">12</span>c.giveup_reason,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">13</span>c.status,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">14</span>c.data_source,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">15</span>from_unixtime(c.created_time)&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">as</span>&nbsp;created_time,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">16</span>from_unixtime(c.last_modified)&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">as</span>&nbsp;last_modified,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">17</span>c.last_modified_user_id<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">18</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">from</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">19</span>contact&nbsp;c<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">20</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">where</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">21</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">exists</span>&nbsp;(<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">22</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">select</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">23</span><span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">1</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">24</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">from</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">25</span>contact_branch&nbsp;cb<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">26</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">inner</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">join</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">27</span>branch_user&nbsp;bu<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">28</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">on</span>&nbsp;cb.branch_id&nbsp;=&nbsp;bu.branch_id<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">29</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">and</span>&nbsp;bu.status&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">in</span>&nbsp;(<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">30</span><span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">1</span>,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">31</span><span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">2</span>)<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">32</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">inner</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">join</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">33</span>org_emp_info&nbsp;oei<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">34</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">on</span>&nbsp;oei.data_id&nbsp;=&nbsp;bu.user_id<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">35</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">and</span>&nbsp;oei.node_left&nbsp;&gt;=&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">2875</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">36</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">and</span>&nbsp;oei.node_right&nbsp;&lt;=&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">10802</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">37</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">and</span>&nbsp;oei.org_category&nbsp;=&nbsp;-&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">1</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">38</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">where</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">39</span>c.id&nbsp;=&nbsp;cb.contact_id<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">40</span>)<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">41</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">order</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">by</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">42</span>c.created_time&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">desc</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">limit</span>&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">0</span>&nbsp;,<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">10</span>;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">43</span>10&nbsp;rows&nbsp;in&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">set</span>&nbsp;(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">0.00</span>&nbsp;sec)<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">验证一下效果 预计在1ms内,提升了13000多倍!</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">本以为至此大工告成,但我们在前面的分析中漏了一个细节,先排序再join和先join再排序理论上开销是一样的,为何提升这么多是因为有一个limit!大致执行过程是:mysql先按索引排序得到前10条记录,然后再去join过滤,当发现不够10条的时候,再次去10条,再次join,这显然在内层join过滤的数据非常多的时候,将是灾难的,极端情况,内层一条数据都找不到,mysql还傻乎乎的每次取10条,几乎遍历了这个数据表!<br>用不同参数的SQL试验下</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">MySQL</p>
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;"><code class="hljs sql" style="overflow-wrap: break-word; margin: 0px 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; overflow-x: auto; padding: 0.5em; background: rgb(40, 40, 40); color: rgb(235, 219, 178); display: block !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important;"><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 1</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">select</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 2</span>&nbsp;&nbsp;&nbsp;sql_no_cache&nbsp;&nbsp;&nbsp;c.id,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 3</span>&nbsp;&nbsp;&nbsp;c.name,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 4</span>&nbsp;&nbsp;&nbsp;c.position,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 5</span>&nbsp;&nbsp;&nbsp;c.sex,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 6</span>&nbsp;&nbsp;&nbsp;c.phone,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 7</span>&nbsp;&nbsp;&nbsp;c.office_phone,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 8</span>&nbsp;&nbsp;&nbsp;c.feature_info,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;"> 9</span>&nbsp;&nbsp;&nbsp;c.birthday,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">10</span>&nbsp;&nbsp;&nbsp;c.creator_id,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">11</span>&nbsp;&nbsp;&nbsp;c.is_keyperson,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">12</span>&nbsp;&nbsp;&nbsp;c.giveup_reason,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">13</span>&nbsp;&nbsp;&nbsp;c.status,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">14</span>&nbsp;&nbsp;&nbsp;c.data_source,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">15</span>&nbsp;&nbsp;&nbsp;from_unixtime(c.created_time)&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">as</span>&nbsp;created_time,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">16</span>&nbsp;&nbsp;&nbsp;from_unixtime(c.last_modified)&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">as</span>&nbsp;last_modified,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">17</span>&nbsp;&nbsp;&nbsp;c.last_modified_user_id&nbsp;&nbsp;&nbsp;&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">18</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">from</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">19</span>&nbsp;&nbsp;&nbsp;contact&nbsp;c&nbsp;&nbsp;&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">20</span><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">where</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">21</span>&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">exists</span>&nbsp;(<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">22</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">select</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">23</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">1</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">24</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">from</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">25</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;contact_branch&nbsp;cb&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">26</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">inner</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">join</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">27</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;branch_user&nbsp;bu&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">28</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">on</span>&nbsp;&nbsp;cb.branch_id&nbsp;=&nbsp;bu.branch_id&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">29</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">and</span>&nbsp;bu.status&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">in</span>&nbsp;(<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">30</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">1</span>,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">31</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">2</span>)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">32</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">inner</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">join</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">33</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;org_emp_info&nbsp;oei&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">34</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">on</span>&nbsp;&nbsp;oei.data_id&nbsp;=&nbsp;bu.user_id&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">35</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">and</span>&nbsp;oei.node_left&nbsp;&gt;=&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">2875</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">36</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">and</span>&nbsp;oei.node_right&nbsp;&lt;=&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">2875</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">37</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">and</span>&nbsp;oei.org_category&nbsp;=&nbsp;-&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">1</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">38</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">where</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">39</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c.id&nbsp;=&nbsp;cb.contact_id&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">40</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">41</span>&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">order</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">by</span><br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">42</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c.created_time&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">desc</span>&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">limit</span>&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">0</span>&nbsp;,<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">43</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">10</span>;<br><span class="linenum hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); padding-right: 20px; word-spacing: 0px; word-wrap: inherit !important; word-break: inherit !important;">44</span>Empty&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">set</span>&nbsp;(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">2</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(251, 73, 52); word-wrap: inherit !important; word-break: inherit !important;">min</span>&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(211, 134, 155); word-wrap: inherit !important; word-break: inherit !important;">18.99</span>&nbsp;sec)<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">2 min 18.99 sec!比之前的情况还糟糕很多。由于mysql的nested loop机制,遇到这种情况,基本是无法优化的。这条语句最终也只能交给应用系统去优化自己的逻辑了。<br>通过这个例子我们可以看到,并不是所有语句都能优化,而往往我们优化时,由于SQL用例回归时落掉一些极端情况,会造成比原来还严重的后果。所以,第一:不要指望所有语句都能通过SQL优化,第二:不要过于自信,只针对具体case来优化,而忽略了更复杂的情况。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">慢查询的案例就分析到这儿,以上只是一些比较典型的案例。我们在优化过程中遇到过超过1000行,涉及到16个表join的“垃圾SQL”,也遇到过线上线下数据库差异导致应用直接被慢查询拖死,也遇到过varchar等值比较没有写单引号,还遇到过笛卡尔积查询直接把从库搞死。再多的案例其实也只是一些经验的积累,如果我们熟悉查询优化器、索引的内部原理,那么分析这些案例就变得特别简单了。</p>
<h3 id="h-7" style="line-height: inherit; margin: 1.5em 0px; font-weight: bold; font-size: 1.3em; margin-bottom: 2em; margin-right: 5px; padding: 8px 15px; letter-spacing: 2px; background-image: linear-gradient(to right bottom, rgb(0, 188, 212), rgb(63, 81, 181)); background-color: rgb(63, 81, 181); color: rgb(255, 255, 255); border-left: 10px solid rgb(51, 51, 51); border-radius: 5px; text-shadow: rgb(102, 102, 102) 1px 1px 1px; box-shadow: rgb(102, 102, 102) 1px 1px 2px;"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;">写在后面的话</span></h3>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0px; margin: 1.5em 0px;">本文以一个慢查询案例引入了MySQL索引原理、优化慢查询的一些方法论;并针对遇到的典型案例做了详细的分析。其实做了这么长时间的语句优化后才发现,<code style="font-size: inherit; line-height: inherit; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: rgb(233, 105, 0); background: rgb(248, 248, 248);">任何数据库层面的优化都抵不上应用系统的优化</code>,同样是MySQL,可以用来支撑Google/FaceBook/Taobao应用,但可能连你的个人网站都撑不住。套用最近比较流行的话:<code style="font-size: inherit; line-height: inherit; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: rgb(233, 105, 0); background: rgb(248, 248, 248);">查询容易,优化不易,且写且珍惜!</code></p></div>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值