The syntax for creating a spatial index in OceanBase Database is different from that in Oracle Database. You need to specify the spatial reference identifier (SRID) of a spatial index column when you create a table in OceanBase Database.
Notice
A spatial index created in OceanBase Database is not supported in Oracle Database. For each spatial index created in Oracle Database, the mapping between the spatial index and the index information must be manually maintained in the USER_SDO_GEOM_METADATA system table. OceanBase Database in Oracle mode uses the spatial index syntax of MySQL and does not maintain mappings between spatial indexes and index information in the USER_SDO_GEOM_METADATA table. Therefore, the syntax of spatial indexes in OceanBase Database is inconsistent with that in Oracle Database.
Considerations
- The SRID must be specified when you create a spatial index.
- You can create only a local spatial index on a partitioned table. In other words, you must explicitly specify the
LOCALkeyword.
Preparations
Before you use the Geographic Information System (GIS) feature, you need to configure GIS metadata in the business tenant. Connect to the server and run the following command to import the default_srs_data_mysql.sql file to the database:
## Replace $password with the login password of the database.
## Replace $localhost with the IP address of the database.
## Replace $tenant_name with the tenant name of the database.
## Replace $port 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 successfully imported.
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
The following example shows how to create a spatial index.
-- Specify the SRID of the index column during table creation.
CREATE TABLE geom (id NUMBER PRIMARY KEY,
name VARCHAR2(32),
shape SDO_GEOMETRY SRID 4326);
-- Insert two test data records into the table.
INSERT INTO geom VALUES(1,'a',SDO_GEOMETRY(2003,4326,NULL,SDO_ELEM_INFO_ARRAY(1,1003,1),SDO_ORDINATE_ARRAY(0,0, 0,1, 1,1, 1,0, 0,0)));
INSERT INTO geom VALUES(2,'a',SDO_GEOMETRY(2001,4326, sdo_point_type(0, 0, NULL), NULL, NULL));
-- Create a spatial index.
CREATE INDEX g ON geom(shape) INDEXTYPE IS MDSYS.SPATIAL_INDEX;
The following example shows how to create a spatial index on a partitioned table.
-- Specify the SRID of the index column when you create a partitioned table.
CREATE TABLE geom_par (id NUMBER PRIMARY KEY,
name VARCHAR2(32),
shape SDO_GEOMETRY SRID 4326)
PARTITION BY HASH(id) PARTITIONS 2;
-- Insert two test data records into the table.
INSERT INTO geom_par VALUES(1,'a',SDO_GEOMETRY(2003,4326,NULL,SDO_ELEM_INFO_ARRAY(1,1003,1),SDO_ORDINATE_ARRAY(0,0, 0,1, 1,1, 1,0, 0,0)));
INSERT INTO geom_par VALUES(2,'a',SDO_GEOMETRY(2001,4326, sdo_point_type(0, 0, NULL), NULL, NULL));
-- Create a local spatial index by specifying the `LOCAL` keyword.
CREATE INDEX g ON geom_par(shape) INDEXTYPE IS MDSYS.SPATIAL_INDEX LOCAL;
The following example shows how to drop a spatial index:
DROP INDEX g;
References
For more information about SRID, see SRS.
For more information about index-based queries, see Query functions.