索引的建立和使用

2021-09-12 05:24:18 字數 1012 閱讀 4697

1.[color=red]建立索引實際上也是個排序的過程,在索引的列上進行排序之後,以平衡樹結構自左向右將索引資訊儲存在磁碟上。[/color]

2.[color=red]索引包括列索引,函式索引和復合索引。[/color]

列索引:create index idx_col on tbl1(col1)

函式索引:create index idx_fn on tbl1(upper(col1))

復合索引:create index idx_com on tbl1(col1,col2)

[color=red]注:復合索引的列順序很重要,一般最常用的列放在最前面[/color]

3.[color=red]索引掃瞄方式[/color]

a.index range scan

b.index full scan

c.index fast full scan(自左向右有序掃瞄,而index full scan是無序掃瞄)

d.index skip scan

4.[color=red]強制利用索引查詢的sql:hint[/color]

select /*+ index(table_name index_name) */ * 

from table_name

where ...

[color=red]注:以注釋的方式啟用索引,即使語法有錯誤,也不會影響查詢[/color]

5.[color=red]利用索引主要是為了提高查詢效率,但是也不是絕對的[/color]

當查詢的資料佔總資料的比重小的時候,利用索引會提高查詢效率

當查詢的資料佔總資料的比重很大的時候,利用索引反而會降低效率

另外,有索引雖然會提高查詢效率,但同時會降低資料變更的效率,

因為在變更資料的同時,要維護索引資訊(即還要變更索引資訊)。

6.[color=red]索引列上不能用表示式(包括函式),否則索引實效。除非建立了函式索引。[/color]

SQL Server 索引的建立和使用

什麼是索引 拿漢語字典的目錄頁 索引 打比方 正如漢語字典中的漢字按頁存放一樣,sql server中的資料記錄也是按頁存放的,每頁容量一般為4k 為了加快查詢的速度,漢語字 詞 典一般都有按拼音 筆畫 偏旁部首等排序的目錄 索引 我們可以選擇按拼音或筆畫查詢方式,快速查詢到需要的字 詞 同理,sq...

建立和檢視索引

create table table name 屬性名1 資料型別,屬性名2 資料型別,屬性名3 資料型別,index index name 屬性1 例如 create table t dept deptno int,dname varchar 20 loc varchar 20 index ind...

各類索引的建立和使用方法

1 各種索引的建立方法 1 tree索引。create index indexname on tablename columnname columnname.2 反向索引。create index indexname on tablename columnname columnname.revers...