douwen4125 2011-04-24 23:27
浏览 30

缓存mysql查询结果?

I use a several mysql queries on a page to fetch data from multiple tables using mysqli prepare statements.

Lets say i have like 20 queries from database tables on a page. Since there are so many queries, I would like to improve the performance by storing the results in cache for some minutes? Is it good idea? If yes, how I can store the results using cache for the , for example, following query?

$city = "Amersfoort";

if ($stmt = $mysqli->prepare("SELECT District FROM City WHERE Name=?")) {
    $stmt->bind_param("s", $city);
    $stmt->execute();
    $stmt->bind_result($district);
    $stmt->fetch();
    printf("%s is in district %s
", $city, $district);
    $stmt->close();
}

Thanks.

  • 写回答

1条回答 默认 最新

  • douzi115522 2011-04-24 23:32
    关注

    If the SQL statements are cheap in processing, you probably don't need to cache them. If they require hefty calculations or run on really large datasets, you might need a cache. Remember that the cache needs to be faster than the SQL query.

    There are several ways to cache in PHP:

    • File cache, serialzing the data and storing it into a file named like the hash of the query. Remember that getting cache lifetime, concurrent accesses and so is hard -so consider using a library that does it, i.e. with PEAR's Cache or Cache_Lite
    • memcached
    • shared memory as cache, i.e. by using APC functions like apc_add

    You also might want to cache the complete page or parts of the page instead of single queries.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?