Entity Framework 4.0 を使用して SQL クエリを生成する、以下の 2 つのクエリ メソッドのうち、大幅に高速なのはどれですか? 1 つ目は 2 つの「Using」ブロックを使用し、2 つ目は 1 つだけを使用します。どちらの方法もデータベースへの 2 回のトリップが必要ですが、最初の方法では接続が 2 回閉じられ、2 番目の方法では接続が 1 回閉じられます。2 つのメソッドの速度はほぼ同じであり、実行速度に大きな違いはないと思いますが、本当でしょうか? ただし、これらのメソッド (Web サービス メソッド) が非常に頻繁に (1 秒間に約 2000 回) アクセスされていると仮定すると、パフォーマンスが少しでも向上します。では、バージョン I のコードをバージョン II として書き直す必要がありますか?
ありがとうございました。
//VERSION I: TWO USING BLOCKS IN SERIES, THE OUTPUT OF THE FIRST AFFECTING THE SECOND
List<Categories> CategoryList = new Categories();
using (EntityFramework1 context = new EntityFramework1())
{
try
{
var Cats = from C in context.Categories
select C);
CategoryList = Cats.ToList();
}
catch (Exception) {}
}
}
} //end of first using block
using (EntityFramework1 context = new EntityFramework1())
{
try
{
Var CustomersFromZipCOde = context.Customers.Where (custo => custo.CustomerID==customerIDString && custo.ZipCode.StartsWith(ZipCodeString) && custo.Customer_Categories.Any(categ => CategoryList.Contains(categ.CategoryID)));
//gives the right output, with CategoryList filled by the earlier query
}
catch (Exception) { }
}
} //end of second using block
//VERSION II: ONE USING BLOCK ONLY--is this significantly faster than Version II?
List<Categories> CategoryList = new Categories();
try
{
using (EntityFramework1 context = new EntityFramework1())
{
var Cats = from C in context.Categories
select C);
CategoryList = Cats.ToList();
Var CustomersFromZipCOde = context.Customers.Where (custo => custo.CustomerID==customerIDString && custo.ZipCode.StartsWith(ZipCodeString) && custo.Customer_Categories.Any(categ => CategoryList.Contains(categ.CategoryID)));
//gives the right output, with CategoryList filled by the earlier query
}
}
catch (Exception) { }