简体   繁体   中英

Is accessing data from database faster than accessing from Arraylist

I have 10000+ records in a file. I read the file and keep the records in ArrayList, my web application reads the arraylist frequently.

I only perform reading operation on arraylist.

However the performance of web application is slow.

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.

If you only ever lookup by one value, eg name, then change to 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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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