PHP - 在 txt 文件中缓存 MYSQL 查询是一个好习惯吗?

发布于 2024-11-16 11:00:02 字数 447 浏览 0 评论 0原文

我正在建立一个网上商店&尝试通过最小化 MYSQL 查询来提高性能。

通过 txt 文件缓存 mysql 查询然后获取它而不是查询是一个好的做法吗?这就是我正在做的“

  1. php 类将 sql 查询作为字符串,
  2. 则对其进行 md5
  3. 如果这是第一次运行,
  4. ,然后在数据库上执行查询,
  5. 在数组中获取结果,
  6. 序列化数组并存储它作为 md5_Of_Query.txt
  7. 返回 unserialize(file_get_contents(md5_of_Query.txt)) 或 $results 实际查询,具体取决于缓存是否存在并且
  8. 该类也 有效​​。检查 txt 文件的 filemtime() ,如果它大于一小时,则重新执行查询并刷新缓存,

这比每次执行 sql 查询更有效吗?

I'm building an online shop & trying to improve performance by minimising MYSQL queries.

Is it good practice to cache the mysql queries via a txt file and then fetch that instead of the query? This is what I'm doing"

  1. A php class takes the sql query as a string
  2. does an md5 of it
  3. if this is the first time it's run
  4. then perform the query on the database
  5. get the results in an array
  6. serialize the array and store it as md5_Of_Query.txt
  7. return either unserialize(file_get_contents(md5_of_Query.txt)) or $results of actual query, depending on whether or not the cache exists and is valid.
  8. The class also checks the filemtime() of the txt file and if its greater than say, one hour old, then re-perform the query and refresh the cache.

Is this more efficient than doing sql queries every time? Any security issues I'm missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

半﹌身腐败 2024-11-23 11:00:02

如果您进行基准测试,创建唯一散列和执行磁盘 IO 的成本将比简单地从 MySQL 服务器获取的成本更高。

恕我直言,不必费心去达到这个程度。好主意,但 MySQL 已经有了内部缓存和性能调整。

专注于构建您的应用程序,因为“过早的优化是万恶之源”。

If you do a benchmark, the costs of doing creating a unique hash and performing IO to disk will be greater than simply fetching from the MySQL server.

IMHO, don't bother going to the extent. Good thoughts, but MySQL already has internal caching and performance tweak.

Focus on building your application, as "premature optimization is the root of all evil".

迟月 2024-11-23 11:00:02

如果您刚刚启动应用程序,内存缓存是比使用文本文件更快的方法。

http://memcached.org/

文本文件可以完成这项工作,并且您概述的步骤很有意义,但 memcache 会更快,并且为您处理许多繁重的工作。

If you're just starting the application, memcache is a much faster way to go than using text files.

http://memcached.org/

Text files will do the job, and the steps you've outlined make sense, but memcache will be faster and handle a lot of the heavy lifting for you.

我的鱼塘能养鲲 2024-11-23 11:00:02

你的方法看起来有点像把问题从一个角落转移到另一个角落。

引入缓存并不能提高Mysql性能。最好看看哪些查询实际上很慢,然后优化查询。

http://dev.mysql.com/doc/refman /5.1/en/slow-query-log.html

Your method looks a bit like shifting the problem from one corner into the other.

Introducing a cache is not improving Mysql performance. Better look which queries are actually slow and then optimize the queries.

http://dev.mysql.com/doc/refman/5.1/en/slow-query-log.html

彡翼 2024-11-23 11:00:02

如果您想实现缓存并着眼于未来的可扩展性,我建议您设置在数据库上运行查询的 RESTful 服务,然后使用 Web 服务器的 HTTP 缓存功能来缓存结果。步骤如下:

  1. 原始页面需要运行查询
  2. 它生成一个到服务 URL 的 http GET 请求,将参数作为 URL 中的查询参数传递
  3. 位于 URL 的 php 脚本接受查询参数,验证它们,并将它们添加到 MySql 查询
  4. 脚本在数据库上运行查询 脚本
  5. 序列化结果并将其设置为输出
  6. Web 服务器缓存请求的响应,并为将来使用相同 URL 的请求返回该响应
  7. 原始页面使用序列化的结果服务的结果生成HTML

您可以在此处阅读有关使用 Apache 缓存 PHP 的更多信息。您现在所做的与此接近,但是您的应用程序将能够通过基于服务的方法更好地扩展。

If you want to achieve caching with an eye towards future scalability, I would recommend setting up RESTful services that run the queries on the database, and then using the HTTP caching features of your web server to cache the results. The steps would look like:

  1. The original page needs to run a query
  2. It generates an http GET request to the service URL passing the parameters as query parameters in the URL
  3. A php script located at the URL accepts the parameters for the query, validates them, and adds them to a MySql query
  4. The script runs the query on the database
  5. The script serializes the result and sets it as output
  6. The web server caches the response for the request and returns it for future requests with the same URL
  7. The original page uses the serialized results from the service to generate the HTML

You can read more about caching PHP with Apache here. What you are doing now is close to this, but your application will be able to scale better with the service-based approach.

娇纵 2024-11-23 11:00:02

只是想权衡一下我的两分钱,serialworm 和 thephpdeveloper 所说的共享这样一个事实:内存/RAM 比您提出的任何磁盘 IO 绑定操作要快得多。
把尽可能多的内存扔给mysql,你就不需要处理缓存管理,除非你真的需要升级到集群,而这需要其他考虑。
Memcache 使您可以更好地控制缓存管理,因此您需要进行更多编码。

我将首先构建应用程序,然后对其进行压力测试并优化查询,和/或根据需要添加缓存管理。

Just wanted to weigh in my two cents, what serialworm and thephpdeveloper said share the fact that memory/ram is much faster than any disk IO bound operation you come up with.
Throw as much ram as you can to mysql and you won't need to deal with cache management unless you really need to upgrade to a cluster, and that needs other considerations.
Memcache gives you more control over cache management and consequently you need to do more codding.

I would start by building the app then stress test it and optimize queries, and/or add cache management as needed.

吝吻 2024-11-23 11:00:02

需要注意两件事,基准测试分析。您可以有意义地比较 atything 的唯一方法是同时使用这些学科的 2 个指标,使用您当前使用的 mysql 配置、php.ini、httpd.conf、.htaccess、mod 重写内容和许多其他内容将进行基准测试和分析执行任务的代理技术。

Two things 2 look at , benchmarking and profiling. About the only way you can compare meaningfully atything is by using the 2 metrics of these diciplines together, using your currently used mysql config, php.ini, httpd.conf, .htaccess, mod rewrite stuff and many other stuff would be benchmarking and profiling the acting technologies doing the task.

十雾 2024-11-23 11:00:02

这是无意义的,您应该缓存结果

查询组装时间应该可以忽略不计。 (如果不是,那么您没有按照预期使用 SQL,而是生成愚蠢的select,其中智能 join 就可以解决)

从磁盘加载查询显然很容易减慢速度。
(不过,操作系统可能会缓存磁盘 IO,从而很难发现)

无论如何,真正需要花时间的是从数据库中获取结果,然后将它们重新设计到网页槽中一个模板。在你的地方,我会将给定查询的样式结果缓存到磁盘,当被问到时,如果缓存不太旧,我会直接 readfile( ) 他们。

It's nonsensical, you should cache results.

The query assembly time should be pretty negligible. (If it isn't, you're not using SQL as it's supposed to, generating, instead, streams of stupid selects where a smart join would have solved)

Loading from disk the query obviously is prone to slow things down.
(The OS could be caching disk IO, though, and making it hard to spot)

What really should take time anyway, is getting the results out of the DB and then style them back into a web page trough a template. At your place I would cache to disk the styled results of a given query, and when asked, if the cache is not much old, I would directly readfile() them.

云淡风轻 2024-11-23 11:00:02

您是否配置了 MySQL 的查询缓存

Have you configured MySQL's query cache?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文