简体   繁体   English

Datetime2与SQL Server中的字符串性能

[英]Datetime2 vs. String performance in SQL Server

What are the advantages of having a column of type datetime2 in SQL Server instead of a string? 在SQL Server中使用类型为datetime2的列而不是字符串有什么好处? Obviously strings are much more flexible. 显然,字符串要灵活得多。 Are there performance advantages while querying? 查询时有性能优势吗?

I'm designing a database where all entries are added via functions in another language and so there's no concern that a value not formatted like a date will be added to the column in question. 我正在设计一个数据库,其中所有条目都通过另一种语言的函数添加,因此不必担心未格式化为日期的值将被添加到所讨论的列中。 I'm considering making that column type string so I can more easily parse them when querying. 我正在考虑使该列类型为字符串,以便在查询时可以更轻松地解析它们。 When they are added to the database they are converted from Python's datetime format, which by convention has six digits of precision in fractions of seconds, to datetime2 format which has seven. 将它们添加到数据库后,它们会从Python的datetime格式(按惯例在几分之一秒内具有六位数的精度)转换为具有七位的datetime2格式。 Making the column be of type string will stop that from happening, but I'm worried about the performance issues that might come with not using datetime2. 将列设置为字符串类型将阻止这种情况的发生,但是我担心不使用datetime2可能会导致性能问题。

Datetime2 (and other date/time types): Datetime2(和其他日期/时间类型):

  • will save you storage space and that will eventually become a performance gain. 将节省您的存储空间,最终将提高性能。
  • will allow you to easily do date math without having to create procedures that need to figure if February will have 28 or 29 days (just to give an example). 这样您就可以轻松进行日期数学运算,而不必创建需要确定2月是28天还是29天的程序(仅举一个例子)。
  • have their built-in constraints so you don't have to worry about someone inserting an invalid date. 具有内置的限制,因此您不必担心有人插入无效的日期。
  • can be easily converted to different formats 可以轻松转换为不同格式
  • can have their parts extracted, including weeks, weekdays or days of the year. 可以提取其部分,包括一年中的几周,工作日或几天。
  • and so on... 等等...

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

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