sql - Does the server execution plan use the index
问题描述
I have added an index on the column “LastChangeDate” in table dbo.Employee. Will the SQL Server execution plan use this index in both the cases below? Can somebody explain why or why not.
SELECT EmployeeName
FROM dbo.Employee
WHERE DATEADD(MONTH, -2, LastChangeDate) >= ‘2016-01-01’
SELECT EmployeeName
FROM dbo.Employee
WHERE LastChangeDate >= ‘2016-01-01’
解决方案
The first cannot use an index, because LastChangeDate
is the argument to a function. The only function (as far as I know) in SQL Server that does not disallow an index is conversion to a date.
The second might or might not use an index. If you have a clustered index on LastChangeDate
, then it should. However, the query is presumably not very selective -- meaning that it selects a significant portion of all rows. In such circumstances, the database optimizer may decide to forego the index and do a full table scan.
推荐阅读
- python - 如何在 Python 中将任意变量设置为全局变量?
- python - 在嵌套 for 循环中声明嵌套对象 - python 3
- java - Spring EntityManager.persist 问题
- angular - 从 Angular 中的 ng-bootstrap 弹出
- c# - 恒定引用的最佳方法或替代方法是什么?
- python - 使用 CGI Python 脚本将 SQL 查询存储为变量
- jsp - javax.servlet.jsp.JspTagException:不知道如何迭代 <forEach> 中提供的“项目”
- jquery - 模块和子模块文件完全异步加载
- git - Git签名提交问题
- java - if (条件 || 条件) vs if (条件) if (条件)