简体   繁体   English

使用内存数据库(例如SQLite)是否比将所有内容保留在HashMap或其他数据结构中更好?

[英]Is it better to use an in-memory database (e.g. SQLite) than to keep everything in HashMap or other data structures?

I need to have very fast access to a big Map - several millions of entries. 我需要快速访问一张大地图-数百万个条目。 Is it worth using an SQLite in-memory database to keep that map as opposed to just having that HashMap in memory? 是否值得使用SQLite内存数据库来保留该映射,而不是仅将HashMap保留在内存中?

its depends on the services you require from your data structure. 它取决于您从数据结构中所需的服务。 do you only need to retrieve values from the map? 您只需要从地图中检索值? or do you need to do a complex query or sorting?. 还是需要进行复杂的查询或排序?

there is nothing magical about a database internal structure, to make it arbitrary faster then a simple data structure . 数据库内部结构没有什么神奇的,它可以使它比简单的数据结构更快地变得任意。 In the database there are more facilities to manipulate large sets of data that probably will cost in an overheard of CPU and memory. 在数据库中,有更多的工具可以处理大量数据,这可能会在窃听CPU和内存的情况下付出代价。 if you only need a dictionary like functionality , go with a map, for something more complex consider a database 如果您只需要像function之类的字典,请搭配一张地图,对于更复杂的东西,请考虑使用数据库

It depends, but if your data model is simple enough to fit into a Map, and you don't need to save the data between runs of your program, then an in-memory database will most likely be overkill. 这要看情况,但是如果您的数据模型足够简单以适合Map,并且不需要在程序运行之间保存数据,则内存数据库很可能会过大。 Databases are designed for more complex data models, safe parallel access and update using transactions, complex queries, constraints, and so on. 数据库设计用于更复杂的数据模型,使用事务进行安全的并行访问和更新,复杂的查询,约束等。

If you decide that a Map is appropriate, then you should carefully select the kind of Map you need. 如果您决定使用适当的地图,则应仔细选择所需的地图类型。 Take a look at the full range of Maps available in java.util.collections, and also look hard at Google Collections, which extends Java's in some very nice ways. 查看java.util.collections中可用的所有Maps,并仔细查看Google Collections,它以一些非常不错的方式扩展了Java。 Also look at java.util.concurrent, which has a good ConcurrentHashMap that will permit your data structure to be used concurrently by multiple threads. 还要看一下java.util.concurrent,它具有良好的ConcurrentHashMap,它将允许您的数据结构被多个线程同时使用。

Be sure to consider how you construct your Map. 请务必考虑如何构建地图。 If you use a HashMap, setting the initial capacity and load factor at construction time may have some impact on performance. 如果使用HashMap,则在构造时设置初始容量和负载因子可能会对性能产生一些影响。

Another thing to do here is hide your implementation behind a facade class you write. 这里要做的另一件事是将您的实现隐藏在您编写的Facade类之后。 That way if you decide to switch approaches you won't impact your client code. 这样,如果您决定切换方法,则不会影响客户代码。

我认为,如果仅按键查询,那么内存中的哈希图没有比这更快的了。

In addition to Alon and Jim's excellent remarks, I would suggest trying both approaches and benchmarking the performance. 除了阿隆(Alon)和吉姆(Jim)的出色讲话外,我建议尝试两种方法并以性能为基准。 Besides being fun (in an admittedly geeky way), this test will force you to encapsulate your data structure in just the right way, so that only the essential functionality is exposed. 除了有趣(以令人讨厌的怪异方式)之外,该测试还将迫使您以正确的方式封装数据结构,从而仅公开基本功能。

暂无
暂无

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

相关问题 使用该类是否更有效,例如Hashtable而不是接口,例如Map? - Is it more efficient to use the class, e.g. Hashtable than the interface, e.g. Map? 阵列可以比物理内存大吗? (例如可以部分分页) - Can an array be larger than physical memory? (e.g. can it be partially paged) 在从文件读取更好的数据行之前,枚举有多复杂(例如.csv)? - How complex can an enum get before reading lines of data from a file is better (e.g. .csv)? 是否存在带有getAndWait()方法的HashMap?例如。一个BlockingConcurrentHashMap实现? - Does a HashMap with a getAndWait() method exist? E.g. a BlockingConcurrentHashMap implementation? SQLite 内存数据库间歇性遇到 SQLITE_LOCKED_SHAREDCACHE - SQLite in-memory database encounters SQLITE_LOCKED_SHAREDCACHE intermittently Linux中的本机内存使用似乎比通过JVM本身(例如,通过JConsole)观察到的要高得多 - Native memory usage in Linux seems to be much higher than observed through JVM itself (e.g. through JConsole) 如何将键从一个哈希图中关联到另一个哈希图中(例如,学生到教室)? - How can I associate keys from a hashmap to another hashmap (e.g. students to a classroom)? 如何将JPEG压缩算法用于一维数据(例如一条线)? - How can I use JPEG Compression algorithm for 1D data (e.g. a line)? 您可以混合使用抽象数据类型吗? 例如地图的优先队列? - Can you use a mix of abstract data types? e.g. a priority queue of maps? 使用休眠创建内存数据库并将数据加载到 - Create in-memory database with hibernate and load data into
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM