php - Mysql查询重载VPS

标签 php mysql ajax load

我有一个以旧的 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>

更新-感谢大家检查我的问题,我截取了我的数据库表的屏幕截图,请检查它是否有帮助,或者让我知道我还应该提供什么。

http://i.imgur.com/VJSU2.jpg

http://i.imgur.com/5O6T0.jpg

我将不胜感激任何建议/帮助。

感谢和问候 普拉桑特

最佳答案

请检查表“$moFo”中的索引 并检查您正在处理的数据量是多少,如果数据量很大,则归档它们或使用分片。

关于php - Mysql查询重载VPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9701313/

相关文章:

php - MySQL 如何检查字段中的值是整数还是文本

php - 表单验证以及数据库错误

mysql - 如何从 Heroku 访问远程 MySQL 数据库?

javascript - 是否可以使用 JavaScript 写入文件(在磁盘上)?

javascript - 如何将 JQuery 导入 Typescript 文件?

php - 使用 PHP 自动执行 PayPal 付款

php - 当前行和上一行之间的 MySQL View 和数学运算

php - 如何在 Laravel 中通过 join 将一对多关系分组

mysql - 如何从另一个 Excel 报表调用一个 Excel 报表?

python - 使用 Google App Engine (Python) 响应 AJAX GET 和 POST 请求