文件读/写与数据库读/写
从资源和效率角度来看,文件读/写操作和数据库读/写操作哪个更昂贵
Which is more expensive to do in terms of resources and efficiency, File read/write operation or Database Read/Write operation
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我最初打算说数据库读/写,毫无疑问,因为它会在数据库开销之上包含必要的文件 io,但后来意识到它并不那么简单。如果您将整个数据库加载到内存中,则读取几乎是瞬时的,因为不涉及文件 IO。
一般来说,写入也会更快,因为数据库引擎不必等待文件 IO 完成后再返回,因为它们可以采用“延迟写入”方法。
另一方面,调整不当的数据库将比任何基于文件的 IO 慢几个数量级。数据库调优很重要。很多。
I was initially going to say database read/write, hands down, as it would include the requisite file io on top of the DB overhead, but then realized its not that simple. If you have your entire DB loaded into memory, reads would be nearly instantaneous as there's no file IO involved.
Writes would, in general, be faster too, as the DB engine doesn't have to wait for the file IO to complete before returning since they can take a "lazy write" approach.
A poorly tuned database, on the other hand, will be orders of magnitude slower than any file based IO. DB tuning matters. A lot.
这是一个有内涵的问题。我们谈论的文件大小是多少?千兆字节?另外,数据库的类型和大小是什么?我经常使用组合。您想控制任何数据级别的完整性吗?如果是这样,您可能希望将其留给数据库,否则您必须在应用程序级别控制所有这些。
有很多因素可以对此做出明智的决定。例如,当我创建不想保留的临时数据时,我使用文件,但如果我使用我想要保留或备份的数据,那么我使用数据库。
这与架构相结合很重要。如果硬件、许可或设施是一个问题,那么您可能不需要数据库服务器等基础设施。但如果您有资源,那么添加数据库层可能是正确的选择。
This is kind of a loaded question. What size files are we talking about? Gigabytes? Also, what type and size of DB? I often use a combination. Do you want to control any data level integrity? If so, you might want to leave that to the DB otherwise you have to control all that at the application level.
There are so many factors to make a good decision on this. For example, when I am creating temporary data that I don't want persisted I use File, but if I am using data I want persisted or backed up, then I use a DB.
This coupled with the architecture is important. If hardware, licensing, or facility is an issue then maybe you don't need the infrastructure of DB servers etc. But if you have the resources then adding a DB layer might be the right choice.
没有简单的答案。对于任何数据库,您都会有让它一直运行的开销。但是当您访问它时通常比访问文件要快得多。如果您只讨论少数访问,您将不会注意到太多差异。但当达到每分钟数百、数千、数百万次访问时,数据库的速度就会快得多。正如蒂姆上面指出的,调整不当的数据库可能比访问平面文件慢得多。
There's no simple answer. With any database you have the overhead of having it running all the time. But then when you access it is generally much faster than accessing a file. If you are talking about just a handful of accesses you won't notice much of difference. But when it gets to hundreds, thousands, and millions of accesses per minute the database will be much faster. And as Tim noted above, a poorly tuned database can be much slower than accessing a flat file.