SQL Server,Oracle,DB2索引建立語句的對比
我們知道,索引是用于加速數(shù)據(jù)庫查詢的數(shù)據(jù)庫對象。原理就是減少查詢的IO操作,從而達(dá)到加速的目的。本文我們主要對SQL Server,Oracle,DB2上的索引建立語句進(jìn)行了總結(jié),接下來就讓我們一起來了解一下這部分內(nèi)容。
索引的種類:
聚集索引:根據(jù)數(shù)據(jù)行的鍵值在表或視圖中排序和存儲這些數(shù)據(jù)行.
非聚集索引:具有獨立于數(shù)據(jù)行的結(jié)構(gòu).
***索引:確保索引鍵不包含重復(fù)的值.
在SQL SERVER上建立的索引:
Create (NONCLUSTERED ) index ind_emp on emp(empno); 默認(rèn)的就是建立非聚集索引。
exec sp_helpindex emp; 用于查看建立的索引,查詢會自己使用可以用到的索引。
Create index ind_emp1 on emp(empno,ename); 可以在多個列上建立復(fù)合索引。
***非聚集索引:
Create unique index ind_sal on emp(sal);
重新生成索引:
Alter index ind_sal on emp rebuild;
Drop index ind_emp on emp; 刪除索引。
Create CLUSTERED index ind_emp on emp(empno); 建立聚集索引。
Create index ind_emp on emp(empno,ename);
注:相同列上可以多次索引。
***聚集索引:
Create unique clustered index ind_sal on f_emp(sal);
在DB2上建立索引:
非***索引:create index ind_empno on emp(empno);
Describe indexes for table emp; 查看所建立的索引。
***索引:create unique index ind_empno on emp(empno);
純索引是DB2上的一種特殊的索引,(相當(dāng)于ORACLE上的索引組織表):相對與一般索引。如下方式表中有倆個字段,其中字段1是***主鍵,字段2為數(shù)據(jù),實際的查詢中經(jīng)常是select empno,ename from emp where empno=1122;CREATE UNIQUE INDEX IDX_ENAME ON emp (empno) INCLUDE(eNAME)。上述的語句的意思就是在empno上創(chuàng)建***索引,選擇包含ename的數(shù)據(jù),這些附加的數(shù)據(jù)將與鍵存儲到一起。
Drop index ind_emp;
Create index ind_emp on emp(empno) cluster;
Create index ind_emp on emp(empno,ename);
***聚集索引:
drop index ind_emp;-- 一個表上只能有一個聚集索引。
Create unique index ind_sal on u_emp(sal) cluster; 建立聚集索引。
在ORACLE上建立索引:
SQL> create index ind1 on emp(mgr); BTree索引。
SQL> create index ind2 on emp(deptno) reverse; 反向索引。
SQL> create index ind3 on emp(hiredate desc); 降序索引。
SQL> create bitmap index ind4 on emp(sal); 位圖索引。
SQL> create index ind5 on emp upper(job); 函數(shù)索引。
關(guān)于SQL Server,Oracle,DB2上的索引建立語句的總結(jié)就介紹到這里了,希望本次的介紹能夠?qū)δ兴斋@!
【編輯推薦】