简体   繁体   English

用于数据检索的 MySQL 或 JSON

[英]MySQL or JSON for data retrieval

So, I have situation and I need second opinion.所以,我有情况,我需要第二意见。 I have database and it' s working great with all foreign keys, indexes and stuff, but, when I reach certain amount of visitors, around 700-800 co-current visitors, my server hits bottle neck and displays "Service temporarily unavailable."我有数据库,它可以很好地处理所有外键、索引和其他东西,但是,当我达到一定数量的访问者,大约 700-800 名同时访问者时,我的服务器遇到瓶颈并显示“服务暂时不可用”。 So, I had and idea, what if I pull data from JSON instead of database.所以,我有想法,如果我从 JSON 而不是数据库中提取数据会怎样。 I mean, I would still update database, but on each update I would regenerate JSON file and pull data from it to show on my homepage.我的意思是,我仍然会更新数据库,但在每次更新时,我都会重新生成 JSON 文件并从中提取数据以显示在我的主页上。 That way I would not press my CPU to hard and I would be able to make some kind of cache on user-end.这样我就不会用力压我的 CPU,我将能够在用户端制作某种缓存。

What you are describing is caching.您所描述的是缓存。

Yes, it's a common optimization to avoid over-burdening your database with query load.是的,这是一种常见的优化,可以避免查询负载使数据库负担过重。

The idea is you store a copy of data you had fetched from the database, and you hold it in some form that is quick to access on the application end.这个想法是您存储从数据库中获取的数据的副本,并以某种形式保存它,以便在应用程序端快速访问。 You could store it in RAM, or in a JSON file.您可以将其存储在 RAM 或 JSON 文件中。 Some people operate a Memcached or Redis in-memory database as a shared resource, so your app can run many processes or threads that access the same copy of data in RAM.有些人将 Memcached 或 Redis 内存数据库作为共享资源运行,因此您的应用程序可以运行多个进程或线程来访问 RAM 中的相同数据副本。

It's typical that your app reads some given data many times for every single time it updates the data.通常,您的应用每次更新数据时都会多次读取某些给定数据。 The greater this ratio of reads to writes, the better the savings in terms of lightening the load on your database.读取与写入的比率越大,在减轻数据库负载方面的节省就越好。

It can be tricky, however, to keep the data in cache in sync with the most recent changes in the database.然而,将缓存中的数据与数据库中的最新更改保持同步可能会很棘手。 In other words, how do all the cache copies know when they should re-fetch the data from the database?换句话说,所有缓存副本如何知道何时应该从数据库中重新获取数据?

There's an old joke about this:有一个关于这个的老笑话

There are only two hard things in Computer Science: cache invalidation and naming things.计算机科学中只有两件难事:缓存失效和命名。

— Phil Karlton — 菲尔·卡尔顿

So after another few days of exploring and trying to get the right answer this is what I have done.所以经过几天的探索并试图得到正确的答案,这就是我所做的。 I decided to create another table, instead of JSON, and put all data, that was suposed to go in JSON file, in the table.我决定创建另一个表,而不是 JSON,并将所有应该放在 JSON 文件中的数据放在表中。

WHY?为什么?

Number one reason is MySQL has ability to lock tables while they're being updated, JSON has not.第一个原因是 MySQL 能够在更新表时锁定表,而 JSON 则没有。

Number two is that I will downgrade from few dozens of queries to just one, simplest, query: SELECT * FROM table.第二个是我将从几十个查询降级到一个,最简单的查询:SELECT * FROM table。

Number three is that I have better control over content this way.第三,我可以通过这种方式更好地控制内容。

Number four, while I was searching for answer I found out that some people had issues with JSON availability if a lot of co-current connections were making request for same JSON, I would never have a problem with availability.第四,当我在寻找答案时,我发现有些人在 JSON 可用性方面存在问题,如果很多并发连接请求相同的 JSON,我永远不会遇到可用性问题。

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

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