简体   繁体   English

什么是更快 - 将数据存储在列表或数据库中? (机器人)

[英]What is faster - Storing data in list or in a database? (Android)

I'm currently working on a project in school (Android application) where we are supposed to store information on the users' expenses and income in a local SQLite database, and then display it as various diagrams, etc. 我目前正在学校的一个项目(Android应用程序),我们应该在本地SQLite数据库中存储用户费用和收入的信息,然后将其显示为各种图表等。

My question is; 我的问题是; What would be faster? 什么会更快? Constantly making queries to the database in order to get information on certain expenses, or simply making a query on startup and storing the returned values as objects in a list? 不断地对数据库进行查询以获取有关某些费用的信息,或者只是在启动时进行查询并将返回的值作为对象存储在列表中?

Thanks! 谢谢!

It is faster to pull every entry from DB at once and then save it into the List till it is used. 一次从DB中提取每个条目然后将其保存到列表中直到使用它会更快。

Unless you want to search in those Entries. 除非你想在那些条目中搜索。 If you are looking for every Entry that looks like "market" i would search in db and put the result in the list to display it. 如果您正在寻找看起来像“市场”的每个条目,我将在db中搜索并将结果放在列表中以显示它。

Basicly, what you want to avoid is to retrieve every Entry from your Database at exactly the Time, that the ListAdapter calls getView and you need new Data. 基本上,您要避免的是在恰好时间从数据库中检索每个条目,ListAdapter调用getView并且您需要新数据。

It really depends on the amount of data and the complexity of the queries you need to perform. 这实际上取决于数据量和您需要执行的查询的复杂性。 If you are dealing with thousands of objects, and you need to iterate over the list to check for specific properties on each, it might well be faster to use a database. 如果您正在处理数千个对象,并且需要遍历列表以检查每个对象的特定属性,那么使用数据库可能会更快。

Storing them as list will definitely be faster. 将它们存储为列表肯定会更快。 Here is a breakdown 这是一个细分

If you load data and keep it in list you will always have access to it since it is in the memory and is already in format you want. 如果您加载数据并将其保存在列表中,您将始终可以访问它,因为它在内存中并且已经是您想要的格式。

If you query every single time then there are three steps involved. 如果您每次查询,则涉及三个步骤。
1) Read data from memory (hard drive) 1)从内存中读取数据(硬盘)
2) Convert it into an object you want 2)将其转换为您想要的对象
3) Store data in memory (RAM) 3)将数据存储在内存(RAM)中
4) Read data from memory (RAM) 4)从内存中读取数据(RAM)

Too many steps and slower execution as you see 如您所见,步骤太多,执行速度较慢

Accessing a database is expensive. 访问数据库非常昂贵。 So it would make sense to get all the data out of the DB. 因此,从数据库中获取所有数据是有意义的。 If the data is changed, update your database in onStop (this is the place to do clean up work). 如果数据已更改,请在onStop中更新数据库(这是清理工作的地方)。

The answer is; 答案是; it depends. 这取决于。 If you have only a small number of items in the list, then yes it will almost certainly be faster to pull them at startup; 如果列表中只有少量项目,那么在启动时拉它们几乎肯定会更快; however if there are thousands of items, it might make the application startup much slower (although accessing the individual items will of course be faster). 但是,如果有数千个项目,它可能会使应用程序启动慢得多(尽管访问单个项目当然会更快)。

My advice would be to actually test it and see which approach works best with the data set you have, and make your decision based on empirical evidence. 我的建议是实际测试它,看看哪种方法最适合你的数据集,并根据经验证据做出决定。

If you're gonna iterate through the objects/data, then probably best to use a Database to query. 如果您要遍历对象/数据,那么最好使用数据库进行查询。

Otherwise, use a List to make it faster and also simple to code. 否则,使用List可以使其更快,也更容易编码。

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

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