Measurements
列を持つテーブルがありDate
ます。
この列にはインデックスが付けられています (ただし、一意のインデックスではありません)
EntityFramework を使用して、特定のmonth
andからすべてのアイテムを取得したいと考えていますyear
。
質問: パフォーマンスの点で優れている代替手段は何ですか?
代替案 1:
var results = db.Measurements.Where(m => m.Date.Month == month && m.Date.Year == year);
(結果の SQL はDATEPART
関数を使用します)
WHERE (((DATEPART (month, [Extent1].[Date])) = @p__linq__1) OR ((DATEPART (month, [Extent1].[Date]) IS NULL) AND (@p__linq__1 IS NULL))) AND (((DATEPART (year, [Extent1].[Date])) = @p__linq__2) OR ((DATEPART (year, [Extent1].[Date]) IS NULL) AND (@p__linq__2 IS NULL)))
(...「OR null」の場合がある理由はわかりませんが)
代替案 2:
DateTime firstDateOfMonth = new DateTime(year, month, 1);
DateTime lastDateOfMonth = new DateTime(year, month, DateTime.DaysInMonth(year, month));
var results = db.Measurements.Where(m => m.Date >= firstDateOfMonth && m.Date <= lastDateOfMonth);
(結果の SQL は演算子>=
and を使用します<=
)
WHERE ([Extent1].[Date] >= @p__linq__1) AND ([Extent1].[Date] <= @p__linq__2)