達夢數據庫索引實踐
達夢數據庫支持二級索引,聚集索引,唯一索引,函數索引,位圖索引,分區索引等。
默認的表是索引組織表,利用rowid創建一個默認的索引,所以我們創建的索引,稱爲二級索引。建索引的目的是加快表的查詢,對數據庫做DML操作的時候,數據庫會自動維護索引。索引是一棵倒置的樹,使用索引,就是對這個索引樹進行遍歷。
建立索引的規則:經常查詢的列、連接條件列、謂詞經常出現的列(where)、查詢是返回表的一小部分數據
不適合創建索引的情況:列上有大量的null、列上的數據有限(例如:性別)
1、查看索引信息
講索引之前注意一下:創建索引,刪除,重建索引和收集統計信息,不要在業務高峯去做。
查看某個用戶下的索引情況
select owner,table_name,index_name,index_type from dba_indexes where owner='TEST1';
首先創建一張來做下測試
create table TAB10 (id1 int, id2 int, id3 int, id4 int, id5 int, id6 int, id7 int, id8 int, name1 char(20), name2 varchar(30));
查詢發現,創建表的時候會默認自帶創建一個聚集索引。
select owner,table_name,index_name,index_type from dba_indexes where table_name='TAB10';
2、創建索引
創建默認索引(默認二級索引)
create or replace index index_default on TAB10 (id1);
創建聚集索引,自帶聚集索引會被替換成新創建的
create or replace cluster index index_cluster on TAB10 (id2);
創建唯一索引
create or replace unique index index_unique on TAB10 (id3);
創建函數索引
create or replace index index_function on TAB10 (id1+id2);
創建位圖索引時,提示位圖索引與聚集索引不能構建在同一表上,需要單獨的表裏創建。
create or replace bitmap index index_bitmap on TAB10 (id4);
創建分區索引
create index index_partition on TAB10 (id5) global partition by range(id5)
(
partition g_p1 values less than(10),
partition g_p2 values less than(maxvalue)
);
3、維護索引
當有數據更新時可以重建索引
alter index index_default rebuild;
索引名稱修改的語句
alter index index_partition rename to index_partition2;
刪除索引跟刪除表結構一樣的語句
drop index index_partition2;
4、查詢語句分析執行計劃
通過查詢執行計劃發現默認會使用聚集索引
explain select * from TAB10;