简体   繁体   English

将小数据写入文件并从中读取或查询数据库

[英]Writing small data to file and reading from it or query the database

I have a situation where I would have to query the database for some data which is the same for all users and changes daily, so I figured I could make a file in which I would save this data (once per day) and then load it from that file each time a user visits my site. 我遇到这样的情况,我必须查询数据库以获取所有用户相同的数据,并且每天都要更改,因此我想可以制作一个文件来保存此数据(每天一次),然后加载它每次用户访问我的网站时都从该文件访问。

Now, I know that this is a common practice (caching) when the requests from database are big but the data I'm about to write to the file is a simple 3 digit number, so my question is should this still be faster or is it just an overkill and I should stick with the database query? 现在,我知道当数据库的请求很大但我要写入文件的数据是一个简单的3位数字时,这是一种常见的做法(缓存),所以我的问题是这应该还是更快些?这只是一个矫kill过正,我应该坚持使用数据库查询吗?

Caching, when done right, is always faster. 正确完成缓存后,缓存总是更快。

It depends how long storing and retrieving data from the file takes and how long requests to the database takes. 这取决于从文件存储和检索数据需要花费多长时间以及对数据库的请求需要花费多长时间。

If the database query to get the number takes long, then caching may be a good idea, since the data is small. 如果数据库查询中获取数字的时间很长,则缓存可能是个好主意,因为数据很小。

If you were to do a search (eg sequential) in a file with lots of cached data (which doesn't seem to be the case), it would take long. 如果要在具有大量缓存数据的文件中进行搜索(例如顺序搜索)(似乎并非如此),则将需要很长时间。

Disk I/O could be slower than database I/O (which is unlikely, unless it's a local DB). 磁盘I / O可能比数据库I / O慢(这不太可能,除非它是本地数据库)。

Bottom line - benchmark. 底线-基准。

For your scenario, caching is probably a good idea, but if it's only a single 3-digit number for all users then I'd just try to stick it in RAM rather than in a file. 对于您的情况,缓存可能是个好主意,但是如果对于所有用户来说,它只是一个3位数字,那么我将尝试将其保留在RAM中而不是文件中。

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

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