我有一个以旧的 php/mysql 风格编写的基于文本的游戏网站(黑手党游戏)。我对 PHP/MYSQL 的了解很少,但我正在学习它。所以我遇到了一个文件的问题,该文件通过ajax每5秒重新加载一次,它包含一些Mysql查询,用于检查消息、论坛消息、传输、攻击等,并且根据检查,如果用户收到任何新消息,它会向用户显示警报,论坛消息、传输、攻击等。我的网站现在正在使用 VPS,如果我将刷新率设置为 5 秒,它会在几分钟内覆盖 VPS,因此我必须将刷新时间设置为 20 秒或更长。我想知道查询是否有任何问题或有任何优化查询/php 代码的建议。下面是我的文件 ajax.php 的代码,需要每 5 秒重新加载一次
<?php
include("funcs.php");
global $tab, $time, $id, $tru, $old;
$round=$_GET['tru'];
$tO=0;
$moFo='r'.$round.'_mafiosi';
$brd=$tab['board'];
$query="select msg,atk,ivt,transf from $moFo where id='$id'";
$result=mysql_query($query);
$ans=mysql_fetch_assoc($result);
foreach($ans as $key=>$value)
{
$tO+=$value;
}
$rtn='#'.$mafioMsg;
echo "$tO$rtn";
?>
#
下面是我正在使用的 jquery/javascript :
<script type="text/javascript" >
var onOff=false;
var replyText='';
window.onload=mainF;
function hideRedNotif()
{
document.getElementById('redNotif').style.display='none';
}
function mainF()
{
fetchNotif();
Updtr=window.setInterval(fetchNotif,25000);
}
function toggleNotif()
{
document.getElementById('redNotif').style.display='none';
if(onOff==false)
{
document.getElementById('parentReply').style.display='';
onOff=true;
}
else
{
document.getElementById('parentReply').style.display='none';
onOff=false;
}
}
function getAjxObject()
{
try {
var o=new XMLHttpRequest();
}
catch(exception)
{
var o=new ActiveXObject('Microsoft.XMLHTTP');
}
return o;
}
function fetchNotif()
{
roundN=document.getElementById('roundName').value;
var o=getAjxObject();
o.open('GET','notifAjx.php?openSes=in&&tru='+roundN,true);
o.onreadystatechange=execute;
o.send();
function execute()
{
if(o.readyState==4 && o.status==200)
{
var countF=0;
resp=o.responseText;
rsp=resp.split('#');
dom=document.getElementById('notifM');
dom.innerHTML=rsp[0];
//document.getElementById('chatRoller').innerHTML=rsp[1];
//if(rsp[1]!=replyText )
//{
//document.getElementById('redNotif').style.display='';
//replyText=rsp[1];
//}
}
}
}
function sendReply()
{
var o2=getAjxObject();
roundN=document.getElementById('roundName').value;
m=document.getElementById('replyText').value;
msg='&&reply=1&&msg='+m;
url='notifAjx.php?tru='+roundN+msg;
o2.open('GET',url,true);
document.getElementById('replyText').value='';
o2.onreadystatechange=execute;
o2.send();
function execute()
{
if(o2.readyState==4 && o2.status==200)
{
}
}
}
</script>
更新-感谢大家检查我的问题,我截取了我的数据库表的屏幕截图,请检查它是否有帮助,或者让我知道我还应该提供什么。
我将不胜感激任何建议/帮助。
感谢和问候 普拉桑特
最佳答案
请检查表“$moFo”中的索引 并检查您正在处理的数据量是多少,如果数据量很大,则归档它们或使用分片。
关于php - Mysql查询重载VPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9701313/