我目前正在爲我的web應用程序使用db4o存儲做一些研究。我很高興db4o是如何輕鬆工作的。因此,當我閱讀我喜歡的Code First方法時,因爲使用EF4 Code First的方式與使用db4o非常相似:創建您的域對象(POCO's),將它們扔到db4o,並且永不回頭。爲什麼EF4 Code First在存儲對象時如此緩慢?
但是,當我做了一個性能比較,EF 4是非常緩慢。我無法弄清楚爲什麼。
我用下面的實體:
public class Recipe { private List _RecipePreparations; public int ID { get; set; } public String Name { get; set; } public String Description { get; set; } public List Tags { get; set; } public ICollection Preparations { get { return _RecipePreparations.AsReadOnly(); } }
public void AddPreparation(RecipePreparation preparation)
{
this._RecipePreparations.Add(preparation);
}
}
公共類RecipePreparation { 公共字符串名稱{;組; } public String Description {get;組; } public int評級{get;組; } public List Steps {get;組; } public List Tags {get;組; } public int ID {get;組; }}
爲了測試我新了一個配方的性能,並增加50.000 RecipePrepations。然後,我存儲對象db4o中像這樣:
IObjectContainer db = Db4oEmbedded.OpenFile(Db4oEmbedded.NewConfiguration(), @"RecipeDB.db4o");
db.Store(recipe1);
db.Close();
這需要約13.000(MS)
我的東西與EF4存儲在SQL Server 2008(Express中,本地)是這樣的:
cookRecipes.Recipes.Add(recipe1);
cookRecipes.SaveChanges();
而這需要200.000(MS)
現在如何在地球上是db4o的15(!!!)倍的速度是EF4/SQL?我是否錯過EF4的祕密渦輪按鈕?我甚至認爲db4o可以做得更快?由於我不初始化數據庫文件,我只是讓它動態增長。
我的猜測是,正在執行許多單個插入語句的開銷是差異最大的部分。有沒有辦法指示EF4合併插入語句以減少開銷? – 2010-07-27 11:46:40
@Lasse:是的,有。 EF實現了開箱即用的工作模式 - 請參閱我的答案。 – 2010-07-27 11:48:46
我已經做了一些與Visual Studio分析。 cookRecipes.Recipes.Add(recipe1)約佔總時間的65%存儲,SaveChanges約爲35%(duh ...;))。 – Saab 2010-07-27 13:22:35