The SPATIAL keyword creates a special index optimized for two-dimensional geometric data (POINT, LINESTRING, POLYGON, MULTIPOLYGON, etc.). In MySQL and MariaDB it can appear in CREATE TABLE, ALTER TABLE, or CREATE INDEX statements. The index stores minimum bounding rectangles (MBRs) and lets the optimizer use R-tree search, drastically reducing the number of rows examined by spatial functions such as ST_Within, ST_Distance, or MBRContains. Key points: - Allowed only on columns of spatial types defined by OpenGIS. - Columns must be NOT NULL in most engines. - InnoDB requires the column to have an SRID and supports only one geometry column per table with a SPATIAL index prior to MySQL 8.0. - Unlike regular indexes, no length prefix is permitted. - A spatial index is ignored when the query uses functions that cannot be resolved with MBRs (e.g., exact-shape predicates without && operator). - In SQL Server and PostgreSQL the same concept exists but is implemented with CREATE SPATIAL INDEX (SQL Server) or GiST/BRIN indexes (PostGIS). The SPATIAL keyword itself is MySQL-specific.
index_name
(identifier) - Name of the spatial index------------
(------------) - ---------------------------------------table_name
(identifier) - Table that owns the geometry columngeometry_column
(column) - Column of a spatial data type to be indexedMySQL 4.1 (MyISAM), InnoDB support added in MySQL 5.7
MyISAM has supported SPATIAL indexes since MySQL 4.1. InnoDB gained full support in MySQL 5.7, provided the geometry column is NOT NULL and has an SRID.
Yes, as long as each one targets a different geometry column. You cannot add two SPATIAL indexes to the same column.
Run EXPLAIN on the statement. The Extra column should include "Using MBR" or list the spatial index name, indicating it is leveraged.
It speeds up candidate selection by bounding box, but the engine still performs exact distance calculations on the filtered set.