1. 程式人生 > >Index 組合索引 Composite Index 中多個欄位的順序

Index 組合索引 Composite Index 中多個欄位的順序

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow

也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!

               

一個表中有三個欄位,XX,YY,ZZ,如果要建立給這三個欄位建組合索引(Composite Index),組合索引中欄位的順序應該遵循怎樣一個原則。

一般的原則:越離散的欄位越靠前。哪個列可以降低索引的掃描成本就放在前面。

比如:下位三個欄位的離散情況
XX:2
YY:1000
ZZ:50000

那麼建立索引的順序應該為:ZZ,YY,XX

CREATE INDEX t_idx ON t (zz,yy,xx);


但是如果where條件中,三個欄位的條件都是通過"="號連線的,那麼組合索引中欄位的順序就是無所謂了。

Refer:https://forums.oracle.com/forums/thread.jspa?threadID=2425684

Because you are using equality conditions in your predicate, then the order should be XX, YY, ZZ for maximum compression. Others are suggesting ZZ should be first because it is more selective (probably). But if you are using equals (=) for all 3, then that really does not matter.


或者It’s Less Efficient To Have Low Cardinality Leading Columns In An Index (Right) ?  (輕功需好)

In actual fact, there’s no real difference in navigating to the specific leaf block of interest for an index on (ID, CODE) compared to an index based on (CODE, ID), providing both indexed columns are known.


再或者 http://www.oraclemagician.com/white_papers/index_order.pdf

Consider the following indexes:
INDEX1: (ZIP_CODE, GENDER)
INDEX2: (GENDER, ZIP_CODE)
Assume we are looking for the combination of Zip_Code = 94568 and Gender = ‘Male.’ Before Oracle traverses the index, it combines the two conditions. The composite value, something like 94568_Male has the same discriminatory value as Male_94568.

           

給我老師的人工智慧教程打call!http://blog.csdn.net/jiangjunshow

這裡寫圖片描述