【发布时间】:2014-04-13 12:42:29
【问题描述】:
我有来自亚马逊的云服务器,一次大约有 20K 访问者,但问题是,我在我的网站中引入了一个部分,它加载的内容取决于页面的高度。
例如如果一个页面的高度为 2000 像素,它会请求大约 40 个帖子,对于少数访问者来说还可以,但对于超过 1000 名访问者(在特定时间)来说就不好了。
当我激活它时它会停止我的服务器并崩溃 mysql ,那么有什么方法可以让我在不丢失访问者的情况下顺利发布帖子?
然后我使用
请求在 5-5 组中发帖function sfpside_bar_post_request(sfpnumber_of_post) {
iCountI = 1;
numbverofrequest = Math.floor(sfpnumber_of_post / 3);
remainderpost = sfpnumber_of_post % 3;
spi = setInterval(function() {
if (iCountI > numbverofrequest)
{
handle_sidepost(remainderpost,iCountI);
clearInterval(spi);
}else{
handle_sidepost(3,iCountI);
}
iCountI++;
}, 1000);
}
function handle_sidepost(num_post,count) {
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
var data = {
wp_nonce: '<?php echo wp_create_nonce('sfp_forum_secret_no_one_knows_LATEST_SIDEBAR'); ?>',
action: 'sfp_sidebar_random_sidebar',
post_num: num_post,
};
jQuery.ajax({
'async': true,
'type': 'POST',
'url': ajaxurl,
'data': data,
'success': function(data)
{
var sidepost = jQuery.parseJSON(data);
sidepostcontent = '<div id="sidesfppost'+count+'">';
for (icount = 0; icount < sidepost.length; icount++) {
sidepostcontent += '<div class="sidebar-one-half"><div class="post_img">';
sidepostcontent += sidepost[icount].image;
sidepostcontent += '</div><article><div class="sflike">';
sidepostcontent += sidepost[icount].like;
sidepostcontent += '</div><div class="conten_wrap left"><h2>';
sidepostcontent += sidepost[icount].title;
sidepostcontent += '</h2></div></article><div class="snippet-box"><div class="left"><p class="sfp_greybtn">';
sidepostcontent += sidepost[icount].greybutton;
sidepostcontent += '</p></div><div class="right">';
sidepostcontent += sidepost[icount].circle;
sidepostcontent += '</div></div></div>';
}
sidepostcontent += "</div>";
jQuery("#sfppost_sidebar_extra_post").append(sidepostcontent);
jQuery(function() {
jQuery("#sidesfppost"+count+" img.imgload").css("width", "54px");
jQuery("#sidesfppost"+count+" img.imgload").css("height", "55px");
jQuery("#sidesfppost"+count+" img.imgload").lazyload({
load: image_loaded_page,
effect: "fadeIn",
failure_limit: 10,
threshold: 200
});
});
},
contentType: "application/x-www-form-urlencoded;charset=ISO-8859-15"
});
}
但它仍然使我的 MySql 崩溃
我用来发帖的WP
if ($_POST['action'] == 'sfp_sidebar_random_sidebar' && wp_verify_nonce($_POST['wp_nonce'], 'sfp_forum_secret_no_one_knows_LATEST_SIDEBAR')):
$args = array('posts_per_page' => $_POST['post_num'], 'orderby' => 'rand', 'post_type' => 'sfp_forum');
$rand_posts = get_posts($args);
$iCount = 0;
$postdata = array();
foreach ($rand_posts as $post_data) :
setup_postdata($post_data);
$postdata[$iCount]['image'] = get_post_home_picture($post_data->post_content, $post_data->ID, $post_data->post_title, $author_username, 'category-small-thumb', 275, 416, true, true);
$postdata[$iCount]['like'] = sfp_like_disklike_arrows("poplr", $post_data->ID, true);
$postdata[$iCount]['title'] = "<a href='" . get_permalink($post_data->ID) . "'>" . sfp_strip_content($post_data->post_title, 50) . "</a>";
$postdata[$iCount]['greybutton'] = sfp_get_grey_button($post_data->ID, $post_data->post_title, true);
$postdata[$iCount]['circle'] = sfp_home_postCircle($post_data->ID, $post_data->comment_count, true);
$iCount++;
endforeach;
wp_reset_postdata();
echo json_encode($postdata);
endif;
die;
【问题讨论】:
-
对每个 Ajax 请求执行的 SQL 是什么?
-
你每次都从数据库中请求
-
是的,但是您每次都要求什么。上面我们只是在客户端上运行了 javascript,而每次运行的实际 sql 上都没有任何内容。
-
@Kickstart 我已经用 WP 请求邮政编码更新了问题
-
如果存在 SQL 问题,它将隐藏在您的代码调用的例程之一中(可能是 get_posts() 或 get_post_home_picture() )。我也会担心这被调用的频率(即,我假设当您根据页面大小改变显示的帖子数量时,您可能正在使用连续滚动,并且可能会遇到重复检查新帖子的问题,具体取决于屏幕尺寸)。
标签: php jquery mysql ajax wordpress