[英]SQL: Nonclustered index group multiple columns vs single columns
我有表GLTrans
,它具有非聚集索引:
我的查詢:
SELECT
_glAccount.[Code] as [AccountCode]
,_glTrans.[CommentBooking] as [CommentBooking]
FROM
[GLTransHeader] _this
INNER JOIN
[GLTrans] _glTrans ON _glTrans.[GLTransHeader_Id] = _this.[Id]
LEFT INNER JOIN
[GLAccount] _glAccount ON _glAccount.[Id] = _glTrans.[GLAccount_Id]
WHERE
_glTrans.Folder_Id = '3AFE5BC5-1CC7-4198-9D89-B65591624C6E'
如果我在GLTrans
表中添加Folder_Id
, GLTrans
顯示查詢。 另一方面,查詢是超時。
我的問題:
Index ON (Col1, Col2)
與“索引ON(Col2,Col1)”不同
索引ON(Col1,Col2)對於以下查詢很有用:
Select *
from YourTable
Where Col1 = ? and Col2 = ?
和以下查詢:
Select *
From YourTable
Where col1 = ?
但對於此查詢沒有用:
Select *
from YourTable
Where Col2 = ?
索引中列的優先級非常重要。
如果在select語句中選擇列,則換句話說,如果不使用include,則可以將include用於索引,sql server使用鍵鎖定來獲取數據。
例如,以下查詢需要Index on (Col1, Col2) include(Col3)
。
Select Col3
from your table
where col1 = ? and col2 = ?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.