[英]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.