我有一個表有以下欄目:聯合索引的性能與可選的where子句
id-> PK
customer_id-> index
store_id-> index
order_date-> index
last_modified-> index
other_columns...
other_columns...
我有三個單列索引。我還有一個customer_id_store_id索引,它是引用其他表的外鍵約束。
id,customer_id,store_id是char(36),它是UUID。 order_date是datetime,last_modifed是UNIX時間戳。
我想通過刪除所有索引並添加一個(customer_id,store_id,order_date)來獲得一些性能。大多數查詢將在where子句中包含這些字段。但有時store_id將不再需要。
什麼是最好的方法?在where子句中添加「store_id IS NOT NULL」或以這種方式創建索引(customer_id,order_date,store_id)。
我還經常需要通過last_modified字段(where子句包括customer_id =,store_id =,last_modified>)來查詢表。
由於我只有一個列索引,並且有數百個插入/更新表的客戶,所以索引往往會掃描超過需要的行。創建另一個索引(customer_id,store_id,last_modified)還是保持原樣?或者將此列添加到前一個索引中,使其成爲四列組合索引。但是再次order_date在這裏是不相關的,省略它可能會導致索引不能按照預期使用。
查詢對沒有多行的客戶可能使用customer_id索引的速度很快。但對於擁有大量數據的客戶來說,這並非最佳選擇。更多的時候我只需要幾天的數據。
任何人都可以請告知什麼是這種情況下最好的索引。
除非您顯示您希望優化的查詢,否則幾乎不可能幫助您。話雖如此,許多單列索引通常被認爲是有害的。 –
請考慮對您的問題進行編輯以提供更多信息。另外,請閱讀。 https://meta.stackoverflow.com/questions/271055/tips-for-asking-a-good-structured-query-language-sql-question/271056#271056 –