简体   繁体   English

使用 JQuery、PHP 和 MySQL 为实时统计页面优化多个连续的 ajax 调用

[英]Optimizing multiple successive ajax calls with JQuery, PHP, and MySQL for a real-time stats page

I have a PHP script that shows multiple tables of data loaded via a JQuery AJAX call for a tournament scoring website.我有一个 PHP 脚本,它显示了通过 JQuery AJAX 调用加载的多个数据表,用于锦标赛评分网站。 The tables are individual and team scores calculated in real-time.这些表格是实时计算的个人和团队分数。

Each table is checked once every minute for new scores and refreshed if needed.每个表每分钟检查一次以获取新分数,并在需要时刷新。 To do this, each table sends a quick ajax request to the last updated time for the scores... if the time is different than what the table currently has, the table is refreshed with new data.为此,每个表都会向分数的上次更新时间发送一个快速 ajax 请求……如果时间与表当前的时间不同,则使用新数据刷新表。

My problem is that these tournaments can get quite large, with many participants, teams, divisions and scores and loading each table uses server resources.我的问题是这些锦标赛可能会变得非常大,有很多参与者、团队、部门和分数,加载每个表使用服务器资源。 This is no problem if a handful of people are viewing the real-time stats, but once more than about 50 people are connected to this stats page, the server starts lagging heavily.如果少数人正在查看实时统计数据,这没有问题,但是一旦超过 50 人连接到此统计页面,服务器就会开始严重滞后。

I would like to know what I can do to optimize the data, the ajax calls, or the server itself to run as efficiently as possible so that the server doesn't top out so quickly.我想知道我可以做些什么来优化数据、ajax 调用或服务器本身以尽可能高效地运行,以便服务器不会那么快达到顶峰。 I've already combed through the MySQL calls and tried to optimize everything as much as possible, but it doesn't seem to be enough.我已经梳理了 MySQL 调用并尝试尽可能地优化所有内容,但这似乎还不够。

Any ideas would be greatly appreciated!任何想法将不胜感激!

Cache the output, every 1 or 5 mins, refresh the cache...缓存输出,每 1 或 5 分钟,刷新缓存...

So what i mean is, simply write the database output to file, then all requests within that minute uses the file... Every request, check the file's age, if older than 1 or 2 or 5 (examples) mins, delete the cache file and rebuild it所以我的意思是,只需将数据库输出写入文件,然后在那一分钟内的所有请求都使用该文件...每个请求,检查文件的年龄,如果超过 1 或 2 或 5(示例)分钟,则删除缓存文件并重建它

This will allow mysql to rest during that time... the Ajax calls are just what needs to be done... as long as you feel your intervals are necessary这将允许 mysql 在那段时间休息...... Ajax 调用正是需要做的......只要你觉得你的间隔是必要的

Some sample PHP:一些示例 PHP:

$cache_expires = 2; # Minutes
if (file_exists("stats.cache") && filemtime("stats.cache")>time()-$cache_expires*60) {
  # Read cache
  $result = unserialize(file_get_contents("stats.cache"));
  # Display result
} else {
  # Query the database
  $result = get_stats_from_database();
  # Write cache
  file_put_contents("stats.cache",serialize($result));
  # Display result
}

You could use a cache, for example in your architecture memcache would fit very well.您可以使用缓存,例如在您的架构中 memcache 非常适合。 Every time your server receive a request your php app try to get from cache the result, if not found execute the query.每次您的服务器收到请求时,您的 php 应用程序都会尝试从缓存中获取结果,如果未找到,则执行查询。

Now you can choose two different way:现在您可以选择两种不同的方式:

  • easy, wait cache expire time.简单,等待缓存过期时间。 When the cache is expired, server miss its search and a new query is submitted.当缓存过期时,服务器会错过它的搜索并提交一个新的查询。 This is usually a short term cache (seconds or minutes)这通常是一个短期缓存(秒或分钟)
  • hard, cache expire in a very far future, for example hours or even days).硬,缓存在很远的将来到期,例如几小时甚至几天)。 In this case your application refresh the cache content every time the database is updated.在这种情况下,每次更新数据库时,您的应用程序都会刷新缓存内容。 In this case, obviously, almost all your requests hit the cache (best performance).在这种情况下,显然,几乎所有的请求都访问了缓存(最佳性能)。

This example, is based on the easy approach and the cache key is based directly on the submitted SQL.此示例基于简单方法,缓存键直接基于提交的 SQL。

 global $memcache; $memcache = new Memcache; // Gets key / value pair into memcache ... called by mysql_query_cache() function getCache($key) { global $memcache; return ($memcache) ? $memcache->get($key) : false; } // Puts key / value pair into memcache ... called by mysql_query_cache() function setCache($key,$object,$timeout = 60) { global $memcache; return ($memcache) ? $memcache->set($key,$object,MEMCACHE_COMPRESSED,$timeout) : false; } // Caching version of mysql_query() function mysql_query_cache($sql,$linkIdentifier = false,$timeout = 60) { if (($cache = getCache(md5("mysql_query" . $sql))) !== false) { $cache = false; $r = ($linkIdentifier !== false) ? mysql_query($sql,$linkIdentifier) : mysql_query($sql); if (is_resource($r) && (($rows = mysql_num_rows($r)) !== 0)) { for ($i=0;$i<$rows;$i++) { $fields = mysql_num_fields($r); $row = mysql_fetch_array($r); for ($j=0;$j<$fields;$j++) { if ($i === 0) { $columns[$j] = mysql_field_name($r,$j); } $cache[$i][$columns[$j]] = $row[$j]; } } if (!setCache(md5("mysql_query" . $sql),$cache,$timeout)) { // If we get here, there isn't a memcache daemon running or responding } } } return $cache; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM