为什么有人需要内存数据库?
我读到一些数据库可以在内存中使用,但无法想到为什么有人想要使用此功能。我总是使用数据库来保存数据和内存缓存以实现快速访问。
I read that a few databases can be used in-memory but can't think of reason why someone would want to use this feature. I always use a database to persist data and memory caches for fast access.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
缓存也是一种数据库,就像文件系统一样。 “内存缓存”只是内存数据库的一种具体应用,有些内存数据库专门用作内存缓存。
内存数据库的其他用途已经包含在其他答案中,但让我也列举一下用途:
Cache is also a kind of database, like a file system is. 'Memory cache' is just a specific application of an in-memory database and some in-memory databases are specialized as memory caches.
Other uses of in-memory databases have already been included in other answers, but let me enumerate the uses too:
一个常见的用例是运行单元/集成测试。
您并不真正关心每次测试运行之间的持久数据,并且您希望测试尽可能快地运行(以鼓励人们经常进行测试)。在进程中托管数据库可以让您非常快速地访问数据。
A common use case is to run unit/integration tests.
You don't really care about persisting data between each test run and you want tests to run as quickly as possible (to encourage people to do them often). Hosting a database in process gives you very quick access to the data.
你的内存缓存有 SQL 支持吗?
您是否认为内存数据库是一个非常聪明的缓存?
这确实留下了如何填充内存数据库、如何管理更新以及如何在多个实例之间保持一致性的问题。
Does your memory cache have SQL support?
How about you consider the in-memory database as a really clever cache?
That does leave questions of how the in-memory database gets populated and how updated are managed and consistency is preserved across multiple instances.
如果不使用索引等技巧,在 100000 个元素中搜索某些内容会很慢。这些技巧已经在数据库引擎中实现(无论是持久的还是内存中的)。
内存数据库可能提供比您通过自己编写的结构快速实现的搜索功能更有效的搜索功能。
Searching for something among 100000 elements is slow if you don't use tricks like indexes. Those tricks are already implemented in a database engine (be it persistent or in-memory).
A in-memory database might offer a more efficient search feature than what you might be able to implement yourself quickly over self-written structures.
对于通用(读取端)查询,内存数据库比传统 RDBMS 大约快至少一个数量级。大多数都是磁盘支持的,提供与普通 RDBMS 完全相同的一致性 - 仅捕获整个数据集必须适合 RAM。
核心思想是磁盘支持的存储具有巨大的随机访问惩罚,这不适用于 DRAM。数据可以以随机访问优化的方式进行索引/组织,而使用传统的 RDBMS 数据缓存方案是不可行的。
In-memory databases are roughly at least an order of magnitude faster than traditional RDBMS for general purpose (read side) queries. Most are disk backed providing the very same consistency as a normal RDBMS - only catch the entire dataset must fit into RAM.
The core idea is disk backed storage has huge random access penalties which does not apply to DRAM. Data can be index/organized in a random access optimized way not feasible using traditional RDBMS data caching schemes.
需要实时响应的应用程序希望使用内存数据库,也许是控制响应时间至关重要的飞机、工厂的应用程序
Applications, which require real time responses would like to use an in memory database, perhaps application to control aircraft, plants where the response time is critical
内存数据库在游戏编程中也很有用。您可以将数据存储在内存数据库中,这比永久数据库快得多。
An in memory database is also useful in game programming. You can store data in an in memory database which is much faster than permanent databases.
它们用作高级数据结构来存储、查询和修改运行时数据。
They are used as an advanced data structure to store, query and modify runtime data.
如果多个不同的应用程序要访问数据集,您可能需要一个数据库。数据库具有用于访问/修改数据的一致接口,而哈希表(或您使用的其他任何东西)则没有。
如果单个程序正在处理数据,那么无论您使用什么语言,都可以合理地使用数据结构。
You may need a database if several different applications are going to access the dataset. A database has a consistent interface for accessing / modifying data, which your hash table (or whatever else you use) won't have.
If a single program is dealing with the data, then it's reasonable to just use a data structure in whatever language you are using though.
内存数据库比执行数据库缓存更好。
在 READ 操作方面,数据库缓存的工作原理与内存数据库类似。
另一方面,当涉及到 WRITE 操作时,内存数据库比数据库缓存更快,后者的数据保存在磁盘上(这会导致 IO 开销)。
此外,使用数据库缓存,您可能会出现缓存未命中的情况,但在使用内存数据库时,您永远不会出现缓存未命中的情况。
In-memory database is better than performing database caching.
Database caching works similar to in-memory databases when it comes to READ operations.
On the other hand, when it comes to WRITE operations, in-memory databases are faster when compared to database caches, where the data is persisted onto disk (which leads to IO overhead).
Also, with database caching you can end with cache misses but you will never end up with cache misses when using in-memory databases.
考虑到它们的速度和 RAM 价格的下降,内存数据库很可能成为未来的主导技术。已经有一些开发了复杂的功能,例如 SQL 查询、二级索引和用于处理大于 RAM 的数据集的引擎。
Given their speed and the declining price of RAM, it’s likely that in-memory databases will become the dominant technology in the future. There are already some that have developed sophisticated features like SQL queries, secondary indexes, and engines for processing datasets larger than RAM.