This topic describes how to create an index with some examples.
Note
This topic mainly introduces how to create indexes by using the CREATE INDEX statement. For other methods of creating indexes, see CREATE TABLE or ALTER TABLE.
Overview
An index, also known as a secondary index, is an optional structure. OceanBase Database uses the clustered index table model. The system automatically generates a primary key index for the specified primary key, and other indexes that you create are secondary indexes. You can determine the fields on which indexes are to be created based on business needs to speed up queries on these fields.
For more information about indexes in OceanBase Database, see About indexes.
Prerequisites
Before you create an index, make sure that:
You have deployed an OceanBase cluster and created an Oracle tenant. For more information about how to deploy an OceanBase cluster, see Deployment overview.
You have connected to the Oracle tenant of OceanBase Database. For more information about how to connect to OceanBase Database, see Overview of connection methods.
You have created a table. For more information, see Create a table.
You have the
INDEXprivilege and theALTERprivilege on the table on which an index is to be created. For more information about how to view your privileges, see View user privileges. If you do not have the required privileges, contact the administrator to obtain the privileges. For more information, see Grant direct privileges.
Limitations
In OceanBase Database, the index name must be unique within the table.
The length of the index name cannot exceed 128 bytes.
Multiple unique indexes can be created on a table, but each unique index must ensure the uniqueness of the corresponding column values.
If you want to use a local unique index to enforce data uniqueness, the local unique index must include the table partition key.
Considerations
We recommend that you use names that succinctly describe the columns covered by the index and its purpose, for example,
idx_customer_name. For more information about naming conventions, see Object naming conventions.If the partition rules for a global index are the same as those for the primary table and have the same number of partitions, we recommend that you create a local index.
We recommend that you limit the number of SQL statements for parallel index creation to the maximum number of CPU cores specified in the tenant's unit specifications. For example, if the tenant's unit specification is 4 CPU cores (4C), it is recommended to have no more than 4 index creation statements executed concurrently.
We recommend that you create indexes on fields that are frequently used for queries, but do not create excessive indexes on tables that are frequently updated.
Do not create indexes on tables with a small amount of data. For a table with a small data amount, it may take a shorter time to query all the data than to traverse the indexes. In this case, indexes cannot produce optimization effects.
If modification requirements are far more than retrieval requirements, do not create indexes.
Create efficient indexes:
Indexes must be created on all the columns to be queried. This can reduce the number of rows returned from table access by index primary key.
Indexes of equivalent conditions should always be placed in the front of the index table.
Indexes for large data filtering and sorting should be placed in the front of the index table.
Create an index by using a statement
You can use the CREATE INDEX statement to create an index.
Note
You can use the USER_INDEXES view to query information about the indexes in the table.
Examples
Example 1: Create a unique index
If there is a need to ensure that the values in an indexed column are unique, a UNIQUE index can be created.
Use the following SQL statements to create a table named test_tbl1 and to create a unique index based on the col2 column in test_tbl1.
Create a table named
test_tbl1.CREATE TABLE test_tbl1(col1 NUMBER, col2 NUMBER, col3 VARCHAR2(50), PRIMARY KEY(col1));Create a unique index named
idx_test_tbl1_col2on thecol2column in thetest_tbl1table.CREATE UNIQUE INDEX idx_test_tbl1_col2 ON test_tbl1(col2);Query indexes of the
test_tbl1table.SELECT INDEX_NAME,INDEX_TYPE,TABLE_OWNER,TABLE_NAME,UNIQUENESS FROM user_indexes WHERE table_name='TEST_TBL1';The result is as follows:
+---------------------------------+------------+-------------+------------+------------+ | INDEX_NAME | INDEX_TYPE | TABLE_OWNER | TABLE_NAME | UNIQUENESS | +---------------------------------+------------+-------------+------------+------------+ | TEST_TBL1_OBPK_1703316804944854 | NORMAL | SYS | TEST_TBL1 | UNIQUE | | IDX_TEST_TBL1_COL2 | NORMAL | SYS | TEST_TBL1 | UNIQUE | +---------------------------------+------------+-------------+------------+------------+ 2 rows in set
Example 2: Create a non-unique index
Execute the following SQL statements to create a table named test_tbl2 and create an index on the col2 column in the test_tbl2 table.
Create a table named
test_tbl2.CREATE TABLE test_tbl2(col1 NUMBER, col2 NUMBER, col3 VARCHAR2(50), PRIMARY KEY(col1));Create an index named
idx_test_tbl2_col2on thecol2column in thetest_tbl2table.CREATE INDEX idx_test_tbl2_col2 ON test_tbl2(col2);Query indexes of the
test_tbl2table.SELECT INDEX_NAME,INDEX_TYPE,TABLE_OWNER,TABLE_NAME,UNIQUENESS FROM user_indexes WHERE table_name='TEST_TBL2';The result is as follows:
+---------------------------------+------------+-------------+------------+------------+ | INDEX_NAME | INDEX_TYPE | TABLE_OWNER | TABLE_NAME | UNIQUENESS | +---------------------------------+------------+-------------+------------+------------+ | TEST_TBL2_OBPK_1703317409002143 | NORMAL | SYS | TEST_TBL2 | UNIQUE | | IDX_TEST_TBL2_COL2 | NORMAL | SYS | TEST_TBL2 | NONUNIQUE | +---------------------------------+------------+-------------+------------+------------+ 2 rows in set
Example 3: Create a local index
A local index is created on data in a single partition. The key-value pairs of the index and the data in the table are in a one-to-one match. Each index partition maps to one table partition. They share the same partitioning rules. Therefore, a local unique index is only guaranteed to be unique in a partition. Its uniqueness within the table is not guaranteed. The key word for creating a local index is LOCAL.
To use a local unique index as a unique constraint on a table, the local unique index must contain a partitioning key of the table.
Execute the following SQL statements to create a subpartitioned table named tbl3_f_rl and create a local unique index on the col1 and col2 columns in the tbl3_f_rl table.
Create a RANGE-LIST-subpartitioned table named
tbl3_f_rlwithout using a template.CREATE TABLE tbl3_f_rl(col1 NUMBER,col2 NUMBER) PARTITION BY RANGE(col1) SUBPARTITION BY LIST(col2) (PARTITION p0 VALUES LESS THAN(100) (SUBPARTITION sp0 VALUES(1,3), SUBPARTITION sp1 VALUES(4,6), SUBPARTITION sp2 VALUES(7,9)), PARTITION p1 VALUES LESS THAN(200) (SUBPARTITION sp3 VALUES(1,3), SUBPARTITION sp4 VALUES(4,6), SUBPARTITION sp5 VALUES(7,9)) );Create an index named
idx_tbl3_f_rl_col1_col2on thecol1andcol2columns in thetbl3_f_rltable.CREATE UNIQUE INDEX idx_tbl3_f_rl_col1_col2 ON tbl3_f_rl(col1,col2) LOCAL;Query indexes of the
tbl3_f_rltable.SELECT INDEX_NAME,INDEX_TYPE,TABLE_OWNER,TABLE_NAME,UNIQUENESS FROM user_indexes WHERE table_name='TBL3_F_RL';The result is as follows:
+-------------------------+------------+-------------+------------+------------+ | INDEX_NAME | INDEX_TYPE | TABLE_OWNER | TABLE_NAME | UNIQUENESS | +-------------------------+------------+-------------+------------+------------+ | IDX_TBL3_F_RL_COL1_COL2 | NORMAL | SYS | TBL3_F_RL | UNIQUE | +-------------------------+------------+-------------+------------+------------+ 1 row in set
Example 4: Create a global index
The keyword for creating a global index is GLOBAL. Unlike a local index, the partitioning of a global index is independent of the partitioning of the table. You can specify the partitioning rules and the number of partitions for a global index. These rules and this number do not have to be the same as those of the table.
If you do not specify the LOCAL or GLOBAL attribute for an index, OceanBase Database in Oracle mode defines this index as a
GLOBALindex, and the index table has only one partition.The partitioning rules of a global index are not necessarily the same as those of the table.
If a global index has the same partitioning rules and the same number of partitions as the table, we recommend that you create a local index in this case. The reasons are twofold. First, global indexes cost higher to maintain. Second, the physical storage location of a table and a global index are not necessarily the same, unless you manually include them in the same table group.
Execute the following SQL statements to create a partitioned table named tbl4_h and create a global index on the col2 column in the tbl4_h table.
Create a HASH-partitioned table named
tbl4_h.CREATE TABLE tbl4_h(col1 NUMBER PRIMARY KEY,col2 NUMBER) PARTITION BY HASH(col1) PARTITIONS 5;Create a RANGE-partitioned global index named
idx_tbl4_h_col2on thecol2column in thetbl4_htable.CREATE INDEX idx_tbl4_h_col2 ON tbl4_h(col2) GLOBAL PARTITION BY RANGE(col2) (PARTITION p0 VALUES LESS THAN(100), PARTITION p1 VALUES LESS THAN(200), PARTITION p2 VALUES LESS THAN(300) );Query indexes of the
tbl4_htable.SELECT INDEX_NAME,INDEX_TYPE,TABLE_OWNER,TABLE_NAME,UNIQUENESS FROM user_indexes WHERE table_name='TBL4_H';The result is as follows:
+------------------------------+------------+-------------+------------+------------+ | INDEX_NAME | INDEX_TYPE | TABLE_OWNER | TABLE_NAME | UNIQUENESS | +------------------------------+------------+-------------+------------+------------+ | TBL4_H_OBPK_1703321659273683 | NORMAL | SYS | TBL4_H | UNIQUE | | IDX_TBL4_H_COL2 | NORMAL | SYS | TBL4_H | NONUNIQUE | +------------------------------+------------+-------------+------------+------------+ 2 rows in set
Example 5: Create a function-based index
A function-based index is created based on the result of any function applied to a column or multiple columns. Function-based indexing is an optimization technique. You can use function-based indexes to quickly locate function values that match query conditions. This avoids repeated calculation and improves query efficiency.
OceanBase Database in Oracle mode imposes limitations on expressions of function-based indexes. Specifically, the expressions of some system functions cannot be used as function-based indexes. For more information about the relevant functions, see System functions supported for function-based indexes and System functions not supported for function-based indexes.
Execute the following SQL statements to create a table named test_tbl5 and create a function-based index on the id column in the test_tbl5 table.
Create a table named
test_tbl5.CREATE TABLE test_tbl5(id NUMBER, name VARCHAR2(18));Create an index named
idx_test_tbl5_idon theidcolumn in thetest_tbl5table.CREATE INDEX dx_test_tbl5_id ON test_tbl5 ((id+1));Query the created function-based index.
SELECT INDEX_NAME,INDEX_TYPE,TABLE_OWNER,TABLE_NAME,UNIQUENESS FROM user_indexes WHERE table_name='TEST_TBL5';The result is as follows:
+-----------------+-----------------------+-------------+------------+------------+ | INDEX_NAME | INDEX_TYPE | TABLE_OWNER | TABLE_NAME | UNIQUENESS | +-----------------+-----------------------+-------------+------------+------------+ | DX_TEST_TBL5_ID | FUNCTION-BASED NORMAL | SYS | TEST_TBL5 | NONUNIQUE | +-----------------+-----------------------+-------------+------------+------------+ 1 row in set
Example 6: Create a spatial index
A spatial index is a database index used to process and optimize spatial data. It is widely used in geographic information systems (GIS) and for storing and querying location data. The syntax for creating a spatial index in OceanBase Database differs from that of Oracle, as the SRID of the spatial index column must be specified when the table is created.
When creating a spatial index, note the following:
- Before using the GIS feature, you need to configure GIS meta data in the business tenant. For detailed steps, see Preparations for creating a spatial index.
- For constraints related to creating a spatial index on partitioned tables, see Limitations on creating a spatial index.
Create a table named
test_tbl6.obclient [test]> CREATE TABLE test_tbl6(id NUMBER PRIMARY KEY, name VARCHAR2(32), shape SDO_GEOMETRY SRID 4326);Create a spatial index named
idx_tbl6_gon the shape column of thetest_tbl6table.obclient [test]> CREATE INDEX idx_tbl6_g ON test_tbl6(shape) INDEXTYPE IS MDSYS.SPATIAL_INDEX;View the index information of the
test_tbl6table.obclient [test]> SELECT INDEX_NAME,INDEX_TYPE,TABLE_OWNER,TABLE_NAME,UNIQUENESS FROM user_indexes WHERE table_name='TEST_TBL6';The return result is as follows:
+---------------------------------+------------+-------------+------------+------------+ | INDEX_NAME | INDEX_TYPE | TABLE_OWNER | TABLE_NAME | UNIQUENESS | +---------------------------------+------------+-------------+------------+------------+ | TEST_TBL6_OBPK_1718852454772761 | NORMAL | SYS | TEST_TBL6 | UNIQUE | | IDX_TBL6_G | DOMAIN | SYS | TEST_TBL6 | NONUNIQUE | +---------------------------------+------------+-------------+------------+------------+ 2 rows in set
What to do next
After creating an index, you may need to optimize query performance. For more information on SQL tuning, see SQL tuning.
References
- For more information about how to query indexes, see Query indexes.
- For more information about how to manage indexes, see DROP INDEX and Drop an index.
- For more information about unique and non-unique indexes, see Unique and non-unique indexes.
- For more information about local and global indexes, see Local and global indexes.