繁体   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