Optimizing Spatial Analysis
999
• With
CREATE TABLE
:
CREATE TABLE geom (g GEOMETRY NOT NULL, SPATIAL INDEX(g)) ENGINE=MyISAM;
• With
ALTER TABLE
:
ALTER TABLE geom ADD SPATIAL INDEX(g);
• With
CREATE INDEX
:
CREATE SPATIAL INDEX sp_index ON geom (g);
For
MyISAM
tables,
SPATIAL INDEX
creates an R-tree index. For storage engines that support
nonspatial indexing of spatial columns, the engine creates a B-tree index. A B-tree index on spatial
values will be useful for exact-value lookups, but not for range scans.
For more information on indexing spatial columns, see
Section 13.1.8, “
CREATE INDEX
Syntax”
.
To drop spatial indexes, use
ALTER TABLE
or
DROP INDEX
:
• With
ALTER TABLE
:
ALTER TABLE geom DROP INDEX g;
• With
DROP INDEX
:
DROP INDEX sp_index ON geom;
Example: Suppose that a table
geom
contains more than 32,000 geometries, which are stored in the
column
g
of type
GEOMETRY
. The table also has an
AUTO_INCREMENT
column
fid
for storing object
ID values.
mysql>
DESCRIBE geom;
+-------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+----------------+
| fid | int(11) | | PRI | NULL | auto_increment |
| g | geometry | | | | |
+-------+----------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
mysql>
SELECT COUNT(*) FROM geom;
+----------+
| count(*) |
+----------+
| 32376 |
+----------+
1 row in set (0.00 sec)
To add a spatial index on the column
g
, use this statement:
mysql>
ALTER TABLE geom ADD SPATIAL INDEX(g);
Query OK, 32376 rows affected (4.05 sec)
Records: 32376 Duplicates: 0 Warnings: 0
12.16.6.2. Using a Spatial Index
The optimizer investigates whether available spatial indexes can be involved in the search for queries
that use a function such as
MBRContains()
[996]
or
MBRWithin()
[997]
in the
WHERE
clause.
The following query finds all objects that are in the given rectangle:
mysql>
SET @poly =
->
'Polygon((30000 15000,31000 15000,31000 16000,30000 16000,30000 15000))';
mysql>
SELECT fid,AsText(g) FROM geom WHERE
->
MBRContains(GeomFromText(@poly),g);
+-----+---------------------------------------------------------------+
| fid | AsText(g) |
+-----+---------------------------------------------------------------+
Summary of Contents for 5.0
Page 1: ...MySQL 5 0 Reference Manual ...
Page 18: ...xviii ...
Page 60: ...40 ...
Page 396: ...376 ...
Page 578: ...558 ...
Page 636: ...616 ...
Page 844: ...824 ...
Page 1234: ...1214 ...
Page 1427: ...MySQL Proxy Scripting 1407 ...
Page 1734: ...1714 ...
Page 1752: ...1732 ...
Page 1783: ...Configuring Connector ODBC 1763 ...
Page 1793: ...Connector ODBC Examples 1773 ...
Page 1839: ...Connector Net Installation 1819 2 You must choose the type of installation to perform ...
Page 2850: ...2830 ...
Page 2854: ...2834 ...
Page 2928: ...2908 ...
Page 3000: ...2980 ...
Page 3122: ...3102 ...
Page 3126: ...3106 ...
Page 3174: ...3154 ...
Page 3232: ...3212 ...