MySQL索引优化:分页和join连接优化
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这篇<span style="color: black;">文案</span>是MySQL索引优化的第三部分。前两篇<span style="color: black;">文案</span><span style="color: black;">能够</span>看:</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">怎样</span>优化mysql索引-最左前缀原则案例详解</a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">怎样</span>优化 MySQL 索引:<span style="color: black;">加强</span>排序性能</a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这篇<span style="color: black;">文案</span>的<span style="color: black;">重点</span>主题是关于优化 MySQL 中的分页和连接。</span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">分页优化</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">分页<span style="color: black;">查找</span><span style="color: black;">指的是</span>从一个大的结果集中只返回一部分数据,常用于网站的数据展示、浏览等场景。在MySQL中,LIMIT关键字用于实现分页<span style="color: black;">查找</span>,其语法如下:</span></p><span style="color: black;">SELECT</span> * <span style="color: black;">FROM</span> table_name <span style="color: black;">LIMIT</span> <span style="color: black;">offset</span> , <span style="color: black;">row_count</span>;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">其中</span><span style="color: black;"><span style="color: black;">offset</span></span><span style="color: black;">指定<span style="color: black;">查找</span>结果的<span style="color: black;">初始</span>位置,并</span><span style="color: black;"><span style="color: black;">row_count</span></span><span style="color: black;">指定<span style="color: black;">查找</span>结果的条数。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">要<span style="color: black;">查找</span>表的前10条记录,<span style="color: black;">能够</span><span style="color: black;">运用</span>以下SQL语句:</span></p><span style="color: black;">SELECT</span> * <span style="color: black;">FROM</span>table_name<span style="color: black;">LIMIT</span> <span style="color: black;">0</span> , <span style="color: black;">10</span> ;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">要<span style="color: black;">查找</span>接下来的10条记录,<span style="color: black;">能够</span><span style="color: black;">运用</span>以下SQL语句:</span></p><span style="color: black;">SELECT</span> * <span style="color: black;">FROM</span> table_name <span style="color: black;">LIMIT</span> <span style="color: black;">10</span> , <span style="color: black;">10</span> ;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">然则</span>,在处理大型数据集时,分页可能会<span style="color: black;">作为</span>性能瓶颈,尤其是在<span style="color: black;">运用</span> OFFSET 子句时,它<span style="color: black;">必须</span>在返回指定的记录子集之前扫描<span style="color: black;">全部</span>数据集。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">看下面这个例子</p><span style="color: black;">explain</span> <span style="color: black;">select</span> * <span style="color: black;">from</span> Students <span style="color: black;">order</span> <span style="color: black;">by</span> <span style="color: black;">name</span> <span style="color: black;">limit</span> <span style="color: black;">100000</span>,<span style="color: black;">10</span>;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">该SQL语句看似从表中检索10条记录,但<span style="color: black;">实质</span>上它从表中检索100010条记录,并丢弃前100000条记录以<span style="color: black;">得到</span>所需的10条记录。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">因此呢</span>,当进一步深入结果集时,该<span style="color: black;">查找</span>的性能会下降。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">SQL<span style="color: black;">能够</span>优化如下:</span></p><span style="color: black;">explain</span> <span style="color: black;">select</span> * <span style="color: black;">from</span> Students t1 <span style="color: black;">join</span>
(<span style="color: black;">select</span> <span style="color: black;">id</span> <span style="color: black;">from</span> Students <span style="color: black;">order</span> <span style="color: black;">by</span> <span style="color: black;">`name`</span> <span style="color: black;">limit</span> <span style="color: black;">100000</span>, <span style="color: black;">10</span>) t2
<span style="color: black;">on</span> t1.id = t2.id;<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-axegupay5k/157f07dd008a47b2bac5bead0d9b46ba~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723347266&x-signature=XvqBtN%2FSHSKXa05dAAcis5RbkV4%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">优化思路:<span style="color: black;">首要</span><span style="color: black;">运用</span>覆盖索引的<span style="color: black;">办法</span><span style="color: black;">查找</span>10条数据,<span style="color: black;">而后</span>用这10条数据进行连接<span style="color: black;">查找</span>。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">这般</span><span style="color: black;">查找</span>的字段就完全被索引覆盖了</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">覆盖索引说明</span></strong><span style="color: black;">:基于MySQL数据结构,主键索引(InnoDB引擎)存储完整的记录,而辅助索引仅存储主键。MySQL 节点<span style="color: black;">一般</span>为 16KB,<span style="color: black;">因此呢</span>二级索引叶节点<span style="color: black;">能够</span>容纳<span style="color: black;">更加多</span>记录。扫描辅助索引比扫描主键索引<span style="color: black;">必须</span>更少的 I/O 操作。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">优化前<span style="color: black;">查找</span>时间:</span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/785a3b274b65454db736634f1057d698~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723347266&x-signature=ETZnzQFSBgVr2WP%2BSxGwpzUqlV8%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">优化后<span style="color: black;">查找</span>时间:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/47e5cae78abb4524b7ba013c47306fcd~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723347266&x-signature=RsHoQ9LcNnjCT8%2BoilfeWzUIGZM%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">其他优化<span style="color: black;">办法</span><span style="color: black;">包含</span>:</span></p><span style="color: black;">尽可能避免<span style="color: black;">运用</span>子<span style="color: black;">查找</span>,<span style="color: black;">由于</span>子<span style="color: black;">查找</span>会<span style="color: black;">增多</span><span style="color: black;">查找</span><span style="color: black;">繁杂</span>性和执行时间。相反,<span style="color: black;">运用</span>联接或表联接<span style="color: black;">查找</span>来替换子<span style="color: black;">查找</span>。</span><span style="color: black;">对<span style="color: black;">必须</span>分页的字段添加索引,<span style="color: black;">加强</span><span style="color: black;">查找</span>速度。分页时,<span style="color: black;">运用</span>limit和order by语句,并<span style="color: black;">按照</span>排序字段创建索引,<span style="color: black;">加强</span>排序效率。</span><span style="color: black;">您<span style="color: black;">能够</span><span style="color: black;">运用</span>缓存机制将分页结果缓存到缓存服务器中,<span style="color: black;">这般</span><span style="color: black;">能够</span>减<span style="color: black;">少许</span>据库<span style="color: black;">查找</span>的次数。</span>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">Join优化</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">Join</span></span><span style="color: black;">操作是<span style="color: black;">平常</span>的数据库操作之一,但<span style="color: black;">亦</span>很容易<span style="color: black;">引起</span>性能问题。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">Join</span></span><span style="color: black;"><span style="color: black;">查找</span>是<span style="color: black;">平常</span>的数据库操作,但连接操作<span style="color: black;">亦</span>会遇到性能问题。连接<span style="color: black;">查找</span>可分为内连接、左连接和右连接。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">执行操作时有两种<span style="color: black;">状况</span></span><span style="color: black;"><span style="color: black;">Join</span></span><span style="color: black;">:<span style="color: black;">运用</span>索引字段连接和不<span style="color: black;">运用</span>索引字段连接。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">以下是两个表 t1 和 t2 的示例,其中a列有索引,而b列<span style="color: black;">无</span>索引:</span></p><span style="color: black;">CREATE</span> <span style="color: black;">TABLE</span> t1 (
<span style="color: black;">id</span> <span style="color: black;">INT</span>(<span style="color: black;">11</span>) <span style="color: black;">NOT</span> <span style="color: black;">NULL</span> AUTO_INCREMENT,
a <span style="color: black;">INT</span>(<span style="color: black;">11</span>) <span style="color: black;">DEFAULT</span> <span style="color: black;">NULL</span>,
b <span style="color: black;">INT</span>(<span style="color: black;">11</span>) <span style="color: black;">DEFAULT</span> <span style="color: black;">NULL</span>,
PRIMARY <span style="color: black;">KEY</span> (<span style="color: black;">id</span>),
<span style="color: black;">INDEX</span> idx_a (a)
) <span style="color: black;">ENGINE</span>=<span style="color: black;">InnoDB</span> <span style="color: black;">DEFAULT</span> <span style="color: black;">CHARSET</span>=utf8mb4;
<span style="color: black;">CREATE</span> <span style="color: black;">TABLE</span> t2 (
<span style="color: black;">id</span> <span style="color: black;">INT</span>(<span style="color: black;">11</span>) <span style="color: black;">NOT</span> <span style="color: black;">NULL</span> AUTO_INCREMENT,
a <span style="color: black;">INT</span>(<span style="color: black;">11</span>) <span style="color: black;">DEFAULT</span> <span style="color: black;">NULL</span>,
b <span style="color: black;">INT</span>(<span style="color: black;">11</span>) <span style="color: black;">DEFAULT</span> <span style="color: black;">NULL</span>,
PRIMARY <span style="color: black;">KEY</span> (<span style="color: black;">id</span>),
<span style="color: black;">INDEX</span> <span style="color: black;">`idx_a`</span> (<span style="color: black;">`a`</span>)
)<span style="color: black;">ENGINE</span>=<span style="color: black;">InnoDB</span> <span style="color: black;">DEFAULT</span> <span style="color: black;">CHARSET</span>=utf8mb4;
<span style="color: black;">INSERT</span> <span style="color: black;">INTO</span> t1 (a, b) <span style="color: black;">VALUES</span> (<span style="color: black;">1</span>,<span style="color: black;">1</span>), (<span style="color: black;">2</span>,<span style="color: black;">2</span>), (<span style="color: black;">3</span>,<span style="color: black;">3</span>), (<span style="color: black;">4</span>,<span style="color: black;">4</span>), (<span style="color: black;">5</span>,<span style="color: black;">5</span>);
<span style="color: black;">INSERT</span> <span style="color: black;">INTO</span> t2 (a, b) <span style="color: black;">VALUES</span> (<span style="color: black;">1</span>,<span style="color: black;">1</span>), (<span style="color: black;">2</span>,<span style="color: black;">2</span>), (<span style="color: black;">3</span>,<span style="color: black;">3</span>), (<span style="color: black;">4</span>,<span style="color: black;">4</span>), (<span style="color: black;">5</span>,<span style="color: black;">5</span>);<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">表t2的结构与t1完全相同,t1有16743行数据,t2有540行数据。</span></p><span style="color: black;">explain</span> <span style="color: black;">select</span> * <span style="color: black;">from</span> t1 <span style="color: black;">inner</span> <span style="color: black;">join</span> t2 <span style="color: black;">on</span> t1.a = t2.a;<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/86e477efe2e149d4b0f23f245ce6e900~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723347266&x-signature=eYpPq5v8E%2B0po7DqrTFWlmKmt%2Bk%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">执行计划分析:</span></p><span style="color: black;"><span style="color: black;">t2</span></span><span style="color: black;">对(540 条记录)执行全表扫描。</span><span style="color: black;"><span style="color: black;">运用</span></span><span style="color: black;"><span style="color: black;">idx_a</span></span><span style="color: black;">连接</span><span style="color: black;"><span style="color: black;">t1和t2</span></span><span style="color: black;">检索 </span><span style="color: black;"><span style="color: black;">t1中</span></span><span style="color: black;">满足连接<span style="color: black;">要求</span>的行并将它们与t2 中的数据合并。将结果返回给客户端。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">耗时估计:</span></p><span style="color: black;">扫描</span><span style="color: black;"><span style="color: black;">t2</span></span><span style="color: black;">表:</span><span style="color: black;"><span style="color: black;">540</span></span><span style="color: black;">次</span><span style="color: black;">扫描</span><span style="color: black;"><span style="color: black;">t1</span></span><span style="color: black;">表:</span><span style="color: black;"><span style="color: black;">N</span></span><span style="color: black;">次。此过程的时间<span style="color: black;">繁杂</span>度大约是</span><span style="color: black;"><span style="color: black;">O(1)</span></span><span style="color: black;"><span style="color: black;">能够</span><span style="color: black;">运用</span>索引定位的数据的时间<span style="color: black;">繁杂</span>度。</span><span style="color: black;">内存中的比较次数:</span><span style="color: black;"><span style="color: black;">540 * 16743 = 9041220</span></span><span style="color: black;">。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这个过程被<span style="color: black;">叫作</span>为基于块的块嵌套循环连接(BNL)算法。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">该算法的原理<span style="color: black;">能够</span>理解为:将驱动表中的数据加载到连接缓冲区中,<span style="color: black;">而后</span>算法扫描驱动表并将驱动表的每一行与连接缓冲区中的数据进行比较。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">运用</span>BNL算法时,<span style="color: black;">倘若</span>连接缓冲区<span style="color: black;">不足</span>大,<span style="color: black;">没法</span>容纳驱动表中的所有数据,<span style="color: black;">能够</span>将表分成多个块,并将<span style="color: black;">每一个</span>块加载到连接缓冲区中进行比较。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">这般</span><span style="color: black;">能够</span>避免join buffer不足的问题。这种<span style="color: black;">办法</span><span style="color: black;">叫作</span>为 Block In-Memory Join 算法,<span style="color: black;">或</span> Sort-Merge Join 算法的变体。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">当</span><span style="color: black;"><span style="color: black;">t2</span></span><span style="color: black;">表有<span style="color: black;">海量</span>行,例如1000行,但连接缓冲区一次只能容纳800行时,将<span style="color: black;">运用</span>一种<span style="color: black;">叫作</span>为“分段”<span style="color: black;">办法</span>的策略。前 800 行将被加载到连接缓冲区中,<span style="color: black;">而后</span>算法将扫描 t1 表并执行比较。之后,连接缓冲区将被清除,剩余的 200 行将被加载到缓冲区中。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">而后</span>算法将再次扫描 t1 表并执行另一次比较。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">换句话说,<span style="color: black;">倘若</span>连接缓冲区在两次迭代中<span style="color: black;">没法</span>容纳所有<span style="color: black;">必须</span>的数据,算法将再次扫描 t1 表,并重复该过程,直到处理完所有<span style="color: black;">必须</span>的数据。</span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">JOIN的其他知识点</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在JOIN<span style="color: black;">查找</span>中,<span style="color: black;">通常</span>有两种算法:</span></p><span style="color: black;">NLJ 算法:<span style="color: black;">运用</span>索引字段进行连接。</span><span style="color: black;">BNL 算法:<span style="color: black;">运用</span>非索引字段进行连接。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">NLJ算法的性能优于BNL算法。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">连接<span style="color: black;">查找</span>的优化<span style="color: black;">办法</span>:</span></p><span style="color: black;">在连接列上添加索引<span style="color: black;">能够</span><span style="color: black;">帮忙</span>MySQL<span style="color: black;">选取</span>NLJ算法,该算法<span style="color: black;">一般</span>比BNL算法更快、更<span style="color: black;">有效</span>。</span><span style="color: black;">在连接<span style="color: black;">查找</span>中<span style="color: black;">运用</span>较小的表来驱动<span style="color: black;">很强</span>的表。</span><span style="color: black;"><span style="color: black;">运用</span>BNL算法时,如有必要,<span style="color: black;">能够</span><span style="color: black;">增多</span>内存中的连接缓冲区<span style="color: black;">体积</span>,以避免驱动表的多次扫描。</span>
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">为何</span>非索引字段不适合嵌套循环连接 (NLJ) 算法?</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这是<span style="color: black;">由于</span>NLJ算法采用磁盘扫描的方式:<span style="color: black;">首要</span>扫描驱动表并检索一行数据。<span style="color: black;">而后</span>,它<span style="color: black;">运用</span>该数据的<span style="color: black;">相关</span>字段在驱动表中<span style="color: black;">查询</span>相应的数据。此过程<span style="color: black;">运用</span>索引字段并且非常快。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">然则</span>,<span style="color: black;">倘若</span><span style="color: black;">运用</span>此<span style="color: black;">办法</span><span style="color: black;">运用</span>非索引字段,则<span style="color: black;">必须</span>全表扫描来<span style="color: black;">查询</span>驱动表中的<span style="color: black;">相关</span>数据,<span style="color: black;">由于</span>索引<span style="color: black;">拜访</span><span style="color: black;">办法</span>不可用。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">例如,<span style="color: black;">倘若</span>驱动表有100行数据,则驱动表<span style="color: black;">必须</span>扫描100次。<span style="color: black;">倘若</span>驱动表<span style="color: black;">包括</span>10,000行数据,<span style="color: black;">那样</span>就会有100*10,000=1,000,000次磁盘I/O操作,速度会非常慢。</span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">MySQL 中 In 和 Exists 关键字的优化。</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">IN和EXISTS</span></span><span style="color: black;">的优化<span style="color: black;">仅有</span>一个原则:</span><strong style="color: blue;"><span style="color: black;">用数据集较小的表来驱动数据集<span style="color: black;">很强</span>的表</span></strong><span style="color: black;">。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">当B表的数据集<span style="color: black;">少于</span>A表的数据集时,</span><span style="color: black;"><span style="color: black;">IN </span></span><span style="color: black;">优先于</span><span style="color: black;"><span style="color: black;">EXISTS</span></span><span style="color: black;">。</span></p><span style="color: black;">select</span> * <span style="color: black;">from</span> A <span style="color: black;">where</span> <span style="color: black;">id</span> <span style="color: black;">in</span> (<span style="color: black;">select</span> <span style="color: black;">id</span> <span style="color: black;">from</span> B)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">当A表的数据集<span style="color: black;">少于</span>B表的数据集时,EXISTS优先于IN,即数据集较小的表为外表。</span></p><span style="color: black;">select</span> * <span style="color: black;">from</span> A <span style="color: black;">where</span> <span style="color: black;">exists</span> (<span style="color: black;">select</span> <span style="color: black;">1</span> <span style="color: black;">from</span> B <span style="color: black;">where</span>B.id = A.id)<h1 style="color: black; text-align: left; margin-bottom: 10px;">索引设计原则</h1><span style="color: black;">先写代码,<span style="color: black;">而后</span><span style="color: black;">按照</span><span style="color: black;">状况</span>创建索引。<span style="color: black;">通常</span><span style="color: black;">来讲</span>,<span style="color: black;">仅有</span>代码写好之后,<span style="color: black;">咱们</span><span style="color: black;">才可</span>清楚地判断<span style="color: black;">那些</span>字段会<span style="color: black;">运用</span>索引。</span><span style="color: black;"><span style="color: black;">尽可能</span>让复合索引尽可能覆盖大部分业务。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"> <span style="color: black;">通常</span>不<span style="color: black;">意见</span>在表上创建太多索引,<span style="color: black;">由于</span><span style="color: black;">守护</span>索引<span style="color: black;">亦</span><span style="color: black;">必须</span>性能开销。<span style="color: black;">因此呢</span>,<span style="color: black;">意见</span>尝试创建1 到3个覆盖业务中SQL<span style="color: black;">查找</span><span style="color: black;">要求</span>的复合索引。</span></p><span style="color: black;">不要在低基数列上创建索引。</span><span style="color: black;"><span style="color: black;">能够</span><span style="color: black;">运用</span>部分前缀创建长字符串的索引</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"> 对长字段的部分字符串(例如 varchar(255))<span style="color: black;">创立</span>索引<span style="color: black;">能够</span>减少索引<span style="color: black;">运用</span>的磁盘空间。 例如,您只能索引前 20 个字符。但<span style="color: black;">必须</span><span style="color: black;">重视</span>的是,这可能会<span style="color: black;">引起</span>排序失败,<span style="color: black;">由于</span>只使 用了前 20 个字符,而索引只能<span style="color: black;">保准</span>粗略的<span style="color: black;">次序</span>。</span></p><span style="color: black;">基于慢<span style="color: black;">查找</span>日志优化索引是一项<span style="color: black;">连续</span>的任务。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"> <span style="color: black;">因为</span>业务需求的迭代,<span style="color: black;">查找</span><span style="color: black;">要求</span>可能会<span style="color: black;">出现</span>变化。<span style="color: black;">此时</span>就<span style="color: black;">能够</span><span style="color: black;">按照</span>慢SQL日志对索引进 行<span style="color: black;">连续</span>优化。</span></p><span style="color: black;"><span style="color: black;">意见</span>尽可能创建<span style="color: black;">独一</span>索引。</span><span style="color: black;">当where<span style="color: black;">要求</span>与</span><span style="color: black;"><span style="color: black;">ORDER BY</span></span><span style="color: black;"><span style="color: black;">查找</span>中存在冲突时,应优先为</span><span style="color: black;"><span style="color: black;">WHERE</span></span><span style="color: black;"><span style="color: black;">要求</span>创建索引。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">由于</span>过滤掉数据后,剩下的数据集<span style="color: black;">一般</span>比较小,排序的成本并不高。<span style="color: black;">因此呢</span>,最好<span style="color: black;">首要</span>使 用where子句上的索引来更快地过滤数据。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span><span style="color: black;">爱好</span>这篇<span style="color: black;">文案</span>,点赞支持一下,关注我<span style="color: black;">第1</span>时间查看<span style="color: black;">更加多</span>内容!</p>
seo常来的论坛,希望我的网站快点收录。 我们有着相似的经历,你的感受我深有体会。 论坛是一个舞台,让我们在这里尽情的释放自己。 这夸赞甜到心里,让我感觉温暖无比。 你的话语如春风拂面,温暖了我的心房,真的很感谢。
页:
[1]