3

SQL Server と ASP.NET を使用する場合、Datevsを使用する際のパフォーマンス/ストレージに関する考慮事項はありDateTimeますか?

要らなくてもDateTimeほとんどのことに使ってる

4

3 に答える 3

6

DateTime takes 8 bytes per value

Date is 3 bytes.

I can't speak for low level performance; however in general we've found it a mistake to store values as DateTime by default. Sooner or later you run into the UTC issue and have to start working out offsets for dates that have 00:00:00.000 in the time portion!

If you're just storing dates I'd stick to the Date datatype; you'll fit more rows per page and save yourself a lot of hassle

于 2012-04-13T16:38:22.630 に答える
1

Depends how many rows you're storing, and what you're using it for. Date is 3 bytes, DateTime is 8 bytes. Can quickly add up when you have billions of rows of data, or are using it for an index. Naturally there is a difference in the resolution of the value stored too. There are other date-types between date and datetime too such as smalldatetime that are more compact, again with different compromises.

SQL Date/Time Types Documentation

于 2012-04-13T16:38:53.637 に答える
1

考慮事項:

  • パフォーマンス: 日付型のビットが少ないということは、SQL ページ (データ ページまたはインデックス ページ) あたりの行が多いことを意味し、クエリの実行中に読み取られるページが少ないことを意味します。
  • 互換性:DATE型は SQL Server 2008 で導入されたため、アプリは SQL Server 2005 と互換性がなくなりました
  • .NET: .NET にクラスがありませんDate- .NET クラスDATEに変換されますDateTime
  • LINQ: LINQ2SQL (および SQL Metal ツール) はDATESQL 型に適しています
  • DATETIMEあなたは何時間も何分も切り取ることができますCAST(@myDateTimeParam AS DATE)

私の経験から: 私はこの新しい型が好きで、T-SQL や C# のプログラミング中に問題はありませんでした

これに注意してください(比較時の日付のデータ型の混合):

DECLARE @startDay DATE = '2012-04-11' -- day
DECLARE @endDay DATE = '2012-04-13' -- day
DECLARE @eventTime DATETIME = '2012-04-13 12:00' -- point in time (noon)

IF @eventTime BETWEEN @startDay AND @endDay PRINT 'In period.' ELSE PRINT 'Not in period!'

結果は次のとおりです。

Not in period!

BETWEEN比較すると、 (特定の時点まで; と共通の型)@endDayにキャストされました。DATETIME@eventTime

と比べて:

DECLARE @startDay DATE = '2012-04-11' -- day
DECLARE @endDay DATE = '2012-04-13' -- day
DECLARE @eventTime DATE = '2012-04-13' -- day

IF @eventTime BETWEEN @startDay AND @endDay PRINT 'In period.' ELSE PRINT 'Not in period!'

結果:

In period.

そしてそれで:

DECLARE @startDay DATETIME = '2012-04-11' -- day, but point in time in fact 00:00.000
DECLARE @endDay DATETIME = '2012-04-13' -- day, but point in time in fact 00:00.000
DECLARE @eventTime DATETIME = '2012-04-13' -- day, but point in time in fact 00:00.000

IF @eventTime BETWEEN @startDay AND @endDay PRINT 'In period.' ELSE PRINT 'Not in period!'

結果:

In period.
于 2012-04-13T21:47:56.140 に答える