admin 发表于 2023-2-16 18:54:42

mysql 索引type介绍

<div id="article_content" class="article_content clearfix">
      <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
      <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-6e43165c0a.css">
                <div id="content_views" class="htmledit_views">
                  <p><strong>以下全部详细解析explain各个属性含义&#xff1a;</strong></p>
<p>    </p>
<p style="text-align:center;"></p>
<p></p>
<p>    <strong>各属性含义&#xff1a;</strong><br>     <strong>id:</strong> 查询的序列号<br>     <strong>select_type:</strong> 查询的类型&#xff0c;主要是区别普通查询和联合查询、子查询之类的复杂查询</p>
<ul><li><code>SIMPLE</code>&#xff1a;查询中不包含子查询或者<code>UNION</code></li><li>查询中若包含任何复杂的子部分&#xff0c;最外层查询则被标记为&#xff1a;<code>PRIMARY</code></li><li>在<code>SELECT</code>或<code>WHERE</code>列表中包含了子查询&#xff0c;该子查询被标记为&#xff1a;<code>SUBQUERY</code></li></ul>
<p>    <strong>table:</strong> 输出的行所引用的表<br>     <strong>type:</strong> 访问类型<br>     </p>
<p style="text-align:center;"></p>
<p></p>
<p>    <strong>从左至右&#xff0c;性能由差到好</strong></p>
<ol><li>ALL: 扫描全表</li><li>index: 扫描全部索引树</li><li>range: 扫描部分索引&#xff0c;索引范围扫描&#xff0c;对索引的扫描开始于某一点&#xff0c;返回匹配值域的行&#xff0c;常见于between、<、>等的查询</li><li>ref: 使用非唯一索引或非唯一索引前缀进行的查找<br><strong>&#xff08;<code>eq_ref和const的区别&#xff1a;</code>&#xff09;</strong></li><li><code>eq_ref&#xff1a;唯一性索引扫描&#xff0c;对于每个索引键&#xff0c;表中只有一条记录与之匹配。常见于主键或唯一索引扫描</code></li><li><code>const, system: 单表中最多有一个匹配行&#xff0c;查询起来非常迅速&#xff0c;例如根据主键或唯一索引查询。system是const类型的特例&#xff0c;当查询的表只有一行的情况下&#xff0c; 使用system。</code></li><li>NULL: 不用访问表或者索引&#xff0c;直接就能得到结果&#xff0c;如<code>select 1 from test where 1</code></li></ol>
<p>    <strong>possible_keys: </strong>表示查询时可能使用的索引。如果是空的&#xff0c;没有相关的索引。这时要提高性能&#xff0c;可通过检验WHERE子句&#xff0c;看是否引用某些字段&#xff0c;或者检查字段不是适合索引</p>
<p>    <strong>key: </strong>显示MySQL实际决定使用的索引。如果没有索引被选择&#xff0c;是NULL</p>
<p>    <strong>key_len: </strong>使用到索引字段的长度</p>
<p>    注&#xff1a;key_len显示的值为索引字段的最大可能长度&#xff0c;并非实际使用长度&#xff0c;即key_len是根据表定义计算而得&#xff0c;不是通过表内检索出的。</p>
<p>    <strong>ref: </strong>显示哪个字段或常数与key一起被使用</p>
<p>    <strong>rows: </strong>这个数表示mysql要遍历多少数据才能找到&#xff0c;表示MySQL根据表统计信息及索引选用情况&#xff0c;估算的找到所需的记录所需要读取的行数&#xff0c;在innodb上可能是不准确的</p>
<p>    <strong>Extra: </strong>执行情况的说明和描述。包含不适合在其他列中显示但十分重要的额外信息。</p>
<ol><li>
<ol><li>Using index&#xff1a;表示使用索引&#xff0c;如果只有 Using index&#xff0c;说明他没有查询到数据表&#xff0c;只用索引表就完成了这个查询&#xff0c;这个叫覆盖索引。</li><li>Using where&#xff1a;表示条件查询&#xff0c;如果不读取表的所有数据&#xff0c;或不是仅仅通过索引就可以获取所有需要的数据&#xff0c;则会出现 Using where。</li></ol></li></ol>
                </div>
      </div>
      <div id="treeSkill"></div>
页: [1]
查看完整版本: mysql 索引type介绍