MySQL数据库优化法则总结
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 网上关于SQL优化的教程<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></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;">期盼</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>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">要正确的优化SQL,<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 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>基本的认识,如网络带宽是2Mbps,硬盘是每分钟7200转等等。<span style="color: black;">因此呢</span>,为了快速找到SQL的性能瓶颈点,<span style="color: black;">咱们</span><span style="color: black;">亦</span>需要<span style="color: black;">认识</span><span style="color: black;">咱们</span>计算机系统的硬件基本性能指标,下图展示的当前主流计算机性能指标数据</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/Wc4rFKffLwQwTRF5gr6BjkV2CeJH6SBD06zWGMXCV7BdGl6LaSpoCmFqoIervzEzkhRFgvAarib4iavia5rU51Gww/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 从图上<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;">暗示</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>处理能力。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 从上图<span style="color: black;">能够</span>看出,计算机系统硬件性能从高到代依次为:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">CPU——Cache(L1-L2-L3)——内存——SSD硬盘——网络——硬盘</strong></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;">重点</span>的工作内容:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">CPU及内存:缓存数据<span style="color: black;">拜访</span>、比较、排序、事务检测、SQL解析、函数或<span style="color: black;">规律</span>运算;</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">网络:结果数据传输、SQL请求、远程数据库<span style="color: black;">拜访</span>(dblink);</strong></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></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>整理出如下图所示的性能基本优化法则:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/Wc4rFKffLwQwTRF5gr6BjkV2CeJH6SBDJicj9lV3D4hce7IuLecsPbNDN6WW2GHFlaeH4vgMKQfOLe4Ggmo7nbQ/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这个优化法则归纳为5个层次:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">1、 减<span style="color: black;">少许</span>据<span style="color: black;">拜访</span>(减少磁盘<span style="color: black;">拜访</span>)</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">2、 返回更<span style="color: black;">少许</span>据(减少网络传输或磁盘<span style="color: black;">拜访</span>)</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">3、 减少交互次数(减少网络传输)</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">4、 减少服务器CPU开销(减少CPU及内存开销)</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">5、 利用<span style="color: black;">更加多</span>资源(<span style="color: black;">增多</span>资源)</strong></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;">提高</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>任何一个SQL的性能优化都应该按这个规则由上到下来诊断问题并提出<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>问题。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 以下是<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;"><span style="color: black;"><span style="color: black;">优化法则</span></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 style="color: black;">性能<span style="color: black;">提高</span>效果</span></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 style="color: black;">优化成本</span></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 style="color: black;">少许</span>据<span style="color: black;">拜访</span></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;">1~1000</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></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 style="color: black;">少许</span>据</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;">1~100</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></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>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">1~20</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></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;">CPU</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;">1~5</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></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 style="color: black;">更加多</span>资源</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;">@~10</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></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 接下来,<span style="color: black;">咱们</span>针对5种优化法则列举常用的优化手段</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">a: 表的设计<span style="color: black;">恰当</span>化(符合3NF)</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">b: 优化SQL语句(索引)</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">c: 分表技术(水平分割、垂直分割)、分区技术</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">d: 读写[写: update/delete/add]分离</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">e: 存储过程 [模块化编程,<span style="color: black;">能够</span><span style="color: black;">加强</span>速度]</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">f: 对mysql配置优化 [配置最大并发数, <span style="color: black;">调节</span>缓存<span style="color: black;">体积</span> ]</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">g: mysql服务器硬件升级</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">h: <span style="color: black;">按时</span>的去清除不需要的数据,<span style="color: black;">按时</span>进行碎片整理</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">1、表的设计<span style="color: black;">恰当</span>化(符合3NF)</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1NF(<span style="color: black;">第1</span>范式)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 1NF的限定<span style="color: black;">要求</span>如下:(只要数据库是关系型数据库,就自动的满足1NF)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 1. <span style="color: black;">每一个</span>列必须有一个<span style="color: black;">独一</span>的名<span style="color: black;">叫作</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 2. 行和列的<span style="color: black;">秩序</span>无关紧要</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 3. 每一列都必须有单个数据类型</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 4. 不<span style="color: black;">准许</span><span style="color: black;">包括</span>相同值的两行</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">5. 每一列都必须<span style="color: black;">包括</span>一个单值 (一个列<span style="color: black;">不可</span><span style="color: black;">保留</span>多个数据值)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 6. 列<span style="color: black;">不可</span><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;">第1</span>范式会存在更新、删除和<span style="color: black;">插进</span><span style="color: black;">反常</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2NF(第二范式)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 2NF的限定<span style="color: black;">要求</span>如下:(<span style="color: black;">一般</span><span style="color: black;">咱们</span>设计一个主键来实现)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 1. 它符合<span style="color: black;">第1</span>范式</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 2. 所有的非键值字段均依赖于所有的键值字段</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>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3NF(第三范式)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3NF的限定<span style="color: black;">要求</span>如下:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 1. 符合2NF</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 2. 不<span style="color: black;">包括</span>传递<span style="color: black;">关联</span>性,(即,一个非键值字段的值依赖于另一个非键值字段的值),不含冗余数据</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">反3NF :<span style="color: black;">无</span>冗余的数据库未必是最好的数据库,有时为了<span style="color: black;">加强</span>运行效率,就必须降低范式标准,适当<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;">详细</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;">准许</span>冗余。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">2、</strong><strong style="color: blue;">优化SQL语句</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(1)<span style="color: black;">快速</span>的定位执行速度慢的语句 </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> a 开启慢<span style="color: black;">查找</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> b 设置慢<span style="color: black;">查找</span>时间</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> c 启用慢<span style="color: black;">查找</span>日志</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> d <span style="color: black;">经过</span>mysqldumoslow工具对慢日志进行<span style="color: black;">归类</span>汇总</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(2)分析SQL语句</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> a <span style="color: black;">经过</span>explain分析<span style="color: black;">查找</span> </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">b 通profiling<span style="color: black;">能够</span>得到更<span style="color: black;">仔细</span>的信息</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(3)SQL语句优化</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> a 创建索引(主键索引/<span style="color: black;">独一</span>索引/全文索引/普通索引)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> b 避免Select * (不<span style="color: black;">查找</span>多余的列与行)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> c Where中少用NOT、!=、<>、!<、!>、NOT EXISTS、NOT IN、NOT LIKE、OR,它们会忽略索引,<span style="color: black;">导致</span>全表扫描</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">d 用Where子句替代having子句,having只会在检索出所有记录之后才对结果集进行过滤</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> e <span style="color: black;">运用</span>视图(经常被<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;">繁杂</span>的表中真正能正确<span style="color: black;">运用</span>索引的人很少,即使是专业的DBA<span style="color: black;">亦</span>不<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;">增多</span>表记录的DML(INSERT,UPDATE,DELETE)开销,正确的索引<span style="color: black;">能够</span>让性能<span style="color: black;">提高</span>100,1000倍以上,不<span style="color: black;">恰当</span>的索引<span style="color: black;">亦</span>可能会让性能下降100倍,<span style="color: black;">因此呢</span>在一个表中创建什么样的索引需要平衡<span style="color: black;">各样</span>业务<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;">倘若</span><span style="color: black;">咱们</span>把一个表的内容认为是一本字典,那索引就相当于字典的目录,如下图所示:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/Wc4rFKffLwQwTRF5gr6BjkV2CeJH6SBDEERg7MBPbSqL63nwcLyg6QU9bfKiaK3bDMRZECgAHxrMwFQIasibIfNA/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">图中是一个字典按部首+笔划数的目录,相当于给字典建了一个按部首+笔划的组合索引。</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>(按拼音、笔划、部首等等)。</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;">叫作</span>为组合索引,如上图<span style="color: black;">便是</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><span style="color: black;">通常</span>在什么字段上建索引?</strong></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;">要求</span>:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">a 字段出<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>索引;</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">b 语句执行频率高,一天会有几千次以上;</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">c <span style="color: black;">经过</span>字段<span style="color: black;">要求</span>可筛选的记录集很小,那数据筛选比例是多少才适合?</strong></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;">评定</span>:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">小表(记录数<span style="color: black;">少于</span>10000行的表):筛选比例<10%;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">大表:(筛选返回记录数)<(表总记录数*单条记录长度)/10000/16</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">单条记录长度≈字段平均内容长度之和+字段数*2</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><span style="color: black;">晓得</span>SQL<span style="color: black;">是不是</span><span style="color: black;">运用</span>了正确的索引?</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">简单SQL<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>办,判断SQL的响应时间是一种策略,<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 style="color: black;">繁杂</span>,详见SQL执行计划专题介绍。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">索引对DML(INSERT,UPDATE,DELETE)附加的开销有多少?</strong></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;">体积</span>密切<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;">针对</span>Insert性能降低56%</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">索引<span style="color: black;">针对</span>Update性能降低47%</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">索引<span style="color: black;">针对</span>Delete性能降低29%</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>写IO压力比<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>的存储空间。</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>满足<span style="color: black;">需要</span>时<span style="color: black;">就可</span>,不要过度优化。</strong>在<span style="color: black;">实质</span>数据库中<span style="color: black;">咱们</span>不可能把<span style="color: black;">每一个</span>SQL请求的字段都建在索引里,<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></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">3、分表技术(水平分割、垂直分割)、分区技术</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><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;">倘若</span>遇到大表的<span style="color: black;">状况</span>下,SQL语句优化<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 style="color: black;">加强</span>表 的增删改查效率。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">什么是分表?</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;">咱们</span><span style="color: black;">能够</span><span style="color: black;">叫作</span>为子表,<span style="color: black;">每一个</span>表都对应三个文件,MYD数据文件,.MYI索引文件,.frm表结构文件。这些子表<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>上。app读写的时候<span style="color: black;">按照</span>事先定义好的规则得到对应的子表名,<span style="color: black;">而后</span>去操作它。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">什么是分区?</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;">能够</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>。分区后,表面上还是一张表,但数据散列到多个位置了。app读写的时候操作的还是大表名字,db自动去组织分区的数据。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">mysql分表和分区有什么联系呢?</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(1)都能<span style="color: black;">加强</span>mysql的性能,在高并发状态下都有一个良好的表现。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(2)分表和分区不矛盾,<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 style="color: black;">能够</span>采取分区的方式等。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(3)分表技术是比较麻烦的,需要手动去创建子表,app服务端读写时候需要计算子表名。采用merge好<span style="color: black;">有些</span>,但<span style="color: black;">亦</span>要创建子表和配置子表间的union关系。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(4)表分区相<span style="color: black;">针对</span>分表,操作方便,不需要创建子表。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">4、读写[写: update/delete/add]分离</strong></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;">不足</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>据库的联接,一方面采用优秀的代码框架,进行代码的优化,采用优秀的数据缓存技术如:memcached,<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;">因此呢</span>,<span style="color: black;">通常</span><span style="color: black;">来讲</span>都是<span style="color: black;">经过</span>主从复制(Master-Slave)的方式来同步数据,再<span style="color: black;">经过</span>读写分离(MySQL-Proxy,是MySQL官方<span style="color: black;">供给</span>的MySQL中间件服务)来<span style="color: black;">提高</span>数据库的并发负载能力 <span style="color: black;">这般</span>的<span style="color: black;">方法</span>来进行部署与实施的</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">实现方式</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;">第1</span>种:</strong>php程序上自己做<span style="color: black;">规律</span>判断,写php代码的时候,自己在程序上做<span style="color: black;">规律</span>判读写匹配。select,insert、update、delete做正则匹配,<span style="color: black;">按照</span>结果<span style="color: black;">选取</span>写服务器(主服务器)。<span style="color: black;">倘若</span>是select操作则<span style="color: black;">选取</span>读服务器(从服务器器) mysql_connect(读写的区分)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">第二种:</strong>MySQL中间件,基本的原理是让主数据库处理写操作(insert、update、delete),而从数据库处理<span style="color: black;">查找</span>操作(select)。而数据库的一致性则<span style="color: black;">经过</span>主从复制来实现。<span style="color: black;">因此</span>说主从复制是读写分离的<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;">有些</span>常用的MySQL中间件的背景介绍</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/Wc4rFKffLwQwTRF5gr6BjkV2CeJH6SBD1skNXrkSH3tFkhsyrBRQAuUMed9syS3uOiaiaVr0kSCpVce0cuVhnvZw/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">5、</strong><strong style="color: blue;">存储过程 </strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(1)<span style="color: black;">为何</span>需要存储过程</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">a 数据不安全,网络传送SQL代码,容易被未授权者截获</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> b 每次提交SQL代码都要经过语法编译后在执行,影响应用程序的运行性能</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> c 网络流量大,<span style="color: black;">针对</span>反复执行的SQL代码,在网络上多次传送,影响网络传输量</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(2)什么是存储过程</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">存储过程是SQL语句和<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>接收参数、输出参数、返回单个或多个结果值及返回值</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(3)<span style="color: black;">运用</span>存储过程的优点</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> a 模块化程序设计,只需创建一次,以后<span style="color: black;">就可</span>调用该存储过程任意次</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> b 执行速度快,效率高</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> c 减少网络流量</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> d <span style="color: black;">拥有</span>良好的安全性</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">6、对mysql配置优化 </strong></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>配置的优化,<span style="color: black;">详细</span>参数的解释就不写了,请<span style="color: black;">自动</span><span style="color: black;">查询</span>资料</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/Wc4rFKffLwQwTRF5gr6BjkV2CeJH6SBD8N0u7ViaDTpqyBNCUEbNwOuFf3uguDg5DrIkZ6qqWelicRXj9JDV7qgQ/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">7、mysql服务器硬件升级</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(1)磁盘 </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">MySQL每秒钟都在进行<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>认为磁盘I/O是制约MySQL性能的最大<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;">处理</span><span style="color: black;">方法</span>:<span style="color: black;">运用</span>RAID-10 、磁盘阵列设备SAN</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(2)CPU <span style="color: black;">针对</span>MySQL应用,<span style="color: black;">举荐</span><span style="color: black;">运用</span>S.M.P.架构的多路对<span style="color: black;">叫作</span>CPU</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(3)内存 越大越好</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(4)网卡 <span style="color: black;">最少</span>两个网卡,均为1GBE。<span style="color: black;">一般</span>我会将这两个nics绑定在<span style="color: black;">一块</span>以<span style="color: black;">供给</span>冗余</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">8、<span style="color: black;">按时</span>的去清除不需要的数据,<span style="color: black;">按时</span>进行碎片整理</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">什么是磁盘碎片?</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;">理学</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>一行数据,被分成N个片段,存储在N个位置.后者的<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>.</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;">运用</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>的问题.Engine <span style="color: black;">区别</span>,OPTIMIZE 的操作<span style="color: black;">亦</span>不<span style="color: black;">同样</span>的,MyISAM <span style="color: black;">由于</span>索引和数据是<span style="color: black;">掰开</span>的,<span style="color: black;">因此</span> OPTIMIZE <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>更慢。</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>:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(1)查看表碎片的<span style="color: black;">办法</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">select ROW_FORMAT,TABLE_ROWS,DATA_LENGTH,INDEX_LENGTH,MAX_DATA_LENGTH,DATA_FREE,ENGINE from TABLES where TABLE_SCHEMA=test_db and TABLE_NAME=table_name limit 1;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(2)Innodb存储引擎清理碎片<span style="color: black;">办法</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">ALTER TABLE tablename ENGINE=InnoDB</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(3)Myisam存储引擎清理碎片<span style="color: black;">办法</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">OPTIMIZE TABLE table_name</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;">倘若</span>表频繁被更改,<span style="color: black;">能够</span>做个计划任务,按周/月来整理。</p>
你说得对,我们一起加油,未来可期。 祝福你、祝你幸福、早日实现等。 你的言辞如同繁星闪烁,点亮了我心中的夜空。
页:
[1]