簡體   English   中英

SQL:非聚集索引組多列與單列

[英]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_IdGLTrans顯示查詢。 另一方面,查詢是超時。

我的問題:

  1. 如果我將關鍵字分為多列,則必須在where關鍵字中使用它們嗎?
  2. 如果我取消分組並為每個單列創建非聚集索引,它與上面有何不同?

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM