简体   繁体   English

PHP,我应该使用json而不是mysql吗?

[英]Php, should i use json instead of mysql?

I am thinking about creating a json/xml/text when something is inserted to mysql database, so that php page will read this instead of mysql queries. 我正在考虑将某些内容插入mysql数据库时创建json / xml / text,以便php页面将读取此内容,而不是mysql查询。 When a data has updated in mysql database, it will update json/xml files too. 当数据已经在mysql数据库中更新后,它也会更新json / xml文件。 Is this a good idea? 这是一个好主意吗? It seems, this will has less mysql queries so I think this can be good. 看来,这将减少mysql查询,因此我认为这可能很好。

it seems, this will has less mysql queries so i think this can be good. 看来,这将减少mysql查询,所以我认为这可能很好。

It depends. 这取决于。 Probably not. 可能不是。

What you plan to do is a form of caching . 您打算做的是一种缓存形式。 If the queries used on a page are really, really complex and take a long time when benchmarking, it is sometimes a good thing to store the result in a cached file, and updating that if data changes. 如果页面上使用的查询确实非常复杂,并且在进行基准测试时需要花费很长时间,那么将结果存储在缓存的文件中并在数据更改时进行更新有时是一件好事。

But having some simple SELECT queries running on every request is nothing to worry about performance-wise. 但是,在每个请求上运行一些简单的SELECT查询,无需担心性能方面的问题。 If you're not seeing any actual performance issues, you're probably best off sticking with SQL queries. 如果您没有看到任何实际的性能问题,那么最好还是坚持使用SQL查询。 A properly indexed database is really, really fast. 正确索引的数据库确实非常快。

What's your goal? 你的目标是什么? Gain more performance? 获得更多性能?

1st rule for performance optimization: Know your bottleneck! 性能优化的第一条规则:了解您的瓶颈! Don't optimize a black box. 不要优化黑匣子。 Do benchmarks before and after each optimization. 在每次优化前后都要进行基准测试。

I doubt, that your database is your current bottleneck. 我怀疑您的数据库是您当前的瓶颈。 Even it is, there are better ways to improve the performance, ie avoid statements, instead of storing the results in a json/text file. 即使是这样,也有更好的方法来提高性能,即避免使用语句,而不是将结果存储在json /文本文件中。

  1. Use MySQL Query cache 使用MySQL查询缓存

    MySQL is really good in caching results. MySQL在缓存结果方面确实非常出色。 You shouldn't take care of caching results yourself. 您不应该自己照顾缓存结果。 Important here: enough RAM. 重要的是:足够的RAM。 Checkout the MySQL docs for more information: http://dev.mysql.com/doc/refman/5.1/en/query-cache.html 查看MySQL文档以获取更多信息: http : //dev.mysql.com/doc/refman/5.1/en/query-cache.html

  2. Implement more cache layer 实施更多的缓存层

    To gain more performance you could implement a second cache layer before the MySQL database to avoid even the call to the database. 为了获得更高的性能,您可以在MySQL数据库之前实现第二个缓存层,甚至避免调用数据库。 You could store the initial results (at the first call you need to get the result from the DB, of course) in a non persistent, awesome fast way using memcached or other NoSQL solutions (Redis, MongoDB to name a few). 您可以使用memcached或其他NoSQL解决方案(Redis,MongoDB等)以一种非持久的,很棒的快速方式存储初始结果(当然,在第一次调用时,您需要从数据库中获取结果)。

  3. Know your bottleneck 知道你的瓶颈

    As I said before, the most important thing in performance optimization is to know where your bottleneck is, and how much you won with a optimization. 正如我之前说过的,性能优化中最重要的事情是知道瓶颈在哪里,以及优化能为您带来多少收益。 At least it's a very good feeling to stat: Hey,... after we did XYZ the system is 6000% faster. 至少感觉很不错:嘿,...完成XYZ之后,系统速度提高了6000%。 :) :)

Maybe your database isn't the thing which needs to be speeded up right now. 也许您的数据库不是现在需要加快的事情。 Perhaps installing APC will speed up your application. 也许安装APC将加快您的应用程序。 :) :)

But coming back to you question: If you really want to store those results in JSON/text file, you should store the files in a tmpfs / RAM filesystem - so that they can be written and read very fast. 但是回到您的问题:如果您真的要将这些结果存储在JSON /文本文件中,则应该将这些文件存储在tmpfs / RAM文件系统中-以便可以非常快速地写入和读取它们。

This really depends I suppose. 我想这真的取决于。 If you are writing a page to be loaded with AJAX, and you have settings or configurations that are very rarely changed but needed to be loaded each time, then go ahead and write it to a json file. 如果您正在编写要用AJAX加载的页面,并且您的设置或配置很少更改,但是每次都需要加载,那么请继续并将其写入json文件。

If the content you are loading is dynamic or you are loading it via server side scripts, then MySQL will be a must. 如果您要加载的内容是动态的,或者您是通过服务器端脚本加载的,则必须使用MySQL。

Beware: If you start writing settings or data to a json file, you may encounter a million issues with security and permissions. 请注意:如果您开始将设置或数据写入json文件,则可能会遇到一百万个关于安全性和权限的问题。

Depends on your application size. 取决于您的应用程序大小。 If is about few things to interact with database. 如果与数据库交互的东西很少。 you can do with JSON storing. 您可以使用JSON存储。

But If database size is large and want to use RDBMS concepts then it will be difficult to manage. 但是,如果数据库很大,并且想使用RDBMS概念,那么将很难管理。

I would like you to go through this article: 我希望您阅读这篇文章:

MySQL-vs-JSON-file-data-storing MySQL-vs-JSON-文件数据存储

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

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