【问题标题】:sql - datetime variable versus string representation of datetime variablesql - 日期时间变量与日期时间变量的字符串表示
【发布时间】:2010-05-06 03:52:44
【问题描述】:

当搜索参数恰好是带日期的 varchar 数据类型时,我有一个查询需要很长时间才能响应。但是,如果我将 varchar 转换为 datetime 变量,则查询运行良好。例如:

这需要很长时间。

select count(id)
  from names
 where updateddate > '1/5/2010'

运行良好。

declare @dateparam datetime
    set @dateparam = convert(datetime, '1/5/2010',102)

select count(id)
  from names
 where updateddate > @dateparam

一个运行正常而另一个运行不正常的原因是什么?

【问题讨论】:

  • 所以,现在您知道为什么 DBMS 会为您提供 DATETIME 等类型;它使计算更加高效。

标签: sql string datetime date


【解决方案1】:

因为在 varchar 的情况下,它必须转换为日期。这需要时间,并且可能会妨碍索引的有效使用。

使用正确的类型几乎总是最好的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    • 2016-06-23
    • 1970-01-01
    • 2016-04-02
    • 2012-10-03
    • 2020-03-29
    • 2020-03-31
    相关资源
    最近更新 更多