索引的創建分類:
1、自動創建
創建主鍵、唯一鍵的同時自動創建對應字段的索引
2、手動創建
用create index、create table手動創建索引
手動創建索引:
在創建表的同時爲主鍵創建索引
create table new_emp (employee_id number(6) constraint emp_id_pk primary key using index(create index emp_id_idx on new_emp(employee_id)), first_name varchar2(20), last_name varchar2(25) );
可以通過 USER_INDEXES 查看已經創建的索引信息;如果不執行主鍵約束名,系統會自動分配
index
select index_name,index_type,tablespace_name,table_name from USER_INDEXES where table_name='NEW_EMP';
執行結果:
INDEX_NAME INDEX_TYPE TABLESPACE_NAME TABLE_NAME
------------------------ ---------------------- ------------------------------------ ----------------------
EMP_ID_IDX NORMAL USERS NEW_EMP
可以通過 USER_CONSTRAINTS 查看已經創建的約束的信息
select OWNER,table_name,constraint_name,constraint_type,index_owner,index_name from user_constraints where owner='HR' and constraint_name like '___\_DEPT%' ESCAPE '\';
運用索引實例:
想快速導入一個大數據的時候,可以先禁止約束條件然後導入完之後在啓動
--step 1 create table new_emp2 (employee_id number(6), first_name varchar2(20), last_name varchar2(25) ); --step 2 create index emp_id_idx2 on new_emp2(employee_id); --step 3 alter table new_emp2 add primary key(employee_id) using index emp_id_idx2;
基於函數的索引:
基於函數的索引是基於表達式的
例如:
create index upper_dept_name_idx on new_dept(UPPER(department_name));
index UPPER_DEPT_NAME_IDX 已創建。
SELECT index_name,index_type,table_owner,table_name FROM USER_INDEXES where table_name='NEW_DEPT';
INDEX_NAME INDEX_TYPE TABLE_OWNER TABLE_NAME
----------------------------------------- ------------------------------------------- ----------------------------------- ---------------------
UPPER_DEPT_NAME_IDX FUNCTION-BASED NORMAL HR NEW_DEPT
Note: 必須把QUERY_REWRITY_ENABLED這個初始化參數設置爲TRUE纔可以使用基於函數的索引
The Oracle Server treats indexs with columns marked DESC as function-based indexs.
The columns marked DESC are sorted in descending order.