fny5jt9 发表于 2024-8-22 21:12:41

加强网站拜访速度的 SQL 查询优化技巧


    <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;">英文:Delicious Brains,翻译:开源中国</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">www.oschina.net/translate/sql-query-optimization</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz_jpg/DmibiaFiaAI4B1gnZEibCXyQrWdY6a6Pk4vcniasMzg1A2bHoHvlxh7wMztYLuxeic03aPhbOK9ZibdfNsYBLQFwJPObg/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 style="color: black;">帮忙</span>网站从Google 上<span style="color: black;">加强</span>排名,<span style="color: black;">能够</span><span style="color: black;">帮忙</span>网站<span style="color: black;">增多</span>转化率。<span style="color: black;">倘若</span>你看过网站性能优化方面的<span style="color: black;">文案</span>,例如</span><span style="color: black;">设置服务器的最佳实现</span><span style="color: black;">、到</span><span style="color: black;">干掉慢速代码</span><span style="color: black;">以及&nbsp;</span><span style="color: black;"><span style="color: black;">运用</span>CDN 加载<span style="color: black;">照片</span></span><span style="color: black;">,就认为你的 WordPress 网站<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>动态数据库驱动的网站,例如WordPress,你的网站可能依然有一个问题亟待<span style="color: black;">处理</span>:数据库<span style="color: black;">查找</span>拖慢了网站<span style="color: black;">拜访</span>速度。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在这篇<span style="color: black;">文案</span>中,我将介绍<span style="color: black;">怎样</span>识别<span style="color: black;">引起</span>性能<span style="color: black;">显现</span>问题的<span style="color: black;">查找</span>,<span style="color: black;">怎样</span>找出它们的问题所在,以及快速修复这些问题和其他加快<span style="color: black;">查找</span>速度的<span style="color: black;">办法</span>。我会把门户网站&nbsp;deliciousbrains.com <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;"><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;">处理慢SQL<span style="color: black;">查找</span>的<span style="color: black;">第1</span>步是找到慢<span style="color: black;">查找</span>。Ashley<span style="color: black;">已然</span>在之前的</span><span style="color: black;">博客</span><span style="color: black;">里面赞扬了调试插件</span><span style="color: black;">Query Monitor</span><span style="color: black;">,<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>原件(插件,主题,WordPress核)过滤这些<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;"><img src="http://mmbiz.qpic.cn/mmbiz_png/DmibiaFiaAI4B1gnZEibCXyQrWdY6a6Pk4vcrV8HibhVQmA5YJytPXC8s02gReKwibg3JEu3icMeEkxhgG2Vcl7NWOiamw/640?wx_fmt=png&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><span style="color: black;">MySQL Slow Query Log</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 style="color: black;">亦</span>应该关闭。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">理解</span></strong></span></h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">一旦你找到了一个你要花很大代价找到的<span style="color: black;">查找</span>,<span style="color: black;">那样</span>接下来<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>找到了一个要执行8秒的<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>WooCommerce和定制版的WooCommerce软件插件来运行<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>客户号的客户的所有订阅。WooCommerce是一个稍微<span style="color: black;">繁杂</span>的数据模型,即使订单以自定义的类型存储,用户的ID(商店为每一个用户创建的WordPress)<span style="color: black;">亦</span><span style="color: black;">无</span>存储在post_author,而是<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>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">MySQL是你的<span style="color: black;">伴侣</span></span></strong></span></h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">MySQL有一个很方便的语句</span><span style="color: black;">DESCRIBE</span><span style="color: black;">,它<span style="color: black;">能够</span>输出表结构的信息,<span style="color: black;">例如</span>字段名,数据类型等等。<span style="color: black;">因此</span>,当你执行DESCRIBE wp_postmeta;你将会看到如下的结果:</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/DmibiaFiaAI4B1gnZEibCXyQrWdY6a6Pk4vcdr8gLDp2Cxgb7OQBwGxJX5bibJ7JEoO7MTYdGlictpMCDoY6BTeKAmew/640?wx_fmt=png&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>DESCRIBE语句<span style="color: black;">能够</span>放在SELECT, INSERT, UPDATE, REPLACE 和 DELETE语句前边<span style="color: black;">运用</span>吗?更为人们所熟知的是他的同义词 EXPLAIN ,并将<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></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/DmibiaFiaAI4B1gnZEibCXyQrWdY6a6Pk4vcq6ZN1O7aQqiaOm238cl8metqTibGTjvc6MHICElTibUPrIcdialoic3C7RA/640?wx_fmt=png&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>SitePoint总结了一个</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;">最重要的字段是type,它描述了一张表是怎么<span style="color: black;">形成</span>的。<span style="color: black;">倘若</span>你想看<span style="color: black;">所有</span>的内容,那就<span style="color: black;">寓意</span>着MySQL要从内存读取整张表,<span style="color: black;">增多</span>I/O的速度并在CPU上加载。这种被<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;">rows字段<span style="color: black;">亦</span>是一个好的标识,标识着MySQL将要不得不做的事情,它<span style="color: black;">表示</span>了结果中<span style="color: black;">查询</span>了多少行。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">Explain<span style="color: black;">亦</span>给了<span style="color: black;">咱们</span><span style="color: black;">非常多</span><span style="color: black;">能够</span>优化的信息。例如,pm2表((wp_postmeta),告诉<span style="color: black;">咱们</span>是Using filesort,<span style="color: black;">由于</span><span style="color: black;">咱们</span><span style="color: black;">运用</span>了&nbsp;ORDER BY语句对结果进行了排序。<span style="color: black;">倘若</span><span style="color: black;">咱们</span>要对<span style="color: black;">查找</span>结果进行分组,这将会给执行<span style="color: black;">增多</span>开销。</span></p>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">可视化<span style="color: black;">科研</span></span></strong></span></h3>
    <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><span style="color: black;">MySQL Workbench</span><span style="color: black;">是<span style="color: black;">另一</span>一个方便,免费的工具。将数据库用MySQL5.6及其以上的版本打开,EXPLAIN的结果<span style="color: black;">能够</span>用JSON格式输出,<span style="color: black;">同期</span>MySQL Workbench将JSON转换成可视化执行语句:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz_png/DmibiaFiaAI4B1gnZEibCXyQrWdY6a6Pk4vcI5YFkJ7496ugIUiaGe9BI871zt25mdpdMVg9fAWLl7MeVGa5t35TIWQ/640?wx_fmt=png&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>马上看到,连接wp_woocommerce_software_licences(别名l)的表有严重的问题。</span></p>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">处理</span></span></strong></span></h3>
    <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 style="color: black;">所有</span>表浏览的<span style="color: black;">查找</span>,<span style="color: black;">由于</span>他<span style="color: black;">运用</span>非索引字段order_id去连接wp_woocommerce_software_licences表和wp_posts表。这<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>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">索引</span></strong></span></h3>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">order_id在表中是一个相当重要的标志性数据,<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;">,除此之外,MySQL将逐字扫描表的每一行,直到找到<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;">CREATE INDEX order_id ON wp_woocommerce_software_licences(order_id)</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="http://mmbiz.qpic.cn/mmbiz_png/DmibiaFiaAI4B1gnZEibCXyQrWdY6a6Pk4vcia2HkWmicjylUePCGwibKqQYweBr4RII3icErnTicT1XibV8wz3ZvPLqW3uw/640?wx_fmt=png&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>的时间缩短了5s.</span></p>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">认识</span>你的<span style="color: black;">查找</span>语句</span></strong></span></h3>
    <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>每一个join,每一个子<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>把licenses 表和posts 表<span style="color: black;">经过</span>order_id 连接起来<span style="color: black;">同期</span>限制post type 为shop_order。这是为了<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>这是一个关于安全的赌注,在posts 表中software license 行是<span style="color: black;">经过</span>order_id 来跟 WooCommerce order <span style="color: black;">关联</span>联的,这在PHP 插件代码中是强制的。让<span style="color: black;">咱们</span>移除join 来<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;"><img src="http://mmbiz.qpic.cn/mmbiz_png/DmibiaFiaAI4B1gnZEibCXyQrWdY6a6Pk4vcWw2SnEf4XdgQ2ojiaxZWSUZd18uDv1vJy3E58yp8icVORrLQGhKhuCuA/640?wx_fmt=png&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>3 秒了。</span></p>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">缓存一切数据</span></strong></span></h3>
    <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>MySQL<span style="color: black;">查找</span>缓存,<span style="color: black;">那样</span>你应该开启缓存。开启缓存<span style="color: black;">寓意</span>着MySQL 会把所有的语句和语句执行的结果<span style="color: black;">保留</span>下来,<span style="color: black;">倘若</span>随后有一条与缓存中完全相同的语句需要执行,<span style="color: black;">那样</span>MySQL 就会返回缓存的结果。缓存不会过时,<span style="color: black;">由于</span>MySQL 会在表数据更新后刷新缓存。</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>语句执行了四次,尽管有MySQL<span style="color: black;">查找</span>缓存很好,<span style="color: black;">然则</span>在一个请求中重复读取数据库的数据是应该完全避免的。你的PHP 代码中的静态缓存很简单并且<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;">class</span>&nbsp;<span style="color: black;">WC_Software_Subscription</span>&nbsp;<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;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">protected</span>&nbsp;<span style="color: black;">static</span>&nbsp;<span style="color: black;">$</span><span style="color: black;">subscriptions</span>&nbsp;<span style="color: black;">=</span>&nbsp;<span style="color: black;">array</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;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">public</span>&nbsp;<span style="color: black;">static</span>&nbsp;<span style="color: black;">function</span>&nbsp;<span style="color: black;">get_user_subscriptions</span><span style="color: black;">(</span>&nbsp;<span style="color: black;">$</span><span style="color: black;">user</span><span style="color: black;">_</span>id&nbsp;<span style="color: black;">)</span>&nbsp;<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;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">if</span>&nbsp;<span style="color: black;">(</span>&nbsp;<span style="color: black;">isset</span><span style="color: black;">(</span>&nbsp;<span style="color: black;">static</span><span style="color: black;">::</span><span style="color: black;">$</span><span style="color: black;">subscriptions</span><span style="color: black;">[</span>&nbsp;<span style="color: black;">$</span><span style="color: black;">user</span><span style="color: black;">_</span>id&nbsp;<span style="color: black;">]</span>&nbsp;<span style="color: black;">)</span>&nbsp;<span style="color: black;">)</span>&nbsp;<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;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">return</span>&nbsp;<span style="color: black;">static</span><span style="color: black;">::</span><span style="color: black;">$</span><span style="color: black;">subscriptions</span><span style="color: black;">[</span>&nbsp;<span style="color: black;">$</span><span style="color: black;">user</span><span style="color: black;">_</span>id&nbsp;<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;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<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;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">global</span>&nbsp;<span style="color: black;">$</span><span style="color: black;">wpdb</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;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$</span><span style="color: black;">sql</span>&nbsp;<span style="color: black;">=</span>&nbsp;<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;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$</span><span style="color: black;">results</span>&nbsp;<span style="color: black;">=</span>&nbsp;<span style="color: black;">$</span><span style="color: black;">wpdb</span><span style="color: black;">-&gt;</span><span style="color: black;">get_results</span><span style="color: black;">(</span>&nbsp;<span style="color: black;">$</span><span style="color: black;">sql</span><span style="color: black;">,</span>&nbsp;<span style="color: black;">ARRAY</span><span style="color: black;">_</span>A&nbsp;<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;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">static</span><span style="color: black;">::</span><span style="color: black;">$</span><span style="color: black;">subscriptions</span><span style="color: black;">[</span>&nbsp;<span style="color: black;">$</span><span style="color: black;">user</span><span style="color: black;">_</span>id&nbsp;<span style="color: black;">]</span>&nbsp;<span style="color: black;">=</span>&nbsp;<span style="color: black;">$</span><span style="color: black;">results</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;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">return</span>&nbsp;<span style="color: black;">$</span><span style="color: black;">results</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;">&nbsp;&nbsp;&nbsp;&nbsp;<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>地说是实例化对象有一个生命周期。<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;"><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 style="color: black;">查找</span>的执行速度。 <span style="color: black;">咱们</span><span style="color: black;">查找</span>的最慢的部分是从客户ID到<span style="color: black;">制品</span>ID再到加入表格所做的工作,<span style="color: black;">咱们</span>必须为<span style="color: black;">每一个</span>客户做到。<span style="color: black;">咱们</span>是不是<span style="color: black;">能够</span>在需要的时候抓取客户的数据?<span style="color: black;">倘若</span>是那样,那<span style="color: black;">咱们</span>就只需要加入一次。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">您<span style="color: black;">能够</span><span style="color: black;">经过</span>创建数据表来存储许可数据,以及所有许可用户标识和<span style="color: black;">制品</span>标识符来对数据进行非规范化(反规范化)处理,并针对特定客户进行<span style="color: black;">查找</span>。 您需要<span style="color: black;">运用</span>INSERT / UPDATE / DELETE上的MySQL触发器来重建表格(<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>连接在MySQL中减慢了<span style="color: black;">查找</span>速度,<span style="color: black;">那样</span>将<span style="color: black;">查找</span>分解为两个或<span style="color: black;">更加多</span>语句并在PHP中单独执行它们可能会更快,<span style="color: black;">而后</span><span style="color: black;">能够</span>在代码中收集和过滤结果。 Laravel <span style="color: black;">经过</span>预加载在 Eloquent 中就做了类似的事情。</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>的自定义帖子类型,WordPress可能会在wp_posts表上减慢<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;"><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>从8秒降低到2秒,并且将<span style="color: black;">查找</span>次数从4次减少到1次。需要说明的是,这些<span style="color: black;">查找</span>时间是在<span style="color: black;">咱们</span><span style="color: black;">研发</span>环境运行时记录的 ,生产环境速度会更快。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这对<span style="color: black;">跟踪</span><span style="color: black;">查找</span>缓慢及其修复等问题是一个有用的指南。 优化<span style="color: black;">查找</span>看起来可能像一个可怕的任务,但只要你尝试一下,并取得<span style="color: black;">有些</span>初步的胜利,你就会<span style="color: black;">起始</span>找到错误,并<span style="color: black;">期盼</span>做出进一步改善。</span></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></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;"><strong style="color: blue;"><span style="color: black;">关注「数据库<span style="color: black;">研发</span>」,<span style="color: black;">提高</span> DB 技能</span></strong></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_png/DmibiaFiaAI4B2sOp0oeiajw1p0x744u9Hib4rSfhhuv5HI8N8WqUoJdxQNWUfDIWtgOicy5T1XmVl9XEblJf30ovzcQ/640?wx_fmt=png&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;"><img src="https://mmbiz.qpic.cn/mmbiz_jpg/FoiciaVBBCfia4PkmTUFBP8fpd2PLlCx8iaJkRZ2iaj7seE62y9yicOlk9jFMWibvEfPG5PBFtORnqCoPP3EiajlyK1Xnw/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;"><strong style="color: blue;"><span style="color: black;">淘口令</span></strong><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>¥极客T恤¥抢先预览(长按复制整段文案,打开手机淘宝<span style="color: black;">就可</span>进入活动内容)</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">近期,北京地区正常发货,但派件时间有所延长。</p>




7wu1wm0 发表于 2024-10-28 21:12:01

期待你更多的精彩评论,一起交流学习。

m5k1umn 发表于 2024-10-29 10:05:56

大势所趋,用于讽刺一些制作目的就是为了跟风玩梗,博取眼球的作品。

7wu1wm0 发表于 2024-11-14 05:35:58

论坛的成功是建立在我们诚恳、务实、高效、创新和团结合作基础上,我们要把这种精神传递下去。
页: [1]
查看完整版本: 加强网站拜访速度的 SQL 查询优化技巧