views:

38

answers:

2

Hi ! I am working with a huge database and trying top optimize it.

I was wondering if it will make any change to index the values that are used as criteria in the request, but through a function.

For example I have this GPS coordinate table :

-Node (#id,lat,lng)

and this request :

SELECT * FROM Node WHERE distance( lat, lng, $lat, $lng ) < $threshold

Would creating an index on lat and lng make any optimization ? (I'm working with SQLite)

Thanks

Edit I just thought about the same question, but if I make the calculation directly like :

SELECT * FROM Node WHERE (lat-$lat)*(lat-$lat) + (lng-$lng)*(lng-$lng)  < $threshold
+2  A: 

For queries, you would absolutely see an performance benefit.

But with a jumbo database you will also encounter a performance hit on insertions.

Sky Sanders
Actually I don't see how, since the index just sorts the values (lat and lng in this case), right ? And the SQLite engine has no idea wether the function (distance) is monotone, and not even linear. So how will it know it just needs to calculate part of the values ?
Julien
@julien - I could be wrong as I am by no means a sqlite expert but the db engine still has to find the row to feed to the function, no? in a large database an index would make a significant difference i would suspect.
Sky Sanders
+1  A: 

The database will need to calculate the distance for each node in your example and will not benefit from an index. If you however index the lng and lat columns and use these to first eliminate all nodes that either have abs(lat - $lat) > $threshold or abs(lng - $lng) > $threshold you could see increased performance since the database can use the created index to eliminate a number of records before calculating the distance for the remaining records.

The query would look something like this:

SELECT * FROM Node 
WHERE lat >= $lat - $threshold
AND lat <= $lat + $threshold
AND lng >= $lng - $threshold
AND lng <= $lng + $threshold
AND distance( lat, lng, $lat, $lng )  < $threshold;
Chris
Thanks for the answer, that's what I thought. The tip is interesting, I'll implement it !
Julien