加快核心数据获取速度

发布于 2024-10-08 00:40:41 字数 781 浏览 0 评论 0原文

这是在 iOS 上。

我有一个包含大约 350 000 个对象的核心数据库。对象(产品)有两个属性:“条形码”和“名称”。用户可以通过搜索“Barcode”来搜索对象,并且应该返回“Designation”。一切都工作正常,除了速度很慢。我使用的代码是:

    NSEntityDescription *_product = [NSEntityDescription entityForName:@"Product" inManagedObjectContext:importContext];
NSFetchRequest *fetch = [[NSFetchRequest alloc]init];

[fetch setEntity:_product];
[fetch setPredicate:[NSPredicate predicateWithFormat:@"Barcode == %@",theBarcode]];

 NSError *error = nil;
 NSArray *results = [importContext executeFetchRequest:fetch error:&error];

NSManagedObject *object = [results objectAtIndex:0];

由于我只想获取一个对象,有没有办法加快速度?

如果我在启动时将每个对象加载到数组中,应用程序的启动速度会非常慢,并且会占用大量 RAM。

提前致谢!

编辑:我添加了 [fetch setFetchLimit:1];这会加快一点速度。但对象在数据库中越往下,速度就越慢。

This is on iOS.

I have a Core Database with about 350 000 objects. The objects (Product) have two properties: "Barcode" and "Designation". The user can search for an object by searching for the "Barcode", and the "Designation" should be returned. Everything is working fine, except it's slow. The code I use is:

    NSEntityDescription *_product = [NSEntityDescription entityForName:@"Product" inManagedObjectContext:importContext];
NSFetchRequest *fetch = [[NSFetchRequest alloc]init];

[fetch setEntity:_product];
[fetch setPredicate:[NSPredicate predicateWithFormat:@"Barcode == %@",theBarcode]];

 NSError *error = nil;
 NSArray *results = [importContext executeFetchRequest:fetch error:&error];

NSManagedObject *object = [results objectAtIndex:0];

Since I only want to fetch one object, is there a way to speed it up?

If I load every object into an Array at the start-up I get a very slow start-up for the app and taking a lot of RAM.

Thanks in advance!

EDIT: I added [fetch setFetchLimit:1]; which speed it up a little bit. But the speed is getting slower the further down in the Database the object is.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

窝囊感情。 2024-10-15 00:40:41

Barcode 属性是否已编入索引?

Is the Barcode attribute indexed?

萧瑟寒风 2024-10-15 00:40:41

首先,正如 @paulbailey 所写,检查 Barcode 是否已索引。

但是,如果您有那么多条目,并且您的条目只有两个属性(条形码和名称),并且您仅从条形码侧查询并返回名称侧,那么使用 CoreData 可能有点大材小用。

CoreData 为您提供了许多面向对象的功能以及对磁盘的持久性,但它当然会带来一些损失。

对您来说,完全放弃 CoreData 并直接使用 sqLite 可能会更好。为此,有一个名为 FMDB 的轻量级 Objective-C 包装器,请参阅此处

如果您想坚持使用 CoreData,一种让事情变得更好的方法是在后台线程中获取并在主线程中显示结果,如 此 Apple 文档。这样,在搜索数据库时 UI 就不会冻结。

First, as @paulbailey wrote, check if Barcode is indexed.

But, if you have that many entries, and if your entry only has two properties (barcode and designation), and if you only query from the barcode side and return the designation side, using CoreData might be an overkill.

CoreData gives you a lot of object-oriented facilities with persistence to the disk, but it of course comes with a penalty.

It might be better for you to drop CoreData altogether, and use sqLite directely. There's a nice light-weight Objective-C wrapper called FMDB for that, see here.

If you want to stick to CoreData, one way to make things better is to fetch in the background thread and to show the result in the main thread, as described in this Apple document. This way the UI doesn't freeze while the database is searched.

仅冇旳回忆 2024-10-15 00:40:41

对象在数据库中越往下走,花费的时间就越长,是因为 Core Data 使用了一种相当枯燥的搜索算法,该算法只是将指针放置到第一个对象,理解其对搜索项的值,将指针放置到下一个对象,然后如此直到比较匹配为止。

您可以使用大量的搜索算法,具体取决于您的数据库(排序/未排序列表、树结构等),您可以使用快速搜索、哈希搜索、树搜索等。

您可能还考虑建立一个 SQlite 数据库,它有一些带有智能搜索算法的不错的框架。

The reason why it takes longer the further down the database the object is, is that Core Data uses a rather dull search algorithm which just places a pointer to the first object, comprehends its value to the searchitem, places the pointer to the next one and so one untill the comparison matches.

There are tons of search algorithms you can use, depending on your database (sorted/ not sorted lists, tree structure etc.) you could use Quicksearch, Hashsearches, treesearch and so on.

You might also think about setting up a SQlite database instead, which has some nice frameworks with intelligent search algorithms.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文