搜索和过滤 ObservableCollection 的有效方法是什么
我有一个包含大约 100 万条记录的数据库。我的应用程序对这些记录进行频繁的增量搜索并过滤 UI 列表。该场景更像是“电话联系人搜索”。
目前我正在关注这个:
- 将数据加载到数据源层的
List
中`- 将其发送到 MainViewModel
- 将
List
加载到ObservableCollection
(绑定到ListView)- 根据关键字,过滤`ObservableCollection
- 当应用程序关闭时,更新数据库[自 ObservableCollection 也可能是 由用户更新]
我的问题:
- 这是有效的方法吗?
- 现在我的应用程序消耗大约 30 到 50 MB 的内存。这公平吗?
- 或者我应该在数据库中执行搜索? [但我不能在速度上妥协]
- 我是否应该始终创建 myModel 列表并将其加载到我的 ObservableCollection 中?
- 还建议我了解非常适合增量搜索的过滤技术(第四点)。目前,我后面有一个 GenericList,其中包含用于查找的整个集合,并将过滤后的项目添加到 ObservableCollection,清除所有以前的项目。
编辑:之前我检查了内存消耗,只有 37k 条记录。如果有 250k 条记录,内存消耗超过 100 MB:(。因此现在我计划在内存中只保留大约 10k 条记录,如果超出这个范围,我将查询数据库。有什么建议吗?
谢谢提前, 维尔
I've a database consisting of about 1 million records. My application makes frequent incremental search on these records and filters the UI list. The scenario is more like the 'phone contact search'.
Currently i'm following this:
- Load the data into
List<myModel>
in the DataSource Layer`- Send it to MainViewModel
- Load
List<myModel>
intoObservableCollection<myViewModel>
(bound to ListView)- According to the keyword, filter the `ObservableCollection
- While the application is being closed, update the database [Since the
ObservableCollection may also be
updated by the user]
My Questions:
- Is this the efficient way?
- Now my applicatation consumes around 30 to 50 MB of memory. Is that fair?
- Or should i perform my search in the database instead? [but i cannot compromise on speed]
- Should i always create a list of myModel and load it into my ObservableCollection?
- Also advise me on the filtering technique that is much suited for incremental search(4th point). Currently I have a GenericList behind containing the entire collection for lookup and add the filtered items to the ObservableCollection, clearing all the previous items.
Edit: Previously i checked the memory consumption with only 37k records. With 250k records, the memory consumption is more than 100 MB:(. Hence now I've planned to keep only some 10k records in memory, If it goes beyond that I'll query the db. Any suggestions?
Thanks in advance,
Veer
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以执行多种操作,而不会中断用户的工作流程或造成巨大的性能损失。
这对于 .net 应用程序来说是正常的。您会注意到启动应用程序后内存占用量并没有小很多。
仅当您无法一开始就加载数据时,重复查询数据库才合适。每当用户在框中键入内容时,您需要确保不会在每次更改搜索条件时查询数据库,而是在其中设置一个短计时器,在使用新的条件查询数据库之前等待一秒钟左右标准。当重新查询数据库时,当用户开始滚动时,它还可能有助于减少通过分页或延迟加载其余数据显示的记录数量。正确的数据库索引将帮助您显着降低此类查询的执行速度。
但是,如果您可以将整个列表保留在内存中(假设它不是太大而无法一步从数据库中查询它,或者您无论如何都需要向用户显示整个列表),那么最好保留一份原始副本列表并拥有该列表的副本,您可以逐步过滤该列表。因此,您需要检查您的搜索条件是否是先前搜索条件的子集。如果是这样,您可以过滤已经过滤的列表,否则您需要过滤原始列表。这可以使用 LINQ
.Where()
运算符有效地实现,并在必要时使用 PLINQ 进行并行化。.Where()
表现出 O(n) 时间 AFAIK。这可以通过使用具有适当键的 HashSet 来改进。
There are several things you can do without intterrupting the workflow of the user or taking a huge performance hit.
That's normal for a .net application. You will notice the memory footprint directly after launching the application isn't much smaller.
Querying the database repeatedly is only appropriate if you couldn't load the data in one step in the first place. Whenever the user types into the box, you want to make sure you aren't querying the database on each change to the search criteria but rather have a short timer in there, that waits for a second or so before querying the database with the new criteria. When requerying the database, it may also help to reduce the number of records shown via paging or lazy loading the rest of the data when the user starts scrolling. Proper database indexing will help you reduce the execution speed of such a query significantly.
If you can however keep the whole list in memory (say it's not too large to query it from the database in one step, or you need to show the whole list to the user anyway), it would be best to keep a pristine copy of the list and have a copy of that list that you can incrementally filter. Therefore you need to check if your search criteria is a subset of the previous search criteria. If so, you can filter the already-filtered list, else you need to filter the pristine list. This can be efficiently implemented using the LINQ
.Where()
operator, and if necessary parralelized using PLINQ..Where()
exhibits O(n) time AFAIK.This can be improved on by using a HashSet with appropriate key.