tw4ld6 发表于 2024-7-27 01:06:58

面向程序员的数据库拜访性能优化法则(上)


    <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 style="color: black;"><span style="color: black;">尤其</span>说明:</span></span></strong></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 style="color: black;">本文只是面对数据库应用<span style="color: black;">研发</span>的程序员,不适合专业</span><span style="color: black;">DBA</span><span style="color: black;">,</span><span style="color: black;">DBA</span><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;">2、</span><span style="color: black;"><span style="color: black;">本文许多示例及概念是基于</span><span style="color: black;">Oracle</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;">KV</span><span style="color: black;">数据库或内存数据库<span style="color: black;">或</span>是基于</span><span style="color: black;">SSD</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;">3、</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;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">读者对像:</span></span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">研发</span>人员:</span></strong><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></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">架构师:</span></strong><span style="color: black;"><span style="color: black;">倘若</span>你<span style="color: black;">已然</span>是数据库应用的架构师,那本文的知识你应该清楚</span><span style="color: black;">90%</span><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;"><strong style="color: blue;"><span style="color: black;">DBA</span></strong><strong style="color: blue;"><span style="color: black;">(数据库管理员):</span></strong><span style="color: black;">大型数据库优化的知识非常<span style="color: black;">繁杂</span>,本文只是从程序员的<span style="color: black;">方向</span>来谈性能优化,</span><span style="color: black;">DBA</span><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></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 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 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;">DBA</span><span style="color: black;"><span style="color: black;">或</span>是</span><span style="color: black;">PL/SQL</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 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 style="color: black;">亦</span>许不清楚线上正式的服务器硬件配置,<span style="color: black;">咱们</span>不可能像</span><span style="color: black;">DBA</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><span style="color: black;">SQL</span><span style="color: black;">的业务<span style="color: black;">规律</span>,<span style="color: black;">咱们</span>清楚</span><span style="color: black;">SQL</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><span style="color: black;">SQL</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;">SQL</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></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>优化法则简介</p>

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"> &nbsp; &nbsp; &nbsp;<span style="color: black;">要正确的优化</span><span style="color: black;">SQL</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><span style="color: black;">SQL</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 style="color: black;">由于</span><span style="color: black;">咱们</span>对这些慢速设备的性能数据有<span style="color: black;">有些</span>基本的认识,如网络带宽是</span><span style="color: black;">2Mbps</span><span style="color: black;">,硬盘是每分钟</span><span style="color: black;">7200</span><span style="color: black;">转等等。<span style="color: black;">因此呢</span>,为了快速找到</span><span style="color: black;">SQL</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></span><img src="http://mmbiz.qpic.cn/mmbiz_jpg/Hk1ceA7cQUDIUmSeQFFicDeH5MUC37Mybw8jB9VHL4RG9KiczmcKlvWcjo44libZEDePcSfhg9vUst6FiayxIuS87w/640?wx_fmt=jpeg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;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 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;">表率</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></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;">CPU</span><span style="color: black;">——</span><span style="color: black;">Cache(L1-L2-L3)</span><span style="color: black;">——内存——</span><span style="color: black;">SSD</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 style="color: black;">因为</span></span><span style="color: black;">SSD</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><span style="color: black;">SSD</span><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>数据库知识,<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;">CPU</span><span style="color: black;">及内存:缓存数据<span style="color: black;">拜访</span>、比较、排序、事务检测、</span><span style="color: black;">SQL</span><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><span style="color: black;">SQL</span><span style="color: black;">请求、远程数据库<span style="color: black;">拜访</span>(</span><span style="color: black;">dblink</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></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><img src="http://mmbiz.qpic.cn/mmbiz_jpg/Hk1ceA7cQUDIUmSeQFFicDeH5MUC37MybXKZsfsVgw8ck1EXbDIVnkyQuuwEoRDyphtU6M7X3Fn1V11yCticc8jA/640?wx_fmt=jpeg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;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><span style="color: black;">5</span><span style="color: black;">个层次:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">1、</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;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">2、</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;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">3、</span><span style="color: black;">减少交互次数(减少网络传输)</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">4、</span><span style="color: black;"><span style="color: black;">减少服务器</span><span style="color: black;">CPU</span><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;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">5、</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;"><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 style="color: black;">更加多</span>,优化成本<span style="color: black;">亦</span>更低。<span style="color: black;">咱们</span>任何一个</span><span style="color: black;">SQL</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></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;"><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;"><strong style="color: blue;"><span style="color: black;">性能<span style="color: black;">提高</span>效果</span></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 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;">1~1000</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>据</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">1~100</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></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">1~20</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><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;">1~5</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>资源</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">@~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><span style="color: black;">5</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 style="color: black;">2、</span>Oracle数据库两个基本概念</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">数据块(Block)</span></strong></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;">IO</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><span style="color: black;">DBA</span><span style="color: black;">在创建数据库或表空间时指定,可指定为</span><span style="color: black;">2K</span><span style="color: black;">、</span><span style="color: black;">4K</span><span style="color: black;">、</span><span style="color: black;">8K</span><span style="color: black;">、</span><span style="color: black;">16K</span><span style="color: black;">或</span><span style="color: black;">32K</span><span style="color: black;">字节。下图是一个</span><span style="color: black;">Oracle</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;"><img src="http://mmbiz.qpic.cn/mmbiz_jpg/Hk1ceA7cQUDIUmSeQFFicDeH5MUC37MybGyxRfj5oUjrEFT0mAqqMVJBdjThP9hLjFJX4z1cbxw28xGrurllrzw/640?wx_fmt=jpeg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></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;">ROWID</span></strong></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;">ROWID</span><span style="color: black;">是每条记录在数据库中的<span style="color: black;">独一</span>标识,<span style="color: black;">经过</span></span><span style="color: black;">ROWID</span><span style="color: black;"><span style="color: black;">能够</span>直接定位记录到对应的文件号及数据块位置。</span><span style="color: black;">ROWID</span><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;"><img src="http://mmbiz.qpic.cn/mmbiz_jpg/Hk1ceA7cQUDIUmSeQFFicDeH5MUC37MybTGxOl7TZHAUvWyCCULN15RbLvhEYSnNRVatntiba8rWKEw0OKeL6qDg/640?wx_fmt=jpeg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">3、</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;"><strong style="color: blue;"><span style="color: black;">1 减<span style="color: black;">少许</span>据<span style="color: black;">拜访</span></span></strong></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;">1.1 创建并<span style="color: black;">运用</span>正确的索引</span></strong></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 style="color: black;">DBA</span><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;">索引会大大<span style="color: black;">增多</span>表记录的</span><span style="color: black;">DML(INSERT,UPDATE,DELETE)</span><span style="color: black;">开销,正确的索引<span style="color: black;">能够</span>让性能<span style="color: black;">提高</span></span><span style="color: black;">100</span><span style="color: black;">,</span><span style="color: black;">1000</span><span style="color: black;">倍以上,不<span style="color: black;">恰当</span>的索引<span style="color: black;">亦</span>可能会让性能下降</span><span style="color: black;">100</span><span style="color: black;">倍,<span style="color: black;">因此呢</span>在一个表中创建什么样的索引需要平衡<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;">平常</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 style="color: black;">那些</span>种类?</span></strong></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 style="color: black;">B-TREE</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><span style="color: black;">B-TREE</span><span style="color: black;">索引<span style="color: black;">包含</span><span style="color: black;">非常多</span>扩展类型,如组合索引、反向索引、函数索引等等,以下是</span><span style="color: black;">B-TREE</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;">B-TREE</span><span style="color: black;">索引<span style="color: black;">亦</span><span style="color: black;">叫作</span>为平衡树索引</span><span style="color: black;">(Balance Tree)</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><span style="color: black;">B-TREE</span><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;"><strong style="color: blue;"><span style="color: black;">叶子节点内容:</span></strong><span style="color: black;">索引字段内容</span><span style="color: black;">+</span><span style="color: black;">表记录</span><span style="color: black;">ROWID</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">根节点,分支节点内容:</span></strong><span style="color: black;">当一个数据块中<span style="color: black;">不可</span>放下所有索引字段数据时,就会形成树形的根节点或分支节点,根节点与分支节点<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;">一个普通的</span><span style="color: black;">BTREE</span><span style="color: black;">索引结构示意图如下所示:</span></span><img src="http://mmbiz.qpic.cn/mmbiz_jpg/Hk1ceA7cQUDIUmSeQFFicDeH5MUC37MybAXth6yy0ibAqja8O7SvCCxsWChdZMXw3dbvsibMtPzbzC9JOWicoHdR3w/640?wx_fmt=jpeg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;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 style="color: black;">倘若</span><span style="color: black;">咱们</span>把一个表的内容认为是一本字典,那索引就相当于字典的目录,如下图所示:</span><img src="http://mmbiz.qpic.cn/mmbiz_jpg/Hk1ceA7cQUDIUmSeQFFicDeH5MUC37MybwYzn2okGW0kAZ7uRDzibfwLBWhWvHXl81icqQT06Q4iaErKV4tmh0nYYQ/640?wx_fmt=jpeg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"><img src="http://mmbiz.qpic.cn/mmbiz_jpg/Hk1ceA7cQUDIUmSeQFFicDeH5MUC37MybibL6xCabkOfUuBfI1ib7x0cNwtOosl3pp4lia4h235CYFmSiaHpKUzuVtg/640?wx_fmt=jpeg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;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 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></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 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></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">SQL</span></strong><strong style="color: blue;"><span style="color: black;">什么<span style="color: black;">要求</span>会<span style="color: black;">运用</span>索引?</span></strong></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></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">INDEX_COLUMN = ?</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">INDEX_COLUMN &gt; ?</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">INDEX_COLUMN &gt;= ?</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">INDEX_COLUMN &lt; ?</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">INDEX_COLUMN &lt;= ?</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">INDEX_COLUMN between ? and ?</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">INDEX_COLUMN in (?,?,...,?)</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;"><span style="color: black;">INDEX_COLUMN like ?||%</span><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;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;"><span style="color: black;">T1. INDEX_COLUMN=T2. COLUMN1</span><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;"><strong style="color: blue;"><span style="color: black;">SQL</span></strong><strong style="color: blue;"><span style="color: black;">什么<span style="color: black;">要求</span>不会<span style="color: black;">运用</span>索引?</span></strong></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 style="color: black;">查找</span><span style="color: black;">要求</span></span></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 style="color: black;">不可</span><span style="color: black;">运用</span>索引<span style="color: black;">原由</span></span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">INDEX_COLUMN &lt;&gt; ?</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">INDEX_COLUMN not in (?,?,...,?)</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;">function(INDEX_COLUMN) = ?</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">INDEX_COLUMN + 1 = ?</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">INDEX_COLUMN || a = ?</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;">INDEX_COLUMN like %||?</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">INDEX_COLUMN like %||?||%</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 style="color: black;">Like</span><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;">INDEX_COLUMN is null</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;">B-TREE</span><span style="color: black;">索引里不<span style="color: black;">保留</span>字段为</span><span style="color: black;">NULL</span><span style="color: black;">值记录,<span style="color: black;">因此呢</span></span><span style="color: black;">IS NULL</span><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;">NUMBER_INDEX_COLUMN=12345</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">CHAR_INDEX_COLUMN=12345</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;">Oracle</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></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">a.INDEX_COLUMN=a.COLUMN_1</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></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 style="color: black;">运用</span>函数索引,这种<span style="color: black;">需要</span><span style="color: black;">意见</span>与</span><span style="color: black;">DBA</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;">第1</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 style="color: black;">咱们</span></span><span style="color: black;">company</span><span style="color: black;">表建了一个</span><span style="color: black;">id+name</span><span style="color: black;">的组合索引,以下</span><span style="color: black;">SQL</span><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;">Select * from company where name=?</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;">Oracle9i</span><span style="color: black;">后引入了一种</span><span style="color: black;">index skip scan</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;">index skip scan</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></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 style="color: black;">咱们</span><span style="color: black;">通常</span>在什么字段上建索引?</span></strong></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></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;"><span style="color: black;">1</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 style="color: black;">运用</span>索引;</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;"><span style="color: black;">2</span><span style="color: black;">、语句执行频率高,一天会有几千次以上;</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;"><span style="color: black;">3</span><span style="color: black;">、<span style="color: black;">经过</span>字段<span style="color: black;">要求</span>可筛选的记录集很小,<strong style="color: blue;">那数据筛选比例是多少才适合?</strong></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></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">小表</span><span style="color: black;">(</span></strong><strong style="color: blue;"><span style="color: black;">记录数<span style="color: black;">少于</span></span><span style="color: black;">10000</span></strong><strong style="color: blue;"><span style="color: black;">行的表</span><span style="color: black;">)</span></strong><strong style="color: blue;"><span style="color: black;">:筛选比例</span><span style="color: black;">&lt;10%</span></strong><strong style="color: blue;"><span style="color: black;">;</span></strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">大表:</span><span style="color: black;">(</span></strong><strong style="color: blue;"><span style="color: black;">筛选返回记录数</span><span style="color: black;">)&lt;(</span></strong><strong style="color: blue;"><span style="color: black;">表总记录数</span><span style="color: black;">*</span></strong><strong style="color: blue;"><span style="color: black;">单条记录长度</span><span style="color: black;">)/10000/16</span></strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">单条记录长度≈字段平均内容长度之和</span><span style="color: black;">+</span></strong><strong style="color: blue;"><span style="color: black;">字段数</span><span style="color: black;">*2</span></strong></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 style="color: black;">B-TREE</span><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;"><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;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">平常</span>字段名</span></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></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">ID,PK</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;">PRODUCT_ID,COMPANY_ID,MEMBER_ID,ORDER_ID,TRADE_ID,PAY_ID</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;">HASH_CODE,USERNAME,IDCARD_NO,EMAIL,TEL_NO,IM_NO</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 style="color: black;">运用</span>场景<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></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">GMT_CREATE,GMT_MODIFIED</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;">YEAR,MONTH</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;">PRODUCT_STATUS,ORDER_STATUS,IS_DELETE,VIP_FLAG</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;">ORDER_TYPE,IMAGE_TYPE,GENDER,CURRENCY_TYPE</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;">COUNTRY,PROVINCE,CITY</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;">CREATOR,AUDITOR</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;">LEVEL,AMOUNT,SCORE</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;">ADDRESS,COMPANY_NAME,SUMMARY,SUBJECT</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></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">DESCRIPTION,REMARK,MEMO,DETAIL</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;">FILE_CONTENT,EMAIL_CONTENT</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">怎样</span><span style="color: black;">晓得</span></span><span style="color: black;">SQL</span></strong><strong style="color: blue;"><span style="color: black;"><span style="color: black;">是不是</span><span style="color: black;">运用</span>了正确的索引?</span></strong></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;">SQL</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><span style="color: black;">SQL</span><span style="color: black;"><span style="color: black;">欠好</span>办,判断</span><span style="color: black;">SQL</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 style="color: black;">运用</span>,需要到数据库中查看</span><span style="color: black;">SQL</span><span style="color: black;">真实的执行计划,这个<span style="color: black;">专题</span>比较<span style="color: black;">繁杂</span>,详见</span><span style="color: black;">SQL</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;"><strong style="color: blue;"><span style="color: black;">索引对</span><span style="color: black;">DML(INSERT,UPDATE,DELETE)</span></strong><strong style="color: blue;"><span style="color: black;">附加的开销有多少?</span></strong></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></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 style="color: black;">Insert</span><span style="color: black;">性能降低</span><span style="color: black;">56%</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 style="color: black;">Update</span><span style="color: black;">性能降低</span><span style="color: black;">47%</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 style="color: black;">Delete</span><span style="color: black;">性能降低</span><span style="color: black;">29%</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 style="color: black;">IO</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></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">1.2 只<span style="color: black;">经过</span>索引<span style="color: black;">拜访</span>数据</span></strong></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 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><span style="color: black;">IO</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;">select id,name from company where type=2;</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 style="color: black;">SQL</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;">type,id,name</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;">create index my_comb_index on company(type,id,name);</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;">SQL</span><span style="color: black;">就<span style="color: black;">能够</span>直接<span style="color: black;">经过</span></span><span style="color: black;">my_comb_index</span><span style="color: black;">索引返回数据,不需要<span style="color: black;">拜访</span></span><span style="color: black;">company</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 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><span style="color: black;">1000</span><span style="color: black;">页,拼音目录有</span><span style="color: black;">20</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;">50</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;"><strong style="color: blue;"><span style="color: black;">切记,性能优化是无止境的,当性能<span style="color: black;">能够</span>满足<span style="color: black;">需要</span>时<span style="color: black;">就可</span>,不要过度优化。</span></strong><span style="color: black;">在<span style="color: black;">实质</span>数据库中<span style="color: black;">咱们</span>不可能把<span style="color: black;">每一个</span></span><span style="color: black;">SQL</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 style="color: black;">便是</span>那种对核心表<span style="color: black;">拜访</span>量最高且<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;"><strong style="color: blue;"><span style="color: black;">1.3 优化SQL执行计划</span></strong></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;">SQL</span><span style="color: black;">执行计划是关系型数据库最核心的技术之一,它<span style="color: black;">暗示</span></span><span style="color: black;">SQL</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><span style="color: black;">SQL</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;">SQL</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;">SQL</span><span style="color: black;">语法及<span style="color: black;">更加多</span>函数外,还需要有一套优秀的算法库来<span style="color: black;">加强</span></span><span style="color: black;">SQL</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 style="color: black;">日前</span></span><span style="color: black;">ORACLE</span><span style="color: black;">有</span><span style="color: black;">SQL</span><span style="color: black;">执行计划的算法约</span><span style="color: black;">300</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><span style="color: black;">SQL</span><span style="color: black;">执行计划是一个非常<span style="color: black;">繁杂</span>的课题,一个普通</span><span style="color: black;">DBA</span><span style="color: black;">能<span style="color: black;">把握</span></span><span style="color: black;">50</span><span style="color: black;">种就很不错了,就算是资深</span><span style="color: black;">DBA</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 style="color: black;">咱们</span>常用的</span><span style="color: black;">SQL</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;">80%</span><span style="color: black;">的</span><span style="color: black;">SQL</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 style="color: black;">因为</span>篇幅的<span style="color: black;">原由</span>,</span><span style="color: black;">SQL</span><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;"><strong style="color: blue;"><span style="color: black;">2 返回更少的数据</span></strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">2.1 数据分页处理</span></strong></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;"><strong style="color: blue;"><span style="color: black;">2.1.1 客户端(应用程序或浏览器)分页</span></strong></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;">优点:编码简单,减少客户端与应用服务器网络交互次数</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 style="color: black;">需求</span>后续操作流畅,如手机</span><span style="color: black;">GPRS</span><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;"><strong style="color: blue;">2.1.2 应用服务器分页</strong></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 style="color: black;">Java</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;">List list=executeQuery(“select * from employee order by id”);</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">Int count= list.size();</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">List subList= list.subList(10, 20);</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;">SQL</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></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;"><strong style="color: blue;"><span style="color: black;">2.1.3 数据库SQL分页</span></strong></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;">SQL</span><span style="color: black;">分页需要两次</span><span style="color: black;">SQL</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;">SQL</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;">SQL</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></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 style="color: black;">区别</span>,需要两次</span><span style="color: black;">SQL</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;">oracle</span><span style="color: black;">数据库<span style="color: black;">通常</span>采用</span><span style="color: black;">rownum</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;"><strong style="color: blue;"><span style="color: black;">直接<span style="color: black;">经过</span></span><span style="color: black;">rownum</span></strong><strong style="color: blue;"><span style="color: black;">分页:</span></strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">select * from (</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;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;select a.*,rownum rn from</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;">(select * from product a where company_id=? order by status) a</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;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;where rownum&lt;=20)</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">where rn&gt;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 style="color: black;">拜访</span>开销</span><span style="color: black;">=</span><span style="color: black;">索引</span><span style="color: black;">IO+</span><span style="color: black;">索引<span style="color: black;">所有</span>记录结果对应的表数据</span><span style="color: black;">IO</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">采用</span><span style="color: black;">rowid</span></strong><strong style="color: blue;"><span style="color: black;">分页语法</span></strong></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 style="color: black;">ROWID</span><span style="color: black;">,再<span style="color: black;">经过</span></span><span style="color: black;">ROWID</span><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;">create index myindex on product(company_id,status);</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">select b.* from (</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;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;select * from (</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;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;select a.*,rownum rn from</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;">(select rowid rid,status from product a where company_id=? order by status) a</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;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;where rownum&lt;=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;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;where rn&gt;10) a, product b</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">where a.rid=b.rowid;</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 style="color: black;">=</span><span style="color: black;">索引</span><span style="color: black;">IO+</span><span style="color: black;">索引分页结果对应的表数据</span><span style="color: black;">IO</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 style="color: black;">制品</span>有</span><span style="color: black;">1000</span><span style="color: black;">条记录,要分页取其中</span><span style="color: black;">20</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;">50</span><span style="color: black;">个</span><span style="color: black;">IO</span><span style="color: black;">,</span><span style="color: black;">2</span><span style="color: black;">条记录需要</span><span style="color: black;">1</span><span style="color: black;">个表数据</span><span style="color: black;">IO</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 style="color: black;">那样</span>按<span style="color: black;">第1</span>种</span><span style="color: black;">ROWNUM</span><span style="color: black;">分页写法,需要</span><span style="color: black;">550(50+1000/2)</span><span style="color: black;">个</span><span style="color: black;">IO</span><span style="color: black;">,按第二种</span><span style="color: black;">ROWID</span><span style="color: black;">分页写法,只需要</span><span style="color: black;">60</span><span style="color: black;">个</span><span style="color: black;">IO(50+20/2);</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">2.2 只返回需要的字段</span></strong></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 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><span style="color: black;">select * from product where company_id=?;</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 style="color: black;">select id,name from product where company_id=?;</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</span><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;">2</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;">3</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;">4</span><span style="color: black;">、字段变更时提前<span style="color: black;">发掘</span>问题,减少程序</span><span style="color: black;">BUG</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;">5</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></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 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></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 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></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;">T_FILE</span><span style="color: black;">(</span><span style="color: black;">ID,FILE_NAME,FILE_SIZE,FILE_TYPE,FILE_CONTENT</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 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;">T_FILE</span><span style="color: black;">(</span><span style="color: black;">ID,FILE_NAME,FILE_SIZE,FILE_TYPE</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;">T_FILECONTENT</span><span style="color: black;">(</span><span style="color: black;">ID, FILE_CONTENT</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 style="color: black;">经过</span>这种分拆,<span style="color: black;">能够</span>大大提少</span><span style="color: black;">T_FILE</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;">T_FILE</span><span style="color: black;">时性能会更好,当需要<span style="color: black;">查找</span></span><span style="color: black;">FILE_CONTENT</span><span style="color: black;">字段内容时再<span style="color: black;">拜访</span></span><span style="color: black;">T_FILECONTENT</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></p>




情迷布拉格 发表于 2024-8-20 05:32:15

“沙发”(SF,第一个回帖的人)‌

7wu1wm0 发表于 2024-10-9 10:16:19

我深受你的启发,你的话语是我前进的动力。

m5k1umn 发表于 2024-10-22 09:35:48

外链发布社区 http://www.fok120.com/

nykek5i 发表于 2024-11-2 20:00:54

外链论坛的成功举办,是与各位领导、同仁们的关怀和支持分不开的。在此,我谨代表公司向关心和支持论坛的各界人士表示最衷心的感谢!
页: [1]
查看完整版本: 面向程序员的数据库拜访性能优化法则(上)