SPATIAL INDEX

Description

On MyISAM, Aria and InnoDB tables, MariaDB can create spatial indexes (an R-tree index) using syntax similar to that for creating regular indexes, but extended with the SPATIAL keyword. Currently, columns in spatial indexes must be declared NOT NULL.

Spatial indexes can be created when the table is created, or added after the fact like so:

  • with CREATE TABLE:
    CREATE TABLE geom (g GEOMETRY NOT NULL, SPATIAL INDEX(g));
    
  • with ALTER TABLE:
    ALTER TABLE geom ADD SPATIAL INDEX(g);
    
  • with CREATE INDEX:
    CREATE SPATIAL INDEX sp_index ON geom (g);
    

SPATIAL INDEX creates an R-tree index. For storage engines that support non-spatial indexing of spatial columns, the engine creates a B-tree index. A B-tree index on spatial values is useful for exact-value lookups, but not for range scans.

For more information on indexing spatial columns, see CREATE INDEX.

To drop spatial indexes, use ALTER TABLE or DROP INDEX:

Data-at-Rest Encyption

Before MariaDB 10.4.3, InnoDB's spatial indexes could not be encrypted. If an InnoDB table was encrypted and if it contained spatial indexes, then those indexes would be unencrypted.

In MariaDB 10.4.3 and later, if innodb_checksum_algorithm is set to full_crc32 or strict_full_crc32, and if the table does not use ROW_FORMAT=COMPRESSED, then InnoDB spatial indexes will be encrypted if the table is encrypted.

See MDEV-12026 for more information.

Comments

 
5 years, 3 months ago Chris Hennick

What's the real world impact of a spatial index on what's probably its most-common practical application, SELECT ... ORDER BY ST_DISTANCE(?, col)?

 
5 years, 6 months ago yegor k

one should be aware of https://bugs.mysql.com/bug.php?id=76384 which also affects mariadb (atleast 10.3.16)

 
10 years, 5 months ago Michael Fröhler

It would be a great deal if spatial indexes would be possible on on columns that allow NULL values.

Spatial Indexes only on NOT NULL columns are only suitable for about 20% of the gis data we work with.

Don´t know why it has been developed that way. Datasets that have a NULL - Geometry simply can be ignored by the index.

 
10 years, 5 months ago Federico Razzoli

As a MariaDB user I agree, but did you consider using a special geometry value instead of NULL? For example, a POINT(0,0). Of course this can be done if you'll never need a "real" point at 0, 0.

 
5 years, 3 months ago Chris Hennick

Why not use otherwise-out-of-bounds instead? For geocoordinates measured in degrees, one could use POINT(-720,-720).

 
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.
Back to Top