I need to do some database housekeeping.
I have a database table which consists of locations (containing latitude and longitude amongst other stuff). It's being used to generate Geofences (if you were interested).
As part of housekeeping, I want to remove location records from the database that are 'close to' other records (i.e. if 2 records in the table are within 250 metres of each other, I will delete the first one created and leave the second one).
My algorithm for achieving this is to loop through each record (I'll call this the root record) and for each root record loop through every other record in the table (I'll call this the check record), calculate the distance between the two locations, and if <250, delete the root record.
[This is pseudocode]
while (rootLoop.moveToNext) {
while (checkLoop.moveToNext) {
distance = getDistanceBetween(rootLocation, checkLocation);
if (distance < 250) delete(rootRecord);
}
}
If I've got a database table of a few hundred locations, this might take a bit of processing effort (I'll obviously run it in a background service).
So my question is - is there a more efficient way of achieving what I want than these nested loops?
If there is then I can't think of it but I thought I'd throw it open to the floor. How creative are you?