2

Visual Studio 2012 および Windows 7、64 ビット アーキテクチャ、4 コアを搭載した Entity Framework 4.0 で .net 4.0 を取得しました。

AdventureWorks データベースを使用して、コンソール アプリケーションでいくつかのテストを実行しています。私を悩ませているのは、いくつかの製品を初めて服用すると、2回目よりもはるかに時間がかかることです.理由を知りたい.

最初のクエリで Entity Framework が実行するタスクがいくつかあることはわかっているので、事前に追加のクエリを実行しました。

たとえば、私のメイン関数には次のものが含まれています。

using (var context = new Context())
{
   execute context.Clients.ToList();   // <- this takes something like 300 ms
}

using (var context = new Context())
{
   execute context.Products.ToList();   // <- this takes something like 200 ms
}

using (var context = new Context())
{
   execute context.Products.ToList();   // <- this takes something like 10 ms
}

したがって、すべての「初回実行」を含む最初のクエリが 300 ミリ秒かかっていることがわかります。

2 番目は 200 秒かかり、3 番目は 2 番目と同じで 10 ミリ秒しかかかりません。

だから私の質問は: 3 番目のクエリにかかる時間が短いのはなぜですか? 自動コンパイル (EF 4.0 には自動コンパイルがないため) や CompiledQuery クラスは使用しません。

また、Products への最初の呼び出しは、モデルに対する最初のクエリではないため、ビュー生成のすべての作業は既に完了しているはずです。

しかし、それでも最初は非常に時間がかかり、Products を呼び出す 2 回目は非常に高速です。どこかに何かをキャッシュしますか?

私が気付いたもう 1 つのことは、64 ビット用にコンパイルすると、1 秒ほどの途方もない時間がかかることです。そして、32ビット用にコンパイルすると、指定した量がかかります。

ここでヒントを教えてもらえますか?

ありがとう

4

1 に答える 1

0

Read this: Performance Considerations (Entity Framework)

There is quite a bit of work the EF has to do besides generate views.

Additionally, your first query on Products may cause your DB server to read the data into its cache. You should use SQL Server Profiler to distinguish between DB performance differences and client (EF) performance differences.

Regarding the 64 bit question, that reminded me of a previous SO question.

于 2012-10-31T20:10:02.533 に答える