简体   繁体   English

从数据库访问数据的速度比从Arraylist访问数据的速度快

[英]Is accessing data from database faster than accessing from Arraylist

I have 10000+ records in a file. 我的文件中有10000多个记录。 I read the file and keep the records in ArrayList, my web application reads the arraylist frequently. 我读取文件并将记录保留在ArrayList中,我的Web应用程序会频繁读取arraylist。

I only perform reading operation on arraylist. 我只对arraylist执行读取操作。

However the performance of web application is slow. 但是,Web应用程序的性能很慢。

Will it be better to keep the data from file in Database and read from database. 将数据保留在数据库中的文件中并从数据库中读取会更好。

Or is there any other collection that I can use. 还是我可以使用其他任何收藏。

With an ArrayList<MyRecord> you don't have a good lookup method, so your code is likely doing sequence searches whenever you need to find a record. 使用ArrayList<MyRecord>您没有很好的查找方法,因此您的代码很可能在需要查找记录时进行序列搜索。

If you only ever lookup by one value, eg name, then change to HashMap<String, MyRecord> . 如果仅按一个值(例如名称)查找HashMap<String, MyRecord>改为HashMap<String, MyRecord> This will allow direct lookup without need for sequence search. 这将允许直接查找而无需序列搜索。

If you lookup by various values, keep the ArrayList<MyRecord> around, but build multiple maps, one for each lookup field (or set of fields). 如果您通过各种值进行查找,请保留ArrayList<MyRecord>左右,但要构建多个映射,每个查找字段(或一组字段)一个。

The maps are basically the same as indexes in a database. 映射基本上与数据库中的索引相同。

Now, if data is updated , things are very different. 现在,如果数据被更新 ,情况将大为不同。 Does an update require you to write the entire file again, immediately? 更新是否要求您立即再次写入整个文件? What if the program dies halfway through a write? 如果程序在写入过程中死了怎么办? Databases have built-in guards that help prevent data loss, and will also allow sharing the data among multiple programs running at the same time. 数据库具有内置的防护措​​施,有助于防止数据丢失,并且还允许在同时运行的多个程序之间共享数据。

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

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