Search code examples
mysqlmysql-5.6

MySQL 5.6 - avoid parsing geometry twice?


I need to query points within a polygon, which is stored in different table. The query below works and does the work:

SET @b := PolyFromText((SELECT AsText(boundaries.geometry) 
                        FROM boundaries_users 
                        LEFT JOIN  boundaries 
                          ON boundaries_users.boundary_id = boundaries.id
                        WHERE boundaries_users.boundary_name = 'name')); 

SELECT 
    id,
    addr_lon, 
    addr_lat,
    ST_Contains(@b, POINT(addr_lat, addr_lon))
FROM buildings
WHERE ST_Contains(@b, POINT(addr_lon, addr_lat));

Here, boundaries.geometry is of type GEOMETRY. However, I don't understand why I had to convert geometry to text and back to geometry in order to do so. When double parsing is removed, @b is displayed as a set of weird symbols, and ST_Contains returns False for any point.

MySQL 5.6


Solution

  • My guess is you are joining multiple geometries, so your two query generate different geometries.

    Check if you are actually using the same geometry:

     SET @b := With double parsing and without parsing
    

    Then see what AsText(@b) generate.