编辑文章 公众号爬虫数据库 null <div id="js_article" class="rich_media"> <div class="rich_media_inner"> <div id="page-content" class="rich_media_area_primary"> <div class="rich_media_area_primary_inner"> <div id="js_content" class="rich_media_content js_underline_content autoTypeSetting24psection " style="visibility: show;"> <section style="font-size: 16px; padding-right: 10px; padding-left: 10px; word-break: break-word; text-align: left; line-height: 1.25; color: #2b2b2b; letter-spacing: 2px; background-image: linear-gradient(90deg, rgba(50, 0, 0, 0.04) 3%, rgba(0, 0, 0, 0) 3%), linear-gradient(360deg, rgba(50, 0, 0, 0.04) 3%, rgba(0, 0, 0, 0) 3%); background-size: 20px 20px; background-position: center center; font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, 'PingFang SC', Cambria, Cochin, Georgia, Times, 'Times New Roman', serif;" data-tool="mdnice编辑器" data-website="https://www.mdnice.com"> <h2 style="margin-top: 30px; margin-bottom: 15px; font-weight: bold; color: black; font-size: 22px; border-bottom: 4px solid #40b8fa;" data-tool="mdnice编辑器"><span style="display: flex; color: #40b8fa; font-size: 20px; margin-left: 25px;">一. 问题描述</span></h2> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; margin-top: 10px; margin-bottom: 10px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">我们在设计表结构的时候,设计规范里面有一条如下规则:</p> <ul class="list-paddingleft-1" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; font-size: 15px; color: #595959; list-style-type: circle;" data-tool="mdnice编辑器"> <li> <section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; font-size: 14px;">对于可变长度的字段,在满足条件的前提下,尽可能使用较短的变长字段长度。</section> </li> </ul> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; margin-top: 10px; margin-bottom: 10px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">为什么这么规定?我在网上查了一下,主要基于两个方面</p> <ul class="list-paddingleft-1" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; font-size: 15px; color: #595959; list-style-type: circle;" data-tool="mdnice编辑器"> <li> <section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; font-size: 14px;"> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #2b2b2b; margin-top: 10px; margin-bottom: 10px; word-spacing: 2px;">基于存储空间的考虑</p> </section> </li> <li> <section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; font-size: 14px;"> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #2b2b2b; margin-top: 10px; margin-bottom: 10px; word-spacing: 2px;">基于性能的考虑</p> </section> </li> </ul> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; margin-top: 10px; margin-bottom: 10px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">网上说<code style="margin-right: 2px; margin-left: 2px; font-family: 'Operator Mono', Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #3594f7; background: rgba(59, 170, 250, 0.1); padding-right: 2px; padding-left: 2px; border-radius: 2px; height: 21px; line-height: 22px;">Varchar(50)</code>和<code style="margin-right: 2px; margin-left: 2px; font-family: 'Operator Mono', Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #3594f7; background: rgba(59, 170, 250, 0.1); padding-right: 2px; padding-left: 2px; border-radius: 2px; height: 21px; line-height: 22px;">varchar(500)</code>存储空间上是一样的,真的是这样吗?</p> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; margin-top: 10px; margin-bottom: 10px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">基于性能考虑,是因为过长的字段会影响到查询性能?</p> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; margin-top: 10px; margin-bottom: 10px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">本文我将带着这两个问题探讨验证一下</p> <h2 style="margin-top: 30px; margin-bottom: 15px; font-weight: bold; color: black; font-size: 22px; border-bottom: 4px solid #40b8fa;" data-tool="mdnice编辑器"><span style="display: flex; color: #40b8fa; font-size: 20px; margin-left: 25px;">二.验证存储空间区别</span></h2> <h3 style="color: black; font-size: 17px; font-weight: bold; text-align: center; margin-top: 20px; margin-bottom: 20px;" data-tool="mdnice编辑器"><span style="border-bottom: 2px solid RGBA(79, 177, 249, .65); color: #2b2b2b; padding-bottom: 2px;">1.准备两张表</span></h3> <pre style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;" data-tool="mdnice编辑器"><code style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; letter-spacing: 0px; padding-top: 15px; background: #282c34; border-radius: 5px;"><span style="color: #c678dd !important; line-height: 26px;">CREATE</span> <span style="color: #c678dd !important; line-height: 26px;">TABLE</span> <span style="color: #98c379; line-height: 26px;">`category_info_varchar_50`</span> (<br /> <span style="color: #98c379; line-height: 26px;">`id`</span> <span style="color: #e6c07b; line-height: 26px;">bigint</span>(<span style="color: #d19a66; line-height: 26px;">20</span>) <span style="color: #c678dd !important; line-height: 26px;">NOT</span> <span style="color: #56b6c2; line-height: 26px;">NULL</span> AUTO_INCREMENT <span style="color: #c678dd !important; line-height: 26px;">COMMENT</span> <span style="color: #98c379; line-height: 26px;">'主键'</span>,<br /> <span style="color: #98c379; line-height: 26px;">`name`</span> <span style="color: #e6c07b; line-height: 26px;">varchar</span>(<span style="color: #d19a66; line-height: 26px;">50</span>) <span style="color: #c678dd !important; line-height: 26px;">NOT</span> <span style="color: #56b6c2; line-height: 26px;">NULL</span> <span style="color: #c678dd !important; line-height: 26px;">COMMENT</span> <span style="color: #98c379; line-height: 26px;">'分类名称'</span>,<br /> <span style="color: #98c379; line-height: 26px;">`is_show`</span> <span style="color: #e6c07b; line-height: 26px;">tinyint</span>(<span style="color: #d19a66; line-height: 26px;">4</span>) <span style="color: #c678dd !important; line-height: 26px;">NOT</span> <span style="color: #56b6c2; line-height: 26px;">NULL</span> <span style="color: #c678dd !important; line-height: 26px;">DEFAULT</span> <span style="color: #98c379; line-height: 26px;">'0'</span> <span style="color: #c678dd !important; line-height: 26px;">COMMENT</span> <span style="color: #98c379; line-height: 26px;">'是否展示:0 禁用,1启用'</span>,<br /> <span style="color: #98c379; line-height: 26px;">`sort`</span> <span style="color: #e6c07b; line-height: 26px;">int</span>(<span style="color: #d19a66; line-height: 26px;">11</span>) <span style="color: #c678dd !important; line-height: 26px;">NOT</span> <span style="color: #56b6c2; line-height: 26px;">NULL</span> <span style="color: #c678dd !important; line-height: 26px;">DEFAULT</span> <span style="color: #98c379; line-height: 26px;">'0'</span> <span style="color: #c678dd !important; line-height: 26px;">COMMENT</span> <span style="color: #98c379; line-height: 26px;">'序号'</span>,<br /> <span style="color: #98c379; line-height: 26px;">`deleted`</span> <span style="color: #e6c07b; line-height: 26px;">tinyint</span>(<span style="color: #d19a66; line-height: 26px;">1</span>) <span style="color: #c678dd !important; line-height: 26px;">DEFAULT</span> <span style="color: #98c379; line-height: 26px;">'0'</span> <span style="color: #c678dd !important; line-height: 26px;">COMMENT</span> <span style="color: #98c379; line-height: 26px;">'是否删除'</span>,<br /> <span style="color: #98c379; line-height: 26px;">`create_time`</span> datetime <span style="color: #c678dd !important; line-height: 26px;">NOT</span> <span style="color: #56b6c2; line-height: 26px;">NULL</span> <span style="color: #c678dd !important; line-height: 26px;">COMMENT</span> <span style="color: #98c379; line-height: 26px;">'创建时间'</span>,<br /> <span style="color: #98c379; line-height: 26px;">`update_time`</span> datetime <span style="color: #c678dd !important; line-height: 26px;">NOT</span> <span style="color: #56b6c2; line-height: 26px;">NULL</span> <span style="color: #c678dd !important; line-height: 26px;">COMMENT</span> <span style="color: #98c379; line-height: 26px;">'更新时间'</span>,<br /> PRIMARY <span style="color: #c678dd !important; line-height: 26px;">KEY</span> (<span style="color: #98c379; line-height: 26px;">`id`</span>) <span style="color: #c678dd !important; line-height: 26px;">USING</span> BTREE,<br /> <span style="color: #c678dd !important; line-height: 26px;">KEY</span> <span style="color: #98c379; line-height: 26px;">`idx_name`</span> (<span style="color: #98c379; line-height: 26px;">`name`</span>) <span style="color: #c678dd !important; line-height: 26px;">USING</span> BTREE <span style="color: #c678dd !important; line-height: 26px;">COMMENT</span> <span style="color: #98c379; line-height: 26px;">'名称索引'</span><br />) <span style="color: #c678dd !important; line-height: 26px;">ENGINE</span>=<span style="color: #c678dd !important; line-height: 26px;">InnoDB</span> <span style="color: #c678dd !important; line-height: 26px;">DEFAULT</span> <span style="color: #c678dd !important; line-height: 26px;">CHARSET</span>=utf8mb4 <span style="color: #c678dd !important; line-height: 26px;">COMMENT</span>=<span style="color: #98c379; line-height: 26px;">'分类'</span>;<br /><br /><span style="color: #c678dd !important; line-height: 26px;">CREATE</span> <span style="color: #c678dd !important; line-height: 26px;">TABLE</span> <span style="color: #98c379; line-height: 26px;">`category_info_varchar_500`</span> (<br /> <span style="color: #98c379; line-height: 26px;">`id`</span> <span style="color: #e6c07b; line-height: 26px;">bigint</span>(<span style="color: #d19a66; line-height: 26px;">20</span>) <span style="color: #c678dd !important; line-height: 26px;">NOT</span> <span style="color: #56b6c2; line-height: 26px;">NULL</span> AUTO_INCREMENT <span style="color: #c678dd !important; line-height: 26px;">COMMENT</span> <span style="color: #98c379; line-height: 26px;">'主键'</span>,<br /> <span style="color: #98c379; line-height: 26px;">`name`</span> <span style="color: #e6c07b; line-height: 26px;">varchar</span>(<span style="color: #d19a66; line-height: 26px;">500</span>) <span style="color: #c678dd !important; line-height: 26px;">NOT</span> <span style="color: #56b6c2; line-height: 26px;">NULL</span> <span style="color: #c678dd !important; line-height: 26px;">COMMENT</span> <span style="color: #98c379; line-height: 26px;">'分类名称'</span>,<br /> <span style="color: #98c379; line-height: 26px;">`is_show`</span> <span style="color: #e6c07b; line-height: 26px;">tinyint</span>(<span style="color: #d19a66; line-height: 26px;">4</span>) <span style="color: #c678dd !important; line-height: 26px;">NOT</span> <span style="color: #56b6c2; line-height: 26px;">NULL</span> <span style="color: #c678dd !important; line-height: 26px;">DEFAULT</span> <span style="color: #98c379; line-height: 26px;">'0'</span> <span style="color: #c678dd !important; line-height: 26px;">COMMENT</span> <span style="color: #98c379; line-height: 26px;">'是否展示:0 禁用,1启用'</span>,<br /> <span style="color: #98c379; line-height: 26px;">`sort`</span> <span style="color: #e6c07b; line-height: 26px;">int</span>(<span style="color: #d19a66; line-height: 26px;">11</span>) <span style="color: #c678dd !important; line-height: 26px;">NOT</span> <span style="color: #56b6c2; line-height: 26px;">NULL</span> <span style="color: #c678dd !important; line-height: 26px;">DEFAULT</span> <span style="color: #98c379; line-height: 26px;">'0'</span> <span style="color: #c678dd !important; line-height: 26px;">COMMENT</span> <span style="color: #98c379; line-height: 26px;">'序号'</span>,<br /> <span style="color: #98c379; line-height: 26px;">`deleted`</span> <span style="color: #e6c07b; line-height: 26px;">tinyint</span>(<span style="color: #d19a66; line-height: 26px;">1</span>) <span style="color: #c678dd !important; line-height: 26px;">DEFAULT</span> <span style="color: #98c379; line-height: 26px;">'0'</span> <span style="color: #c678dd !important; line-height: 26px;">COMMENT</span> <span style="color: #98c379; line-height: 26px;">'是否删除'</span>,<br /> <span style="color: #98c379; line-height: 26px;">`create_time`</span> datetime <span style="color: #c678dd !important; line-height: 26px;">NOT</span> <span style="color: #56b6c2; line-height: 26px;">NULL</span> <span style="color: #c678dd !important; line-height: 26px;">COMMENT</span> <span style="color: #98c379; line-height: 26px;">'创建时间'</span>,<br /> <span style="color: #98c379; line-height: 26px;">`update_time`</span> datetime <span style="color: #c678dd !important; line-height: 26px;">NOT</span> <span style="color: #56b6c2; line-height: 26px;">NULL</span> <span style="color: #c678dd !important; line-height: 26px;">COMMENT</span> <span style="color: #98c379; line-height: 26px;">'更新时间'</span>,<br /> PRIMARY <span style="color: #c678dd !important; line-height: 26px;">KEY</span> (<span style="color: #98c379; line-height: 26px;">`id`</span>) <span style="color: #c678dd !important; line-height: 26px;">USING</span> BTREE,<br /> <span style="color: #c678dd !important; line-height: 26px;">KEY</span> <span style="color: #98c379; line-height: 26px;">`idx_name`</span> (<span style="color: #98c379; line-height: 26px;">`name`</span>) <span style="color: #c678dd !important; line-height: 26px;">USING</span> BTREE <span style="color: #c678dd !important; line-height: 26px;">COMMENT</span> <span style="color: #98c379; line-height: 26px;">'名称索引'</span><br />) <span style="color: #c678dd !important; line-height: 26px;">ENGINE</span>=<span style="color: #c678dd !important; line-height: 26px;">InnoDB</span> AUTO_INCREMENT=<span style="color: #d19a66; line-height: 26px;">288135</span> <span style="color: #c678dd !important; line-height: 26px;">DEFAULT</span> <span style="color: #c678dd !important; line-height: 26px;">CHARSET</span>=utf8mb4 <span style="color: #c678dd !important; line-height: 26px;">COMMENT</span>=<span style="color: #98c379; line-height: 26px;">'分类'</span>;<br /></code></pre> <h3 style="color: black; font-size: 17px; font-weight: bold; text-align: center; margin-top: 20px; margin-bottom: 20px;" data-tool="mdnice编辑器"><span style="border-bottom: 2px solid RGBA(79, 177, 249, .65); color: #2b2b2b; padding-bottom: 2px;">2.准备数据</span></h3> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; margin-top: 10px; margin-bottom: 10px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">给每张表插入相同的数据,为了凸显不同,插入100万条数据</p> <pre style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;" data-tool="mdnice编辑器"><code style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; letter-spacing: 0px; padding-top: 15px; background: #282c34; border-radius: 5px;">DELIMITER $$<br /><span style="color: #c678dd !important; line-height: 26px;">CREATE</span> <span style="color: #c678dd !important; line-height: 26px;">PROCEDURE</span> batchInsertData(<span style="color: #c678dd !important; line-height: 26px;">IN</span> total <span style="color: #e6c07b; line-height: 26px;">INT</span>)<br /><span style="color: #c678dd !important; line-height: 26px;">BEGIN</span><br /> <span style="color: #c678dd !important; line-height: 26px;">DECLARE</span> start_idx <span style="color: #e6c07b; line-height: 26px;">INT</span> <span style="color: #c678dd !important; line-height: 26px;">DEFAULT</span> <span style="color: #d19a66; line-height: 26px;">1</span>;<br /> <span style="color: #c678dd !important; line-height: 26px;">DECLARE</span> end_idx <span style="color: #e6c07b; line-height: 26px;">INT</span>;<br /> <span style="color: #c678dd !important; line-height: 26px;">DECLARE</span> batch_size <span style="color: #e6c07b; line-height: 26px;">INT</span> <span style="color: #c678dd !important; line-height: 26px;">DEFAULT</span> <span style="color: #d19a66; line-height: 26px;">500</span>;<br /> <span style="color: #c678dd !important; line-height: 26px;">DECLARE</span> insert_values <span style="color: #e6c07b; line-height: 26px;">TEXT</span>;<br /> <br /> <span style="color: #c678dd !important; line-height: 26px;">SET</span> end_idx = <span style="color: #c678dd !important; line-height: 26px;">LEAST</span>(total, start_idx + batch_size - <span style="color: #d19a66; line-height: 26px;">1</span>);<br /> <br /> WHILE start_idx <= total <span style="color: #c678dd !important; line-height: 26px;">DO</span><br /> <span style="color: #c678dd !important; line-height: 26px;">SET</span> insert_values = <span style="color: #98c379; line-height: 26px;">''</span>;<br /> WHILE start_idx <= end_idx <span style="color: #c678dd !important; line-height: 26px;">DO</span><br /> <span style="color: #c678dd !important; line-height: 26px;">SET</span> insert_values = <span style="color: #c678dd !important; line-height: 26px;">CONCAT</span>(insert_values, <span style="color: #c678dd !important; line-height: 26px;">CONCAT</span>(<span style="color: #98c379; line-height: 26px;">'(\'</span><span style="color: #c678dd !important; line-height: 26px;">name</span><span style="color: #98c379; line-height: 26px;">', start_idx, '</span>\<span style="color: #98c379; line-height: 26px;">', 0, 0, 0, NOW(), NOW()),'</span>));<br /> <span style="color: #c678dd !important; line-height: 26px;">SET</span> start_idx = start_idx + <span style="color: #d19a66; line-height: 26px;">1</span>;<br /> <span style="color: #c678dd !important; line-height: 26px;">END</span> <span style="color: #c678dd !important; line-height: 26px;">WHILE</span>;<br /> <span style="color: #c678dd !important; line-height: 26px;">SET</span> insert_values = <span style="color: #c678dd !important; line-height: 26px;">LEFT</span>(insert_values, <span style="color: #c678dd !important; line-height: 26px;">LENGTH</span>(insert_values) - <span style="color: #d19a66; line-height: 26px;">1</span>); <span style="color: #5c6370; font-style: italic; line-height: 26px;">-- Remove the trailing comma</span><br /> <span style="color: #c678dd !important; line-height: 26px;">SET</span> @<span style="color: #c678dd !important; line-height: 26px;">sql</span> = <span style="color: #c678dd !important; line-height: 26px;">CONCAT</span>(<span style="color: #98c379; line-height: 26px;">'INSERT INTO category_info_varchar_50 (name, is_show, sort, deleted, create_time, update_time) VALUES '</span>, insert_values, <span style="color: #98c379; line-height: 26px;">';'</span>);<br /> <br /> <span style="color: #c678dd !important; line-height: 26px;">PREPARE</span> stmt <span style="color: #c678dd !important; line-height: 26px;">FROM</span> @<span style="color: #c678dd !important; line-height: 26px;">sql</span>;<br /> <span style="color: #c678dd !important; line-height: 26px;">EXECUTE</span> stmt;<br /> <span style="color: #c678dd !important; line-height: 26px;">SET</span> @<span style="color: #c678dd !important; line-height: 26px;">sql</span> = <span style="color: #c678dd !important; line-height: 26px;">CONCAT</span>(<span style="color: #98c379; line-height: 26px;">'INSERT INTO category_info_varchar_500 (name, is_show, sort, deleted, create_time, update_time) VALUES '</span>, insert_values, <span style="color: #98c379; line-height: 26px;">';'</span>); <br /> <span style="color: #c678dd !important; line-height: 26px;">PREPARE</span> stmt <span style="color: #c678dd !important; line-height: 26px;">FROM</span> @<span style="color: #c678dd !important; line-height: 26px;">sql</span>;<br /> <span style="color: #c678dd !important; line-height: 26px;">EXECUTE</span> stmt;<br /> <br /> <span style="color: #c678dd !important; line-height: 26px;">SET</span> end_idx = <span style="color: #c678dd !important; line-height: 26px;">LEAST</span>(total, start_idx + batch_size - <span style="color: #d19a66; line-height: 26px;">1</span>);<br /> <span style="color: #c678dd !important; line-height: 26px;">END</span> <span style="color: #c678dd !important; line-height: 26px;">WHILE</span>;<br /><span style="color: #c678dd !important; line-height: 26px;">END</span>$$<br />DELIMITER ;<br /><br /><span style="color: #c678dd !important; line-height: 26px;">CALL</span> batchInsertData(<span style="color: #d19a66; line-height: 26px;">1000000</span>);<br /></code></pre> <h3 style="color: black; font-size: 17px; font-weight: bold; text-align: center; margin-top: 20px; margin-bottom: 20px;" data-tool="mdnice编辑器"><span style="border-bottom: 2px solid RGBA(79, 177, 249, .65); color: #2b2b2b; padding-bottom: 2px;">3.验证存储空间</span></h3> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; margin-top: 10px; margin-bottom: 10px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">查询第一张表SQL</p> <pre style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;" data-tool="mdnice编辑器"><code style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; letter-spacing: 0px; padding-top: 15px; background: #282c34; border-radius: 5px;"><span style="color: #c678dd !important; line-height: 26px;">SELECT</span><br /> table_schema <span style="color: #c678dd !important; line-height: 26px;">AS</span> <span style="color: #98c379; line-height: 26px;">"数据库"</span>,<br /> table_name <span style="color: #c678dd !important; line-height: 26px;">AS</span> <span style="color: #98c379; line-height: 26px;">"表名"</span>,<br /> table_rows <span style="color: #c678dd !important; line-height: 26px;">AS</span> <span style="color: #98c379; line-height: 26px;">"记录数"</span>,<br /> <span style="color: #c678dd !important; line-height: 26px;">TRUNCATE</span> ( data_length / <span style="color: #d19a66; line-height: 26px;">1024</span> / <span style="color: #d19a66; line-height: 26px;">1024</span>, <span style="color: #d19a66; line-height: 26px;">2</span> ) <span style="color: #c678dd !important; line-height: 26px;">AS</span> <span style="color: #98c379; line-height: 26px;">"数据容量(MB)"</span>,<br /> <span style="color: #c678dd !important; line-height: 26px;">TRUNCATE</span> ( index_length / <span style="color: #d19a66; line-height: 26px;">1024</span> / <span style="color: #d19a66; line-height: 26px;">1024</span>, <span style="color: #d19a66; line-height: 26px;">2</span> ) <span style="color: #c678dd !important; line-height: 26px;">AS</span> <span style="color: #98c379; line-height: 26px;">"索引容量(MB)"</span> <br /><span style="color: #c678dd !important; line-height: 26px;">FROM</span><br /> information_schema.TABLES <br /><span style="color: #c678dd !important; line-height: 26px;">WHERE</span><br /> table_schema = <span style="color: #98c379; line-height: 26px;">'test_mysql_field'</span> <br /> <span style="color: #c678dd !important; line-height: 26px;">and</span> TABLE_NAME = <span style="color: #98c379; line-height: 26px;">'category_info_varchar_50'</span><br /><span style="color: #c678dd !important; line-height: 26px;">ORDER</span> <span style="color: #c678dd !important; line-height: 26px;">BY</span><br /> data_length <span style="color: #c678dd !important; line-height: 26px;">DESC</span>,<br /> index_length <span style="color: #c678dd !important; line-height: 26px;">DESC</span>;<br /></code></pre> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; margin-top: 10px; margin-bottom: 10px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">查询结果</p> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; margin-top: 10px; margin-bottom: 10px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器"><img style="display: block; margin-left: auto; margin-right: auto;" src="http://img.liflag.cn/post/548df8575527f32e1febf5ef9cecef14.png" width="633" height="91" /></p> <figure style="margin-top: 10px; margin-bottom: 10px; display: flex; flex-direction: column; justify-content: center; align-items: center;" data-tool="mdnice编辑器"></figure> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; margin-top: 10px; margin-bottom: 10px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">查询第二张表SQL</p> <pre style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;" data-tool="mdnice编辑器"><code style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; letter-spacing: 0px; padding-top: 15px; background: #282c34; border-radius: 5px;"><span style="color: #c678dd !important; line-height: 26px;">SELECT</span><br /> table_schema <span style="color: #c678dd !important; line-height: 26px;">AS</span> <span style="color: #98c379; line-height: 26px;">"数据库"</span>,<br /> table_name <span style="color: #c678dd !important; line-height: 26px;">AS</span> <span style="color: #98c379; line-height: 26px;">"表名"</span>,<br /> table_rows <span style="color: #c678dd !important; line-height: 26px;">AS</span> <span style="color: #98c379; line-height: 26px;">"记录数"</span>,<br /> <span style="color: #c678dd !important; line-height: 26px;">TRUNCATE</span> ( data_length / <span style="color: #d19a66; line-height: 26px;">1024</span> / <span style="color: #d19a66; line-height: 26px;">1024</span>, <span style="color: #d19a66; line-height: 26px;">2</span> ) <span style="color: #c678dd !important; line-height: 26px;">AS</span> <span style="color: #98c379; line-height: 26px;">"数据容量(MB)"</span>,<br /> <span style="color: #c678dd !important; line-height: 26px;">TRUNCATE</span> ( index_length / <span style="color: #d19a66; line-height: 26px;">1024</span> / <span style="color: #d19a66; line-height: 26px;">1024</span>, <span style="color: #d19a66; line-height: 26px;">2</span> ) <span style="color: #c678dd !important; line-height: 26px;">AS</span> <span style="color: #98c379; line-height: 26px;">"索引容量(MB)"</span> <br /><span style="color: #c678dd !important; line-height: 26px;">FROM</span><br /> information_schema.TABLES <br /><span style="color: #c678dd !important; line-height: 26px;">WHERE</span><br /> table_schema = <span style="color: #98c379; line-height: 26px;">'test_mysql_field'</span> <br /> <span style="color: #c678dd !important; line-height: 26px;">and</span> TABLE_NAME = <span style="color: #98c379; line-height: 26px;">'category_info_varchar_500'</span><br /><span style="color: #c678dd !important; line-height: 26px;">ORDER</span> <span style="color: #c678dd !important; line-height: 26px;">BY</span><br /> data_length <span style="color: #c678dd !important; line-height: 26px;">DESC</span>,<br /> index_length <span style="color: #c678dd !important; line-height: 26px;">DESC</span>;<br /></code></pre> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; margin-top: 10px; margin-bottom: 10px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">查询结果</p> <figure style="margin-top: 10px; margin-bottom: 10px; display: flex; flex-direction: column; justify-content: center; align-items: center;" data-tool="mdnice编辑器"><img src="http://img.liflag.cn/post/a884b42d9bb24f4401ffa83b727d5587.png" width="632" height="87" /></figure> <h3 style="color: black; font-size: 17px; font-weight: bold; text-align: center; margin-top: 20px; margin-bottom: 20px;" data-tool="mdnice编辑器"><span style="border-bottom: 2px solid RGBA(79, 177, 249, .65); color: #2b2b2b; padding-bottom: 2px;">4.结论</span></h3> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; margin-top: 10px; margin-bottom: 10px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">两张表在占用空间上确实是一样的,并无差别</p> <h2 style="margin-top: 30px; margin-bottom: 15px; font-weight: bold; color: black; font-size: 22px; border-bottom: 4px solid #40b8fa;" data-tool="mdnice编辑器"><span style="display: flex; color: #40b8fa; font-size: 20px; margin-left: 25px;">三.验证性能区别</span></h2> <h3 style="color: black; font-size: 17px; font-weight: bold; text-align: center; margin-top: 20px; margin-bottom: 20px;" data-tool="mdnice编辑器"><span style="border-bottom: 2px solid RGBA(79, 177, 249, .65); color: #2b2b2b; padding-bottom: 2px;">1.验证索引覆盖查询</span></h3> <pre style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;" data-tool="mdnice编辑器"><code style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; letter-spacing: 0px; padding-top: 15px; background: #282c34; border-radius: 5px;"><span style="color: #c678dd !important; line-height: 26px;">select</span> <span style="color: #c678dd !important; line-height: 26px;">name</span> <span style="color: #c678dd !important; line-height: 26px;">from</span> category_info_varchar_50 <span style="color: #c678dd !important; line-height: 26px;">where</span> <span style="color: #c678dd !important; line-height: 26px;">name</span> = <span style="color: #98c379; line-height: 26px;">'name100000'</span><br /><span style="color: #5c6370; font-style: italic; line-height: 26px;">-- 耗时0.012s</span><br /><span style="color: #c678dd !important; line-height: 26px;">select</span> <span style="color: #c678dd !important; line-height: 26px;">name</span> <span style="color: #c678dd !important; line-height: 26px;">from</span> category_info_varchar_500 <span style="color: #c678dd !important; line-height: 26px;">where</span> <span style="color: #c678dd !important; line-height: 26px;">name</span> = <span style="color: #98c379; line-height: 26px;">'name100000'</span><br /><span style="color: #5c6370; font-style: italic; line-height: 26px;">-- 耗时0.012s</span><br /><span style="color: #c678dd !important; line-height: 26px;">select</span> <span style="color: #c678dd !important; line-height: 26px;">name</span> <span style="color: #c678dd !important; line-height: 26px;">from</span> category_info_varchar_50 <span style="color: #c678dd !important; line-height: 26px;">order</span> <span style="color: #c678dd !important; line-height: 26px;">by</span> <span style="color: #c678dd !important; line-height: 26px;">name</span>;<br /><span style="color: #5c6370; font-style: italic; line-height: 26px;">-- 耗时0.370s</span><br /><span style="color: #c678dd !important; line-height: 26px;">select</span> <span style="color: #c678dd !important; line-height: 26px;">name</span> <span style="color: #c678dd !important; line-height: 26px;">from</span> category_info_varchar_500 <span style="color: #c678dd !important; line-height: 26px;">order</span> <span style="color: #c678dd !important; line-height: 26px;">by</span> <span style="color: #c678dd !important; line-height: 26px;">name</span>;<br /><span style="color: #5c6370; font-style: italic; line-height: 26px;">-- 耗时0.379s</span><br /></code></pre> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; margin-top: 10px; margin-bottom: 10px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">通过索引覆盖查询性能差别不大</p> <h3 style="color: black; font-size: 17px; font-weight: bold; text-align: center; margin-top: 20px; margin-bottom: 20px;" data-tool="mdnice编辑器"><span style="border-bottom: 2px solid RGBA(79, 177, 249, .65); color: #2b2b2b; padding-bottom: 2px;">1.验证索引查询</span></h3> <pre style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;" data-tool="mdnice编辑器"><code style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; letter-spacing: 0px; padding-top: 15px; background: #282c34; border-radius: 5px;"><span style="color: #c678dd !important; line-height: 26px;">select</span> * <span style="color: #c678dd !important; line-height: 26px;">from</span> category_info_varchar_50 <span style="color: #c678dd !important; line-height: 26px;">where</span> <span style="color: #c678dd !important; line-height: 26px;">name</span> = <span style="color: #98c379; line-height: 26px;">'name100000'</span><br /><span style="color: #5c6370; font-style: italic; line-height: 26px;">--耗时 0.012s</span><br /><span style="color: #c678dd !important; line-height: 26px;">select</span> * <span style="color: #c678dd !important; line-height: 26px;">from</span> category_info_varchar_500 <span style="color: #c678dd !important; line-height: 26px;">where</span> <span style="color: #c678dd !important; line-height: 26px;">name</span> = <span style="color: #98c379; line-height: 26px;">'name100000'</span><br /><span style="color: #5c6370; font-style: italic; line-height: 26px;">--耗时 0.012s</span><br /><span style="color: #c678dd !important; line-height: 26px;">select</span> * <span style="color: #c678dd !important; line-height: 26px;">from</span> category_info_varchar_50 <span style="color: #c678dd !important; line-height: 26px;">where</span> <span style="color: #c678dd !important; line-height: 26px;">name</span> <span style="color: #c678dd !important; line-height: 26px;">in</span>(<span style="color: #98c379; line-height: 26px;">'name100'</span>,<span style="color: #98c379; line-height: 26px;">'name1000'</span>,<span style="color: #98c379; line-height: 26px;">'name100000'</span>,<span style="color: #98c379; line-height: 26px;">'name10000'</span>,<span style="color: #98c379; line-height: 26px;">'name1100000'</span>,<br /><span style="color: #98c379; line-height: 26px;">'name200'</span>,<span style="color: #98c379; line-height: 26px;">'name2000'</span>,<span style="color: #98c379; line-height: 26px;">'name200000'</span>,<span style="color: #98c379; line-height: 26px;">'name20000'</span>,<span style="color: #98c379; line-height: 26px;">'name2200000'</span>,<span style="color: #98c379; line-height: 26px;">'name300'</span>,<span style="color: #98c379; line-height: 26px;">'name3000'</span>,<span style="color: #98c379; line-height: 26px;">'name300000'</span>,<span style="color: #98c379; line-height: 26px;">'name30000'</span>,<span style="color: #98c379; line-height: 26px;">'name3300000'</span>,<br /><span style="color: #98c379; line-height: 26px;">'name400'</span>,<span style="color: #98c379; line-height: 26px;">'name4000'</span>,<span style="color: #98c379; line-height: 26px;">'name400000'</span>,<span style="color: #98c379; line-height: 26px;">'name40000'</span>,<span style="color: #98c379; line-height: 26px;">'name4400000'</span>,<span style="color: #98c379; line-height: 26px;">'name500'</span>,<span style="color: #98c379; line-height: 26px;">'name5000'</span>,<span style="color: #98c379; line-height: 26px;">'name500000'</span>,<span style="color: #98c379; line-height: 26px;">'name50000'</span>,<span style="color: #98c379; line-height: 26px;">'name5500000'</span>,<br /><span style="color: #98c379; line-height: 26px;">'name600'</span>,<span style="color: #98c379; line-height: 26px;">'name6000'</span>,<span style="color: #98c379; line-height: 26px;">'name600000'</span>,<span style="color: #98c379; line-height: 26px;">'name60000'</span>,<span style="color: #98c379; line-height: 26px;">'name6600000'</span>,<span style="color: #98c379; line-height: 26px;">'name700'</span>,<span style="color: #98c379; line-height: 26px;">'name7000'</span>,<span style="color: #98c379; line-height: 26px;">'name700000'</span>,<span style="color: #98c379; line-height: 26px;">'name70000'</span>,<span style="color: #98c379; line-height: 26px;">'name7700000'</span>,<span style="color: #98c379; line-height: 26px;">'name800'</span>,<br /><span style="color: #98c379; line-height: 26px;">'name8000'</span>,<span style="color: #98c379; line-height: 26px;">'name800000'</span>,<span style="color: #98c379; line-height: 26px;">'name80000'</span>,<span style="color: #98c379; line-height: 26px;">'name6600000'</span>,<span style="color: #98c379; line-height: 26px;">'name900'</span>,<span style="color: #98c379; line-height: 26px;">'name9000'</span>,<span style="color: #98c379; line-height: 26px;">'name900000'</span>,<span style="color: #98c379; line-height: 26px;">'name90000'</span>,<span style="color: #98c379; line-height: 26px;">'name9900000'</span>) <br /><span style="color: #5c6370; font-style: italic; line-height: 26px;">-- 耗时 0.011s -0.014s </span><br /><span style="color: #5c6370; font-style: italic; line-height: 26px;">-- 增加 order by name 耗时 0.012s - 0.015s</span><br /><br /><span style="color: #c678dd !important; line-height: 26px;">select</span> * <span style="color: #c678dd !important; line-height: 26px;">from</span> category_info_varchar_50 <span style="color: #c678dd !important; line-height: 26px;">where</span> <span style="color: #c678dd !important; line-height: 26px;">name</span> <span style="color: #c678dd !important; line-height: 26px;">in</span>(<span style="color: #98c379; line-height: 26px;">'name100'</span>,<span style="color: #98c379; line-height: 26px;">'name1000'</span>,<span style="color: #98c379; line-height: 26px;">'name100000'</span>,<span style="color: #98c379; line-height: 26px;">'name10000'</span>,<span style="color: #98c379; line-height: 26px;">'name1100000'</span>,<br /><span style="color: #98c379; line-height: 26px;">'name200'</span>,<span style="color: #98c379; line-height: 26px;">'name2000'</span>,<span style="color: #98c379; line-height: 26px;">'name200000'</span>,<span style="color: #98c379; line-height: 26px;">'name20000'</span>,<span style="color: #98c379; line-height: 26px;">'name2200000'</span>,<span style="color: #98c379; line-height: 26px;">'name300'</span>,<span style="color: #98c379; line-height: 26px;">'name3000'</span>,<span style="color: #98c379; line-height: 26px;">'name300000'</span>,<span style="color: #98c379; line-height: 26px;">'name30000'</span>,<span style="color: #98c379; line-height: 26px;">'name3300000'</span>,<br /><span style="color: #98c379; line-height: 26px;">'name400'</span>,<span style="color: #98c379; line-height: 26px;">'name4000'</span>,<span style="color: #98c379; line-height: 26px;">'name400000'</span>,<span style="color: #98c379; line-height: 26px;">'name40000'</span>,<span style="color: #98c379; line-height: 26px;">'name4400000'</span>,<span style="color: #98c379; line-height: 26px;">'name500'</span>,<span style="color: #98c379; line-height: 26px;">'name5000'</span>,<span style="color: #98c379; line-height: 26px;">'name500000'</span>,<span style="color: #98c379; line-height: 26px;">'name50000'</span>,<span style="color: #98c379; line-height: 26px;">'name5500000'</span>,<br /><span style="color: #98c379; line-height: 26px;">'name600'</span>,<span style="color: #98c379; line-height: 26px;">'name6000'</span>,<span style="color: #98c379; line-height: 26px;">'name600000'</span>,<span style="color: #98c379; line-height: 26px;">'name60000'</span>,<span style="color: #98c379; line-height: 26px;">'name6600000'</span>,<span style="color: #98c379; line-height: 26px;">'name700'</span>,<span style="color: #98c379; line-height: 26px;">'name7000'</span>,<span style="color: #98c379; line-height: 26px;">'name700000'</span>,<span style="color: #98c379; line-height: 26px;">'name70000'</span>,<span style="color: #98c379; line-height: 26px;">'name7700000'</span>,<span style="color: #98c379; line-height: 26px;">'name800'</span>,<br /><span style="color: #98c379; line-height: 26px;">'name8000'</span>,<span style="color: #98c379; line-height: 26px;">'name800000'</span>,<span style="color: #98c379; line-height: 26px;">'name80000'</span>,<span style="color: #98c379; line-height: 26px;">'name6600000'</span>,<span style="color: #98c379; line-height: 26px;">'name900'</span>,<span style="color: #98c379; line-height: 26px;">'name9000'</span>,<span style="color: #98c379; line-height: 26px;">'name900000'</span>,<span style="color: #98c379; line-height: 26px;">'name90000'</span>,<span style="color: #98c379; line-height: 26px;">'name9900000'</span>) <br /><span style="color: #5c6370; font-style: italic; line-height: 26px;">-- 耗时 0.012s -0.014s </span><br /><span style="color: #5c6370; font-style: italic; line-height: 26px;">-- 增加 order by name 耗时 0.014s - 0.017s</span><br /></code></pre> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; margin-top: 10px; margin-bottom: 10px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">索引范围查询性能基本相同, 增加了order By后开始有一定性能差别;</p> <h3 style="color: black; font-size: 17px; font-weight: bold; text-align: center; margin-top: 20px; margin-bottom: 20px;" data-tool="mdnice编辑器"><span style="border-bottom: 2px solid RGBA(79, 177, 249, .65); color: #2b2b2b; padding-bottom: 2px;">3.验证全表查询和排序</span></h3> <h4 style="margin-top: 30px; margin-bottom: 15px; font-weight: bold; color: black; font-size: 18px;" data-tool="mdnice编辑器"><span style="height: 16px; line-height: 16px; font-size: 16px;">全表无排序</span></h4> <p><span style="height: 16px; line-height: 16px; font-size: 16px;"><img src="http://img.liflag.cn/post/98d20f8309f2e2c784bf147de93e8898.png" /></span></p> <figure style="margin-top: 10px; margin-bottom: 10px; display: flex; flex-direction: column; justify-content: center; align-items: center;" data-tool="mdnice编辑器"></figure> <h4 style="margin-top: 30px; margin-bottom: 15px; font-weight: bold; color: black; font-size: 18px;" data-tool="mdnice编辑器"><span style="height: 16px; line-height: 16px; font-size: 16px;">全表有排序</span></h4> <pre style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;" data-tool="mdnice编辑器"><code style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; letter-spacing: 0px; padding-top: 15px; background: #282c34; border-radius: 5px;"><span style="color: #c678dd !important; line-height: 26px;">select</span> * <span style="color: #c678dd !important; line-height: 26px;">from</span> category_info_varchar_50 <span style="color: #c678dd !important; line-height: 26px;">order</span> <span style="color: #c678dd !important; line-height: 26px;">by</span> <span style="color: #c678dd !important; line-height: 26px;">name</span> ;<br /><span style="color: #5c6370; font-style: italic; line-height: 26px;">--耗时 1.498s</span><br /><span style="color: #c678dd !important; line-height: 26px;">select</span> * <span style="color: #c678dd !important; line-height: 26px;">from</span> category_info_varchar_500 <span style="color: #c678dd !important; line-height: 26px;">order</span> <span style="color: #c678dd !important; line-height: 26px;">by</span> <span style="color: #c678dd !important; line-height: 26px;">name</span> ;<br /><span style="color: #5c6370; font-style: italic; line-height: 26px;">--耗时 4.875s</span><br /></code></pre> <figure style="margin-top: 10px; margin-bottom: 10px; display: flex; flex-direction: column; justify-content: center; align-items: center;" data-tool="mdnice编辑器"></figure> <p><span style="height: 16px; line-height: 16px; font-size: 16px;"><img src="http://img.liflag.cn/post/29d536db024408415418e9e183c3d556.png" width="645" height="154" /></span></p> <h4 style="margin-top: 30px; margin-bottom: 15px; font-weight: bold; color: black; font-size: 18px;" data-tool="mdnice编辑器"><span style="height: 16px; line-height: 16px; font-size: 16px;">结论:</span></h4> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; margin-top: 10px; margin-bottom: 10px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">全表扫描无排序情况下,两者性能无差异,在全表有排序的情况下, 两种性能差异巨大;</p> <h4 style="margin-top: 30px; margin-bottom: 15px; font-weight: bold; color: black; font-size: 18px;" data-tool="mdnice编辑器"><span style="height: 16px; line-height: 16px; font-size: 16px;">分析原因</span></h4> <h5 style="margin-top: 30px; margin-bottom: 15px; font-weight: bold; color: black;" data-tool="mdnice编辑器">varchar50 全表执行sql分析</h5> <figure style="margin-top: 10px; margin-bottom: 10px; display: flex; flex-direction: column; justify-content: center; align-items: center;" data-tool="mdnice编辑器"><img src="http://img.liflag.cn/post/f109f7540297e890ec46e6948d2affc0.png" width="739" height="559" /></figure> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; margin-top: 10px; margin-bottom: 10px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">我发现86%的时花在数据传输上,接下来我们看状态部分,关注Created_tmp_files和sort_merge_passes</p> <figure style="margin-top: 10px; margin-bottom: 10px; display: flex; flex-direction: column; justify-content: center; align-items: center;" data-tool="mdnice编辑器"><img src="http://img.liflag.cn/post/508dba3f28636a1d206e554befc0b2e9.png" width="753" height="495" /></figure> <figure style="margin-top: 10px; margin-bottom: 10px; display: flex; flex-direction: column; justify-content: center; align-items: center;" data-tool="mdnice编辑器"><img src="http://img.liflag.cn/post/8e20f05f56803f5e0bf4e27338ab3030.png" width="751" height="487" /></figure> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; margin-top: 10px; margin-bottom: 10px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">Created_tmp_files为3</p> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; margin-top: 10px; margin-bottom: 10px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">sort_merge_passes为95</p> <h5 style="margin-top: 30px; margin-bottom: 15px; font-weight: bold; color: black;" data-tool="mdnice编辑器">varchar500 全表执行sql分析</h5> <figure style="margin-top: 10px; margin-bottom: 10px; display: flex; flex-direction: column; justify-content: center; align-items: center;" data-tool="mdnice编辑器"><img src="http://img.liflag.cn/post/25da4d6eeaaec5fa8049bce6af7c480d.png" width="615" height="753" /></figure> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; margin-top: 10px; margin-bottom: 10px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">增加了临时表排序</p> <figure style="margin-top: 10px; margin-bottom: 10px; display: flex; flex-direction: column; justify-content: center; align-items: center;" data-tool="mdnice编辑器"><img src="http://img.liflag.cn/post/a09900a5972caf02d4d77baa453abb86.png" width="664" height="438" /></figure> <figure style="margin-top: 10px; margin-bottom: 10px; display: flex; flex-direction: column; justify-content: center; align-items: center;" data-tool="mdnice编辑器"><img src="http://img.liflag.cn/post/48b8827af50ce1de50d33d45b64f33a4.png" width="675" height="454" /></figure> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; margin-top: 10px; margin-bottom: 10px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">Created_tmp_files 为 4</p> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; margin-top: 10px; margin-bottom: 10px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">sort_merge_passes为645</p> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; margin-top: 10px; margin-bottom: 10px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">关于sort_merge_passes, Mysql给出了如下描述:</p> <blockquote style="font-size: 0.9em; overflow: auto; padding: 10px 10px 10px 20px; margin-bottom: 20px; margin-top: 20px; text-size-adjust: 100%; line-height: 1.55em; border-radius: 6px; color: #595959; box-sizing: inherit; border-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-color: rgba(64, 184, 250, 0.4); background: rgba(64, 184, 250, 0.1);" data-tool="mdnice编辑器"><span style="color: rgba(64, 184, 250, .5); font-size: 34px; line-height: 1; font-weight: bold;">❝</span> <p style="padding-top: 8px; padding-bottom: 8px; font-size: 14px; word-spacing: 2px; line-height: 26px;">Number of merge passes that the sort algorithm has had to do. If this value is large, you may want to increase the value of the sort_buffer_size.</p> <span style="float: right; color: rgba(64, 184, 250, .5);">❞</span></blockquote> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; margin-top: 10px; margin-bottom: 10px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">其实sort_merge_passes对应的就是MySQL做归并排序的次数,也就是说,如果sort_merge_passes值比较大,说明sort_buffer和要排序的数据差距越大,我们可以通过增大sort_buffer_size或者让填入sort_buffer_size的键值对更小来缓解sort_merge_passes归并排序的次数。</p> <h2 style="margin-top: 30px; margin-bottom: 15px; font-weight: bold; color: black; font-size: 22px; border-bottom: 4px solid #40b8fa;" data-tool="mdnice编辑器"><span style="display: flex; color: #40b8fa; font-size: 20px; margin-left: 25px;">四.最终结论</span></h2> <p style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; margin-top: 10px; margin-bottom: 10px; font-size: 14px; word-spacing: 2px;" data-tool="mdnice编辑器">至此,我们不难发现,当我们最该字段进行排序操作的时候,Mysql会根据该字段的设计的长度进行内存预估, 如果设计过大的可变长度, 会导致内存预估的值超出sort_buffer_size的大小, 导致mysql采用磁盘临时文件排序,最终影响查询性能;</p> </section> <section style="padding-right: 10px; padding-left: 10px; outline: 0px; text-wrap: wrap; background-color: #ffffff; font-size: 16px; color: black; line-height: 1.6; letter-spacing: 0px; text-align: left; word-break: break-all; font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, 'PingFang SC', Cambria, Cochin, Georgia, Times, 'Times New Roman', serif; visibility: visible;" data-tool="mdnice编辑器" data-website="https://www.mdnice.com"> <blockquote class="js_blockquote_wrap" style="outline: 0px; color: var(--weui-fg-1);" data-type="2" data-url="" data-author-name="" data-content-utf8-length="37" data-source-title=""> <section class="js_blockquote_digest" style="outline: 0px;"> <section style="outline: 0px;"><span style="outline: 0px; color: #000000; font-size: 13px;">来源:juejin.cn/post/7350228838151847976</span></section> </section> </blockquote> </section> <p style="margin-bottom: 0px; outline: 0px; font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif; letter-spacing: 0.544px; text-wrap: wrap; background-color: #ffffff; text-align: left;"> </p> <p style="display: none;"> </p> </div> </div> </div> </div> </div> 标题: 分类: 请选择分类(必选) 博客 酷玩 分享 相册 标签: 按 Tab、回车或逗号把输入的文字变成标签;点 × 或在输入框为空时按退格删除;最多 8 个 封面图: 上传图片 建议尺寸 800x400,图片大小不超过 1MB 展开更多设置(关键词、摘要、转载、评论) 关键词: 摘要: 一. 问题描述 我们在设计表结构的时候,设计规范里面有一条如下规则: 对于可变长度的字段,在满足条件的前提下,尽可能使用较短的变长字段长度。 为什么这么规定?我在网上查了一下,主要基于两个方面 基于存储空间的考虑 基于性能的... 选项: 是否转载 启用评论 勾选「是否转载」可标注文章来源,原文链接可留空。 正文: B I U H2 H3 引用 • 列表 1. 列表 链接 图片 清格式 源码 该文章是富文本(HTML)写的,已自动切到富文本编辑器,不会按 markdown 解析;要精确改 HTML 点「源码」。 保存修改