iPhone 上复杂查询的 NSArray 与 SQLite

发布于 2024-08-31 04:45:54 字数 285 浏览 0 评论 0原文

在为 iPhone 进行开发时,我有一组需要进行复杂查询的点。例如:“有多少个点的 y 坐标为 10”和“返回 X 坐标在 3 到 5 之间且 y 坐标为 7 的所有点”。

目前,我只是循环遍历 NSArray 的每个元素并检查每个元素是否与我的查询匹配。不过,编写查询很痛苦。 SQLite 会好得多。我不确定哪个会更有效,因为 SQLite 数据库驻留在磁盘上而不是内存中(据我所知)。 SQLite 在这里会同样高效或更高效吗?或者说除了这些我没想到的方法之外还有更好的方法吗?我需要使用多组点执行多个查询数千次,因此最佳性能很重要。

Developing for iPhone, I have a collection of points that I need to make complex queries on. For example: "How many points have a y-coordinate of 10" and "Return all points with an X-coordinate between 3 and 5 and a y-coordinate of 7".

Currently, I am just cycling through each element of an NSArray and checking to see if each element matches my query. It's a pain to write the queries though. SQLite would be much nicer. I'm not sure which would be more efficient though since a SQLite database resides on disk and not in memory (to my understanding). Would SQLite be as efficient or more efficient here? Or is there a better way to do it other than these methods that I haven't thought of? I would need to perform the multiple queries with multiple sets of points thousands of times, so the best performance is important.

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

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

发布评论

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

评论(3

故事与诗 2024-09-07 04:45:54

您可以使用 SQLite 作为内存数据库。只需使用文件名 ":memory:" 对其进行初始化即可。由于 SQL 引擎和动态类型系统的开销,SQLite 永远不会像精心设计的数据结构那样执行。但凭借 ad hoc SQL 的便利性和完全通用性,它仍然可能产生非常好的结果。您甚至可以向内存数据库添加索引以帮助提高查询性能。

You can use SQLite as an in-memory database. Just initialise it with the filename ":memory:". SQLite will never perform as well as carefully hand-crafted data structures, due to the overheads of the SQL engine and the dynamic type system. But it might still yield very good results with the convenience and full generality of ad hoc SQL. You can even add indexes to in-memory databases to help with query performance.

淡淡的优雅 2024-09-07 04:45:54

如果性能是您的关键标准,那么您应该使用浮点数组来表示您的点,而不是 NSArray,因为它的速度会尽可能快。
如果您确实想使用 sqlite,可以将其配置为作为内存数据库运行,请参阅此处 详细信息。

如果你真的想降低性能,你可能会考虑使用 LLVM 动态生成机器代码来迭代你的数据集,但这可能有点过头了;)

If performance is your key criteria then you should be using an array of floats to represent your points rather than an NSArray as that is as fast as it is going to get.
If you really want to use sqlite, it can be configured to run as an in memory database, see here for the details.

If you really want to get down and dirty with performance you might look into using LLVM to dynamically generate machine code to iterate over your data set, but that is probably over kill ;)

谈情不如逗狗 2024-09-07 04:45:54

我认为 Core Data 和 SQLite 都实现了缓存,这样您就可以轻松查询并获得关系数据库的强大功能。应该值得研究一下。

I think Core Data and SQLite both implement caching, so that you get the ease of querying and the power of a relational database. Should be worth investigating.

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