Index 是為了加速查詢的速度,它是儲存在數(shù)據(jù)庫中的實實在在的數(shù)據(jù)結(jié)構(gòu)。
通過 Index 可以快速定位到數(shù)據(jù)庫中的具體某一行
Index 的實現(xiàn)數(shù)據(jù)結(jié)構(gòu)常有
- 平衡樹(B tree, B+ tree)
對數(shù)級,(ex:A < v, A=v,A > v) - 哈希表
常數(shù)級,(ex: A=v)
Index 的壞處有
- 額外的存儲空間
- Index 創(chuàng)建需要時間(如果需要經(jīng)常查詢,這是值得的)
- Index 維護(表內(nèi)容更改時,索引也要更改)
所以,建不建立所以需要考慮如下因素
- Size of table (and possibly layout)
- Data distributions
- Query vs. update load
SQL 創(chuàng)建 Index 語法
Create Index IndexName on T(A)
Create Index IndexName on T(A1, A2,..,An)
Create Unique Index IndexName on T(A)
Drop Index IndexName