patternsqlMinor
MySQL 8 not using Spatial Index?
Viewed 0 times
mysqlusingindexnotspatial
Problem
I have an issue where whatever I do I cannot get MySQL to use a spatial index.
A query like the following then takes 5 seconds!
Any way to force this to use the index at all?
Explain of query
Table Structure
``
A query like the following then takes 5 seconds!
Any way to force this to use the index at all?
WITH ranked_reports AS (
SELECT
station_id,
raw_text,
observation_time,
RANK() OVER ( PARTITION BY station_id ORDER BY observation_time DESC ) order_rank
FROM
METAR
WHERE
ST_Distance_Sphere (
geo_point,
ST_GeomFromText ( 'POINT(51.85 -0.79)', 4326 )) <= 100 * 1609.34
) SELECT
*
FROM
ranked_reports
WHERE order_rank =1Explain of query
+----+-------------+------------+------------+------+---------------+-------------+---------+-------+--------+----------+-----------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+------------+------+---------------+-------------+---------+-------+--------+----------+-----------------------------+
| 1 | PRIMARY | | NULL | ref | | | 8 | const | 10 | 100.00 | NULL |
| 2 | DERIVED | METAR | NULL | ALL | NULL | NULL | NULL | NULL | 585814 | 100.00 | Using where; Using filesort |
+----+-------------+------------+------------+------+---------------+-------------+---------+-------+--------+----------+-----------------------------+Table Structure
``
CREATE TABLE METAR (
station_id varchar(5) NOT NULL,
station_iata varchar(5) DEFAULT NULL,
observation_time datetime NOT NULL,
latitude float(255,2) DEFAULT NULL,
longitude float(255,2) DEFAULT NULL,
raw_text varchar(255) DEFAULT NULL,
temp_c float(255,1) DEFAULT NULL,
dewpoint_c float(255,1) DEFAULT NULL,
wind_dir_degrees int(255) DEFAULT NULL,
wind_speed_kt int(11) DEFAULT NULL,
wind_gust_kt` int(11) DEFAULT NULL,Solution
You need to add SRID to column definition:
The SRID attribute makes a spatial column SRID-restricted, which has
these implications:
The column can contain only values with the given SRID. Attempts to
insert values with a different SRID produce an error.
The optimizer can use SPATIAL indexes on the column. See Section
8.3.3, “SPATIAL Index Optimization”.
Spatial columns with no SRID attribute are not SRID-restricted and
accept values with any SRID. However, the optimizer cannot use SPATIAL
indexes on them until the column definition is modified to include an
SRID attribute, which may require that the column contents first be
modified so that all values have the same SRID.
The SRID attribute makes a spatial column SRID-restricted, which has
these implications:
The column can contain only values with the given SRID. Attempts to
insert values with a different SRID produce an error.
The optimizer can use SPATIAL indexes on the column. See Section
8.3.3, “SPATIAL Index Optimization”.
Spatial columns with no SRID attribute are not SRID-restricted and
accept values with any SRID. However, the optimizer cannot use SPATIAL
indexes on them until the column definition is modified to include an
SRID attribute, which may require that the column contents first be
modified so that all values have the same SRID.
Context
StackExchange Database Administrators Q#260757, answer score: 2
Revisions (0)
No revisions yet.