dqrsceg6279196 2014-12-14 20:59
浏览 27
已采纳

MySQL查询执行率

I am making a live chat, which involves long-polling to receive messages asap. However, since afaik web sockets are not supported cross-platform, and i couldnt find a true long polling chat example, i made a similar thing, which i am actually not sure in.

What i do is i call the script in background, which executes query each 200ms until new message appears. And this is the thing im curious about - do i execute it too often? Lets say i have around 100 ppl using this chat so it will be like 500 queries per second. How will this affect the server perfomance? Posting code im using below:

while (($x < 99) && ($allowloop))
{
  $query = mysqli_query($con, 'SELECT * FROM messages');
  if (mysqli_num_rows($query) == 0)
  {
    usleep(200000);
    $x = $x + 1;
  } else {
    $allowloop = false;
  }
}

#processing results

Any tips and suggestions appreciated

  • 写回答

1条回答 默认 最新

  • duanqiao1947 2014-12-14 21:11
    关注

    You do not want to do this, for the reasons you already know.

    What you need is some change, a client's browser can detect, that signals a new message is present. This could be the presence of a file (with the new message?), or a string in a file.

    So what you could do is:

    1. A new message is available on the server.
    2. The server sets the flag, either a file, or a string in a file.
    3. The client browser polls the small file, checking for the flag.
    4. If the flag is detect, the new message is shown.

    Polling once every few seconds is more than fast enough for chatting. This method means that the clients browser doesn't invoke a MySQL query every time a check is done.

    In all other cases I would use something like: http://nodejs.org

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题