[英]postgres: when index is used
為了更好地了解在哪種情況下正確使用了索引,我想列舉一些可能的情況。
假設我們有一個包含“ a”,“ b”,“ c”,“ d”列的表。
我們在(a,b,c,d)上創建索引:
create index my_index on table my_table (a, b, c, d)
是否在以下情況下使用:
1)
where a=% and b=% and c=% and d=%
2)
where a=% and b=%
3)
where a=%
4)
where b=% and c=% and d=%
5)
where c=% order by b
6)
where a=% and b=% and c=% order by case when d is not null then d else c end
7)假設現在我們有7點的更多列,但索引僅在(a,b,c,d)上
where a=% and b=% and c=% and d=% and e=% and f=% and g=%
MySQL有很好的文檔說明了多列索引。 規則在數據庫之間基本相同(盡管有一些更高級的東西)。
當然,還有其他因素-例如from
子句中發生的情況,選擇了哪些列,有關表的統計信息等等。
以下是基本指導:
1) where a=% and b=% and c=% and d=%
是的,只要所有條件都是平等的。 我不知道排序規則沖突會怎樣。
2)其中a =%,b =%
是的,只要所有條件都是平等的。 我不知道排序規則沖突會怎樣。
3)其中a =%
是的,只要所有條件都是平等的。 我不知道排序規則沖突會怎樣。
4)其中b =%,c =%和d =%
可能不是。 如果使用索引,則需要對其進行掃描。 如果索引覆蓋了查詢,就會是這種情況。
5)其中c = b的%階
可能不是。
6)其中,當d不為null時,a =%且b =%且c =%依次排序,則d else c結束
應該用於where
子句。 仍然需要某種排序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.