Reputation: 2811
I'm using MySQL 8.0.30 which has fixed a major known issue related to SPATIAL indexes.
I've modified a POINT() column to transform it to SRID 4326.
Before, I had the following query returning 20+ rows:
SELECT r.id
FROM record r
WHERE ST_CONTAINS(ST_GeomFromText('POLYGON((-74.5 40.5, -73 40.5, -73 39.5, -74.5 39.6, -74.5 40.5))'), r.location);
The location
column had a SPATIAL index that was not used by the MySQL engine as the SRID was missing on this column. The query was working but slow.
After, I added an SRID 4326 definition on that column, made sure I had no NULL entries and added a SPATIAL index:
mysql> describe record;
+-------------------+------------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+------------------+------+-----+-------------------+-----------------------------+
| id | int unsigned | NO | PRI | NULL | auto_increment |
| location | point | NO | MUL | NOT NULL |
+-------------------+------------------+------+-----+-------------------+-----------------------------+
and
mysql> show indexes from record;
+--------------+------------+-----------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+--------------+------------+-----------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| record | 1 | my_spatial | 1 | location | A | 14351 | 32 | NULL | | SPATIAL | | | YES | NULL |
+--------------+------------+-----------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
Here is the new query, forcing the use of SRID 4326:
SELECT r.id
FROM record r
WHERE ST_CONTAINS(ST_GeomFromText('POLYGON((-74.5 40.5, -73 40.5, -73 39.5, -74.5 39.6,
-74.5 40.5))', 4326), r.location);
I am not getting any error, however the previous query was returning 20+ rows and I'm now getting 0.
I checked both SRID and they look good to me:
mysql> select ST_SRID(location) from record limit 1;
+--------------------+
| ST_SRID(location) |
+--------------------+
| 4326 |
+--------------------+
mysql> select ST_SRID(ST_GeomFromText('POLYGON((-74.5 40.5, -73 40.5, -73 39.5, -74.5 39.6, -74.5 40.5))', 4326));
+-----------------------------------------------------------------------------------------------------+
| ST_SRID(ST_GeomFromText('POLYGON((-74.5 40.5, -73 40.5, -73 39.5, -74.5 39.6, -74.5 40.5))', 4326)) |
+-----------------------------------------------------------------------------------------------------+
| 4326 |
+-----------------------------------------------------------------------------------------------------+
Am I doing something the wrong way?
Upvotes: 0
Views: 857