iPhone硬计算和缓存 [英] iPhone hard computation and caching
问题描述
我有问题。我的数据库中有 500k 条记录。每个记录存储纬度,经度,动物种类,观察日期。我必须在mapkit视图上绘制网格(15x10),显示在这个网格单元中的物种的集中。每个单元格为32x32框。
I have problem. I have database with 500k records. Each record store latitude, longitude, specie of animal,date of observation. I must draw grid(15x10) above mapkit view, that show the concentration of specie in this grid cell. Each cell is 32x32 box.
如果我在运行时计算,这是非常慢。
If I calculate in run-time it is very slow. Have somebody idea how to cache it?In memory or in database.
数据结构:
观察:
- 纬度
- 经度
- 日期
- Latitude
- Longitude
- Date
- Specie
- some other unimportant data
屏幕示例:
alt text http://img6.imageshack.us/img6/7562/20091204201332.png
每个红色方框都显示该区域的物种数。
Each red box opocasity show count of species in this region.
现在使用的代码:
data - >从数据库中选择,在地图区域中查看
Code that i use now: data -> select from database, it is all observation in map region
for (int row = 0; row < rows; row++)
{
for (int column = 0; column < columns; column++)
{
speciesPerBox=0;
minG=boxes[row][column].longitude;
if (column!=columns-1) {
maxG=boxes[row][column+1].longitude;
} else {
maxG=buttomRight.longitude;
}
maxL=boxes[row][column].latitude;
if (row!=rows-1) {
minL=boxes[row+1][column].latitude;
} else {
minL=buttomRight.latitude;
}
for (int i=0; i<sightingCount; i++) {
l=data[i].latitude;
g=data[i].longitude;
if (l>=minL&&l<maxL&&g>=minG&&g<maxG) {
for (int j=0; j<speciesPerBox; j++) {
if (speciesCountArray[j]==data[i].specie) {
hasSpecie=YES;
}
}
if (hasSpecie==NO) {
speciesCountArray[speciesPerBox]=data[i].specie;
speciesPerBox++;
}
hasSpecie=NO;
}
}
}
mapData[row][column].count = speciesPerBox;
}
}
推荐答案
你的数据是静态的,你可以预先计算每个网格的每个物种,并将其存储在数据库中,而不是所有的位置坐标。
Since you data is static, you can pre-compute each species for each grid and store it in the database instead of all the location coordinates.
由于你有15 x 10 = 150个单元格,你最终会在数据库中有150 * [num of species]条记录,这应该是一个小得多的数字。
Since you have 15 x 10 = 150 cells, you'll end up with 150 * [num of species] records in the database, which should be a much smaller number.
索引在适当的列。否则,您的查询将必须一次扫描每个记录。
Also, make sure you have indexes on the proper columns. Otherwise, your queries will have to scan every single record over and over again.
这篇关于iPhone硬计算和缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!