Why is this postgresql query so slow?

Posted by user315975 on Stack Overflow See other posts from Stack Overflow or by user315975
Published on 2010-06-08T00:40:16Z Indexed on 2010/06/08 0:52 UTC
Read the original article Hit count: 214

Filed under:
|
|

I'm no database expert, but I have enough knowledge to get myself into trouble, as is the case here. This query

SELECT DISTINCT p.* 
  FROM points p, areas a, contacts c 
 WHERE (    p.latitude > 43.6511659465 
        AND p.latitude < 43.6711659465 
        AND p.longitude > -79.4677941889 
        AND p.longitude < -79.4477941889) 
   AND p.resource_type = 'Contact' 
   AND c.user_id = 6

is extremely slow. The points table has fewer than 2000 records, but it takes about 8 seconds to execute. There are indexes on the latitude and longitude columns. Removing the clause concering the resource_type and user_id make no difference.

The latitude and longitude fields are both formatted as number(15,10) -- I need the precision for some calculations.

There are many, many other queries in this project where points are compared, but no execution time problems. What's going on?

© Stack Overflow or respective owner

Related posts about sql

Related posts about postgresql