views:

39

answers:

2

I'm trying to get 100 points from my table with a lowest distance to a given point.

I'm using

SELECT *, GLENGTH(
            LINESTRINGFROMWKB(
              LINESTRING(
                ASBINARY(
                  POINTFROMTEXT("POINT(40.4495 -79.988)")
                ),
                ASBINARY(pt)
              )
            )
          )
 AS `distance` FROM `ip_group_city` ORDER BY distance LIMIT 100

(Yeah, that's painful. I've just googled it. I have no idea how to measure distance in MySQL correctly)

It takes very long time for execute. EXPLAIN says that there are no possible_keys.

I created a SPATIAL index on the pt column:

CREATE SPATIAL INDEX sp_index ON  ip_group_city (pt);

Though I don't really know how to use it correctly. Can you please help me?

A: 

I've used the great circle equation to do these types of calculations in the past. I'm not sure how the performance compares but it might be worth trying it and comparing.

Here is a good SO post that goes over how to do it in MySQL.

Abe Miessler
it does not use spatial at all, too... what is it (spatial index) for, then?
valya
Yours is the only type of solution that would potentially use the spatial index. I'm just suggesting another method worth trying to see how the performance compares.
Abe Miessler
oh. I started with a method, similar to yours, and got a lot worst perfomance
valya
A: 

Have a look at these questions:

Craig Trader
it's not sorting and it does not use spatial at all
valya