Syntax structure: create an index
CREATE [UNIQUE] INDEX index_name ON table_name(column_name[,column_name…])
Grammar analysis:
1. UNIQUE: The value on the specified index column must be unique. called a unique index.
2. index_name: Specify the index name.
3. tabl_name: Specifies which table to create the index for.
4. column_name: Specify which column to create the index on. We can also create indexes on multiple columns; such indexes are called composite indexes.
Case 4: Create a unique index for the ENAME column of the EMP table, create a common index for the salary column of the EMP table, and change the JOB column to lowercase before creating the index.
Code Demo: Create Index
SQL> CREATE UNIQUE INDEX UQ_ENAME_IDX ON EMP(ENAME); ① Index created SQL> CREATE INDEX IDX_SAL ON EMP(SAL); ② Index created SQL> CREATE INDEX IDX_JOB_LOWER ON EMP(LOWER(JOB)); ③ Index created |
Code analysis:
① Create a unique index for the ENAME column of the SCOTT.EMP table.
② Create an index for the SAL column of the SCOTT.EMP table.
③ In the query, the lowercase of the job may often be used as the expression of the condition, so when creating an index, you can first convert all the values in the JOB column to lowercase and then create the index, and then you need to use the lower function. This index is called Function-based indexing.
When a select statement is used to query, the Oracle system will automatically apply an index to the column on the query condition. The index is to sort a certain column, so the less duplicate values on the index column, the more obvious the effect of the index.
Oracle can create bitmap indexes on some columns with very many repeated and limited values (such as gender columns). For more Oracle index types (such as reverse key indexes, etc.), please refer to Oracle's official documentation.
Oracle index (Index) creation and use
Oracle Object Tutorial: Index Creation and Use, Index: Sort some columns in a database table to improve query efficiency.
当我们在某本书中查找特定的章节内容时,可以先从书的目录着手,找到该章节所在的页码,然后快速的定位到该页。这种做法的前提是页面编号是有序的。如果页码无序,就只能从第一页开始,一页页的查找了。
数据库中索引(Index)的概念与目录的概念非常类似。如果某列出现在查询的条件中,而该列的数据是无序的,查询时只能从第一行开始一行一行的匹配。创建索引就是对某些特定列中的数据排序,生成独立的索引表。在某列上创建索引后,如果该列出现在查询条件中,Oracle会自动的引用该索引,先从索引表中查询出符合条件记录的ROWID,由于ROWID是记录的物理地址,因此可以根据ROWID快速的定位到具体的记录,表中的数据非常多时,引用索引带来的查询效率非常可观。
·如果表中的某些字段经常被查询并作为查询的条件出现时,就应该考虑为该列创建索引。
·当从很多行的表中查询少数行时,也要考虑创建索引。有一条基本的准则是:当任何单个查询要检索的行少于或者等于整个表行数的10%时,索引就非常有用。
Oracle数据库会为表的主键和包含唯一约束的列自动创建索引。索引可以提高查询的效率,但是在数据增删改时需要更新索引,因此索引对增删改时会有负面影响。