OceanBase Database allows you to create a spatial index by using the SPATIAL keyword. When you create a table, the spatial index column must be declared as NOT NULL. You can create a spatial index on a stored (STORED) generated column, but not on a virtual (VIRTUAL) generated column.
Considerations
- The column definition for creating a spatial index must include the
NOT NULLconstraint. - The column on which a spatial index is created must have an SRID defined. Otherwise, the spatial index on this column will not take effect during queries.
- If you create a spatial index on a stored generated column, you must explicitly specify the
STOREDkeyword during the column creation DDL operation. If you do not specify theVIRTUALorSTOREDkeyword when creating a generated column, a virtual generated column will be created by default. - After an index is created, the SRID in the column definition is used for coordinate system comparison. A spatial index stores the MBR (Minimum Bounding Rectangle) of a geometry object. The MBR comparison method also depends on the SRID.
Prerequisites
Before you use the GIS feature, you need to configure GIS meta data in the business tenant. After you connect to the server, execute the following command to import the default_srs_data_mysql.sql file into the database:
## $password needs to be replaced with the password for logging in to the database.
## $localhost needs to be replaced with the IP address for logging in to the database.
## $tenant_name needs to be replaced with the tenant name for logging in to the database.
## $port needs to be replaced with the port number of the database.
$python /home/admin/oceanbase/bin/import_srs_data.py -p'$password' -h $localhost -P $port -t $tenant_name -f /home/admin/oceanbase/etc/default_srs_data_mysql.sql
If the following result is returned, the data file is imported successfully:
INFO: succeed to import srs data
INFO: oceanbase.__all_spatial_reference_systems old result rows -- 1
INFO: oceanbase.__all_spatial_reference_systems new result rows -- 5152
Examples
Create a spatial index
The following example shows how to create a spatial index:
- Use the
CREATE TABLEstatement:
CREATE TABLE geom (g GEOMETRY NOT NULL SRID 4326, SPATIAL INDEX(g));
- Use the
ALTER TABLEstatement:
CREATE TABLE geom (g GEOMETRY NOT NULL SRID 4326);
ALTER TABLE geom ADD SPATIAL INDEX(g);
- Use the
CREATE INDEXstatement:
CREATE TABLE geom (g GEOMETRY NOT NULL SRID 4326);
CREATE SPATIAL INDEX g ON geom (g);
The following example shows how to drop a spatial index:
- Use the
ALTER TABLEstatement:
ALTER TABLE geom DROP INDEX g;
- Use the
DROP INDEXstatement:
DROP INDEX g ON geom;
Create a spatial index on a generated column
A generated column is a special type of column in a database table. For more information, see Operations on generated columns.
The following example shows how to create a spatial index on a stored generated column:
- Create a spatial index on a generated column of the
LINESTRINGtype. Other types such asPOINT/POLYGON/ | MULTIPOINT/MULTILINESTRING/ | MULTIPOLYGONare also supported:
CREATE TABLE `receivable_items` (
`from_unit` int NOT NULL,
`to_unit` int NOT NULL,
`unit_range` linestring GENERATED ALWAYS AS (linestring(point(-(1),`from_unit`), point(1,`to_unit`))) STORED NOT NULL srid 0,
SPATIAL KEY `idx_unit_range` (`unit_range`)
);
- Create a spatial index on a generated column of the
GEOMETRYtype:
CREATE TABLE `receivable_items` (
`id` int(32) NOT NULL auto_increment,
`geo_text` varchar(1024) NOT NULL,
`unit_range` geometry GENERATED ALWAYS AS (st_geomfromtext(geo_text, 4326, 'axis-order=long-lat')) STORED NOT NULL srid 4326
);
- A spatial index cannot be created on a virtual generated column. The creation statement will return an error:
CREATE TABLE `receivable_items` (
`id` int(32) NOT NULL auto_increment,
`geo_text` varchar(1024) NOT NULL,
`unit_range` geometry GENERATED ALWAYS AS (st_geomfromtext(geo_text, 4326, 'axis-order=long-lat')) NOT NULL srid 4326,
SPATIAL KEY `idx_unit_range` (`unit_range`)
);
ERROR 3106 (HY000): 'unit_range' is not supported for generated columns.
- Support for creating an index after the column is created:
CREATE TABLE `receivable_items` (
`id` int(32) NOT NULL auto_increment,
`geo_text` varchar(1024) NOT NULL,
`unit_range` geometry GENERATED ALWAYS AS (st_geomfromtext(geo_text, 4326, 'axis-order=long-lat')) STORED NOT NULL srid 4326
);
INSERT INTO receivable_items(geo_text) VALUES('point(120.34904267189361 30.320965261625222)');
INSERT INTO receivable_items(geo_text) VALUES('point(120.34904267189360 30.320965261625222)');
CREATE SPATIAL INDEX IF NOT EXISTS `idx_unit_range` ON `receivable_items` (`unit_range`);
- Support for partitioned tables:
CREATE TABLE `receivable_items` (
`id` int(32) NOT NULL auto_increment primary key,
`geo_text` varchar(1024) NOT NULL,
`unit_range` geometry GENERATED ALWAYS AS (st_geomfromtext(geo_text, 4326, 'axis-order=long-lat')) STORED NOT NULL srid 4326,
SPATIAL KEY `idx_unit_range` (`unit_range`)
) PARTITION BY hash(id) PARTITIONS 3;