代码之家  ›  专栏  ›  技术社区  ›  SoftwareGeek

sql-datetime变量与datetime变量的字符串表示法

  •  4
  • SoftwareGeek  · 技术社区  · 16 年前

    当搜索参数碰巧是带有日期的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
    

    为什么一个跑得好而另一个跑不好?

    1 回复  |  直到 16 年前
        1
  •  5
  •   John Saunders    16 年前

    因为对于varchar,它必须转换为date。这需要时间,可能会妨碍索引的有效使用。

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