【发布时间】: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 服务器以便它可以快速处理这么多请求的建议? 或者如何优化这个功能的代码? 提前谢谢!
【问题讨论】: