简体   繁体   English

SQL Server:为什么不转换为日期使用索引而不转换为nvarchar?

[英]SQL Server : why convert to date use index and convert to nvarchar not?

I have the following 2 queries with function on the index, one do index seek and one index scan: 我有以下两个具有索引功能的查询,一个查询执行索引查找,一个查询扫描:

This uses the index: 这使用索引:

select num_of_row 
from test0
where convert(date, index_date) = '2017-06-13'

This does not use the index: 这不使用索引:

select num_of_row 
from test0
where convert(nvarchar, index_date, 103) = '2017-06-13'

Why is the index used when I convert to date , but not when I convert to nvarchar ? 为什么在转换为date时使用索引,而在转换为nvarchar时不使用索引?

通常,如果希望字段可保存(=使用索引),则不能对字段使用任何函数,但是datetime-> date是例外之一,很可能是因为可以保证顺序相同(在日期和日期时间),但不适用于日期时间和(n)varchars。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM