拆分CoreData以缩短查询时间 [英] Splitting CoreData up for a faster query time

查看:87
本文介绍了拆分CoreData以缩短查询时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个巨大的经度和纬度核心数据数据库,用于地图上的路径。

I have a huge core data database of longitudes and latitudes for paths on a map.

我有一个称为方法的对象由一组有序的节点(lon,lat)组成。我还沿途存储了封闭盒(minLon,minLat,maxLon,maxLat ...)。

I have an object called a way which consists of an ordered set of Nodes (lon,lat). I also store the enclosing box around the way (minLon, minLat, maxLon, maxLat ...).

我的查询会查找地图特定区域中的所有路径:

My query finds all the ways in a particular region of the map:

NSPredicate *predicate = [NSPredicate predicateWithFormat:
    @"minLon < %f AND maxLon > %f AND minLat < %f AND maxLat > %f",
        maxLon, minLon, maxLat, minLat];

它的水坝很慢!

我的想法加快查询速度是通过某种方式将数据划分为正方形区域(多个表?多个 .sqllite 文件?创建位置的哈希?),因此要减少数据量

My idea to speed up the query is to split the data by square regions somehow (Multiple tables? Multiple .sqllite files? Create a hash of the location?) so there is less data to search though.

我该怎么做?

推荐答案


  1. 您可以通过将Way实体中的最小/最大值存储为比例整数而不是浮点数来加快查询速度。选择一个可以给您带来可接受的区域分辨率的比例因子,在存储它们时使用它来预缩放值,并在执行查询时使用相同的比例因子。

  1. You could speed up the query by storing the min/max values in your Way entity as scaled integers instead of floats. Pick a scaling factor that gives you an acceptable area resolution, use it to prescale the values when you store them and use the same scale factor when you do the query.

您还可以考虑将 quadtree 数据结构添加到核心数据模型中

You could also look at adding a quadtree data structure to your core data model to home in on Ways in the desired area.

SQlite将在第一个未通过复合谓词的测试中纾困,因此将最便宜的测试放在首位。

SQlite will bail out at the first test that fails in a compound predicate so put the cheapest test first.

还要看看为这4个属性启用索引。

Also look at enabling indexing for those 4 attributes.

不过,老实说,我对此很慢感到惊讶。您的数据库中总共有多少种方式,通常要查询的区域中有多少种方式?

To be honest though, I'm surprised that this is slow. How many total Ways do you have in your database and how many typically in an area that you want to query?

您是否尝试过打开SQL调试来查看什么查询实际针对数据库运行? (提示:将-com.apple.CoreData.SQLDebug 1添加为方案编辑器中用于运行行为的命令行参数)

Have you tried turning on SQL debugging to see what query is actually run against the database? (hint: add -com.apple.CoreData.SQLDebug 1 as a command line argument in your Scheme editor for the Run behaviour)

这篇关于拆分CoreData以缩短查询时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆