2011-01-09 88 views
0

我有兩個字段用於搜索表格:field1,field2。它們並非通過所有記錄都是唯一的,既不是單獨的也不是組合的MySQL索引:索引2個字段的方式有什麼區別?

他們所以我添加索引:

方法之一:

alter table xx add index (field1, field2); 
alter table xx add index (field2); 

方法有兩個:

alter table xx add index (field1); 
alter table xx add index (field2); 

我的問題是什麼這兩個方法之間的差異,如果任何?對於以下每個選擇查詢:

select * from table xx where field1 = ?? 
select * from table xx where field2 = ?? 
select * from table xx where field1 = ?? and field2 = ?? 
select * from table xx where field1 = ?? or field2 = ?? 

哪種方法更好?

而且,對於此查詢,哪種方法更好?

select * from table xx where field1 = ?? and field2 = ?? 

回答

0

對於自己張貼的做法1將是更好的查詢,因爲綜合指數(field1, field2)將用於查詢與field1 = ?? and field2 = ??

由於field1 = ?? or field2 = ??查詢 - index merge將嘗試應用,並沒有按」無論哪種方法爲這種查詢選擇。

總結:如果您有field1 = ?? and field2 = ?? - 那麼您必須選擇合成索引。

相關問題