duanheye7909 2009-10-22 12:03
浏览 83
已采纳

缓存表结果可以获得更好的性能......怎么样?

First of all, the website I run is hosted and I don't have access to be able to install anything interesting like memcached.

I have several web pages displaying HTML tables. The data for these HTML tables are generated using expensive and complex MySQL queries. I've optimized the queries as far as I can, and put indexes in place to improve performance. The problem is if I have high traffic to my site the MySQL server gets hammered, and struggles.

Interestingly - the data within the MySQL tables doesn't change very often. In fact it changes only after a certain 'event' that takes place every few weeks.

So what I have done now is this:

  1. Save the HTML table once generated to a file
  2. When the URL is accessed check the saved file if it exists
  3. If the file is older than 1hr, run the query and save a new file, if not output the file

This ensures that for the vast majority of requests the page loads very fast, and the data can at most be 1hr old. For my purpose this isn't too bad.

What I would really like is to guarantee that if any data changes in the database, the cache file is deleted. This could be done by finding all scripts that do any change queries on the table and adding code to remove the cache file, but it's flimsy as all future changes need to also take care of this mechanism.

Is there an elegant way to do this?

I don't have anything but vanilla PHP and MySQL (recent versions) - I'd like to play with memcached, but I can't.

  • 写回答

4条回答 默认 最新

  • douhuang3833 2009-10-22 12:09
    关注

    Ok - serious answer.

    If you have any sort of database abstraction layer (hopefully you will), you could maintain a field in the database for the last time anything was updated, and manage that from a single point in your abstraction layer.

    e.g. (pseudocode): On any update set last_updated.value = Time.now()

    Then compare this to the time of the cached file at runtime to see if you need to re-query.

    If you don't have an abstraction layer, create a wrapper function to any SQL update call that does this, and always use the wrapper function for any future functionality.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c