使用 PHP/MySQL 显示一些统计信息的最有效方法
我需要在我们网站的首页上显示一些基本统计数据,例如博客数量、会员数量和一些计数 - 所有这些都是基本查询。
我更喜欢找到一种方法来运行这些查询,比如每 30 分钟运行一次并存储输出,但我不确定最好的方法,而且我真的不想使用 cron。基本上,我不想每天进行数千次查询只是为了显示这些结果。
关于此类功能的最佳方法有什么想法吗?
提前致谢
I need to show some basic stats on the front page of our site like the number of blogs, members, and some counts - all of which are basic queries.
Id prefer to find a method to run these queries say every 30 mins and store the output but im not sure of the best approach and I don't really want to use a cron. Basically, I don't want to make thousands of queries per day just to display these results.
Any ideas on the best method for this type of function?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不幸的是,
cron
是更好、更可靠的解决方案。如果要将输出存储到磁盘文件中,
您可以随时检查 filemtime 是否少于 30 分钟,
然后继续重新运行昂贵的查询。
Unfortunately,
cron
is better and reliable solution.If you are to store the output into disk file,
you can always check the filemtime is lesser than 30 minutes,
before proceed to re-run the expensive queries.
使用 cron 将此类内容存储在某处并没有任何问题。
如果您正在寻找更复杂的缓存方法,我建议阅读 memcached 或 APC,这两者都可以为您的问题提供解决方案。
There is nothing at all wrong with using a cron to store this kind of stuff somewhere.
If you're looking for a bit more sophisticated caching methods, I suggest reading into memcached or APC, which could both provide a solution for your problem.
Cron Job 是最好的方法,我认为没有其他可行的方法。
Cron Job is best approach nothing else i seen feasible.
你有很多事情要做,我认为好的不是最好的,你可以将你的数据存储在表格上并每 30 分钟显示一次。使用函数
sleep()
我建议你看看wordpress博客系统,特别是插件BuddyPress..
我前段时间也做了同样的事情,每次有人加载页面时,查询都会完成工作并从数据库中检索信息,我记得它类似于
SELECT COUNT(*) FROM my_table
,我得到了我的案例中的帖子数量。
无论如何,方法有很多。祝你好运。
不要忘记 cron 永远是你最好的朋友。
You have many to do this, I think the good not the best, you can store your data on table and display it every 30 min. using the function
sleep()
I recommend you to take a look at wordpress blog system, and specially at the plugin BuddyPress..
I did the same some time ago, and every time someone load the page, the query do the job and retrieve the information from database, I remenber It was something like
SELECT COUNT(*) FROM my_table
and I got the number of posts in my case.
Anyway, there are so many approach. Good Luck.
Dont forget The cron is always your best friend.
使用 cron 是解决该问题的最简单方法。
不使用 cron 的一个很好的理由是——即使没有人请求,你也会生成统计数据。
根据生成数据所需的时间长度(您可能希望跟踪之前的计数,并仅添加时间戳大于之前运行的计数 - 使用适当的索引!),然后您可以在请求时触发此操作进来时数据看起来好像已经过时了。
请注意,您应该将统计信息保留在数据库中,并考虑如何实现互斥体以避免多个请求同时尝试更新缓存。
然而,正确的解决方案是每次添加记录时更新统计信息。除非您的流量非常大,否则开销会很小。虽然“SELECT count(*) FROM some_table”运行得非常快,但如果您不想简单地计算表中的所有行(例如,如果博客和回复是放在同一张表中)。事实上,如果您要将统计更新作为相关表上的触发器来实现,那么您不需要对 PHP 代码进行任何更改。
Using cron is the simplest way to solve the problem.
One good reason for not using cron - you'll be generating the stats even if nobody will request them.
Depending on the length of time it takes to generate the data (you might want to keep track of the previous counts and just add counts where the timestamp is greater than the previous run - with appropriate indexes!) then you could trigger this when a request comes in and the data looks as if it is stale.
Note that you should keep the stats in the database and think about how to implement a mutex to avoid multiple requests trying to update the cache at the same time.
However the right solution would be to update the stats every time a record is added. Unless you've got very large traffic volumes, the overhead would be minimal. While 'SELECT count(*) FROM some_table' will run very quickly you'll obviously run into problems if you don't simply want to count all the rows in a table (e.g. if blogs and replies are held in the same table). Indeed, if you were to implement the stats update as a trigger on the relevant tables, then you wouldn't need to make any changes to your PHP code.