Hi,
I've got a MySQL query that pulls lat longs from a database based on a criterion, tests whether these points are within a polygon, and returns the points that are within the polygon.
Everything works fine. The problem is that the query takes approx. 20 seconds to return a result. Is there a way to optimize this query so that query speed is faster?
SELECT latitude, longitude
FROM myTable
WHERE offense = 'green' AND myWithin(
POINTFROMTEXT( CONCAT( 'POINT(', latitude, ' ', longitude, ')' ) ) , POLYFROMTEXT( 'POLYGON(( ...bunch of lat longs...))' )
) = 1;
I ran an EXPLAIN SELECT... which produced
id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra
1 SIMPLE myTable ALL NULL NULL NULL NULL 137003 Using where
Is there a way to optimize a query that is run on every latitude and longitude in the db or is this as good as it gets?
I'm thinking about doing a select into another table and then querying the results table, but I was hoping that there would be a way to improve the performance of this query.
If anyone has any suggestions or ideas, I'd love to hear them.
Thanks,
Laxmidi