【问题标题】:Many queries on wordpress and slow server response关于 wordpress 的许多查询和缓慢的服务器响应
【发布时间】:2018-07-07 11:52:41
【问题描述】:

我在 Scaleway 服务器上安装了 wordpress 3.8.6(4 个 X86 64 位内核, 4GB 内存,200Mbit/s 不限速) Apache/2.4.18 + PHP 5.6.36 + MySQL Percona 服务器 5.6.40-84.0

主页有4355个查询,是服务器响应慢(17.9s)的原因

发出这些请求的函数如下所示:

        <?php 
        $categories = array(24,121,123,124,127,19,20,91,92,21,22,23,89,29,30,31,38,55,52,65,87,99,120,108,113,119,128,206,159,137,142,139,143,146,147,150,164,153,154,174,175,177,184,186,191,196,198,200,201,204);
        $cats = get_terms('catalog');
        foreach ($cats as $cat) {
            if (in_array($cat->term_id, $categories)) {

                $image = get_field('photo', 'catalog_'.$cat->term_id);

                $query = new WP_Query( 
                     array ( 'tax_query' => array(
                            array(
                                'taxonomy' => 'catalog',
                                'field'    => 'id',
                                'terms'    => $cat->term_id
                            )
                        )) 
                );
                $minprice=0;
                if ( $query->have_posts() ) {
                    while ( $query->have_posts() ) {
                        $query->the_post();
                        $fields = get_fields( get_the_ID() );
                        if ((int)$fields['price'] < $minprice && (int)$fields['price']>0 || $minprice==0) {
                            $minprice = (int)$fields['price'];
                        }

                    }
                }
                ?>
                    <a class="cat-item" href="<?=get_term_link($cat);?>" title="">
                    <img src="<?=$image['sizes']['main-category']?>" width="178" height="158" alt="<?=$cat->name?>">
                    <div class="cat-title" style="bottom: 26px;"><?=$cat->name?></div><div style="text-align:center; margin-top: 10px;">Цена от <?=$minprice?> руб.</div></a>
                <?php
            }


        }

    ?>

问题! 如何在不更改输出功能的情况下减少服务器的响应? 是否有关于设置 MySQL 服务器以便它可以快速处理这么多请求的建议? 或者如何优化这个功能的代码? 提前谢谢!

【问题讨论】:

    标签: mysql wordpress pagespeed


    【解决方案1】:

    有一个名为query-monitor 的插件。您可以通过此插件监控查询。

    要减少查询,您可以使用 wordpress Transients_API

    或者您可以使用任何兑现插件,例如W3 Total Cache

    【讨论】:

    • 我在哪个页面发现了问题。大量查询创建了这个函数——目录中商品最低价格的计算和输出:$minprice=0; ...等缓存已安装
    • youtube.com/watch?v=bYxZEHGV_eE 观看此视频。它可能会有所帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-18
    • 2017-07-01
    • 2012-01-15
    • 2019-02-03
    • 1970-01-01
    • 2022-06-12
    相关资源
    最近更新 更多