ORM到底哪家強? 很多人都想知道這個問題,自已也沒測試過,只能道聽途說。 閑的無聊就將幾個ORM拿出來比一比,假如懷疑測試代碼有問題可以將它下載下來慢慢研究。 參賽ORM 1、SqlSugar:是一款輕量級的MSSQL ORM ,除了具有媲美ADO的性能外還具有和EF相似簡單易用的語法,地址:ht ...
ORM到底哪家強? 很多人都想知道這個問題,自已也沒測試過,只能道聽途說。
閑的無聊就將幾個ORM拿出來比一比,假如懷疑測試代碼有問題可以將它下載下來慢慢研究。
參賽ORM
1、SqlSugar:是一款輕量級的MSSQL ORM ,除了具有媲美ADO的性能外還具有和EF相似簡單易用的語法,地址:http://www.cnblogs.com/sunkaixuan/p/5654695.html
2、Chloe ORM: 模仿EF代碼風格與設計的一款輕量級高性能ORM ,地址:http://www.cnblogs.com/so9527/
3、Dapper ORM : 是一個輕型的ORM類。代碼就一個SqlMapper.cs文件,主要是IDbConnection的擴展方法,官方站點http://code.google.com/p/dapper-dot-net/ ,也可以通過Nuget進行安裝
4、EF6.0: .NET官方ORM
測試結果:
從測試代碼可以看出 SqlSugar Dapper Chloe的性能都是差不多的,EF就有些不盡人意
將EF改成AsNoTracking又測了幾次,【查詢所有】性能有所提升但是還是最慢, 查詢單條和分頁AsNoTracking貌似沒什麼效果
測試代碼切片
using System.Data.Entity; using System.Data.Entity.ModelConfiguration.Conventions; using System.Linq; using System.Text; using System.Threading.Tasks; using SqlSugar; using SyntacticSugar; using Dapper; using System.Data.SqlClient; using System; using Chloe.SqlServer; namespace ORMTest { /// <summary> /// 查詢所有 /// </summary> public class SelectAll { public static void Execute(int count,string title) { Console.WriteLine(title); PerHelper.Execute(count, " SqlSugar Queryable ", () => { using (SqlSugarClient db = new SqlSugarClient(ConfigSugar.GetConfigString("SchoolContext"))) { var list = db.Queryable<Student>().ToList(); } }); PerHelper.Execute(count, " SqlSugar Sqlable ", () => { using (SqlSugarClient db = new SqlSugarClient(ConfigSugar.GetConfigString("SchoolContext"))) { var list = db.Sqlable().From<Student>("t").SelectToList<Student>("*"); } }); PerHelper.Execute(count, " SqlSugar SqlQuery ", () => { using (SqlSugarClient db = new SqlSugarClient(ConfigSugar.GetConfigString("SchoolContext"))) { var list = db.SqlQuery<Student>("select * from Student"); } }); PerHelper.Execute(count, " Chloe ORM Query", () => { using (MsSqlContext context = new MsSqlContext(ConfigSugar.GetConfigString("SchoolContext"))) { var list = context.Query<Student>().ToList(); } }); PerHelper.Execute(count, " Chloe ORM SqlQuery", () => { using (MsSqlContext context = new MsSqlContext(ConfigSugar.GetConfigString("SchoolContext"))) { var list = context.SqlQuery<Student>("select * from Student").ToList(); } }); PerHelper.Execute(count, " EF6.0 ", () => { using (SchoolContext sc = new SchoolContext()) { var list = sc.Students.AsNoTracking().ToList(); } }); PerHelper.Execute(count, " Dapper Sql ", () => { using (SqlConnection conn = new SqlConnection(ConfigSugar.GetConfigString("SchoolContext"))) { var list = conn.Query<Student>("select * from Student").ToList(); ; } }); Console.WriteLine(""); } } }查詢所有
using System.Data.Entity; using System.Data.Entity.ModelConfiguration.Conventions; using System.Linq; using System.Text; using System.Threading.Tasks; using SqlSugar; using SyntacticSugar; using Dapper; using System.Data.SqlClient; using System; using Chloe.SqlServer; using Chloe; namespace ORMTest { /// <summary> /// 查詢分頁 /// </summary> public class SelectPage { public static void Execute(int count,string title) { Console.WriteLine(title); PerHelper.Execute(count, " SqlSugar Queryable ", () => { using (SqlSugarClient db = new SqlSugarClient(ConfigSugar.GetConfigString("SchoolContext"))) { var list = db.Queryable<Student>().OrderBy("id").ToPageList(10,20); } }); PerHelper.Execute(count, " SqlSugar Sqlable ", () => { using (SqlSugarClient db = new SqlSugarClient(ConfigSugar.GetConfigString("SchoolContext"))) { var list = db.Sqlable().From<Student>("t").SelectToPageList<Student>("*","id",10,20); } }); PerHelper.Execute(count, " SqlSugar SqlQuery ", () => { using (SqlSugarClient db = new SqlSugarClient(ConfigSugar.GetConfigString("SchoolContext"))) { var list = db.SqlQuery<Student>("select * from(select *,row_number() over(order by id) as r from Student ) t where t.r between @b and @e",new { b=181,e=200}); } }); PerHelper.Execute(count, " Chloe ORM Queryable ", () => { using (MsSqlContext context = new MsSqlContext(ConfigSugar.GetConfigString("SchoolContext"))) { var list = context.Query<Student>().Skip(180).Take(20).ToList(); } }); PerHelper.Execute(count, " Chloe ORM SqlQuery", () => { using (MsSqlContext context = new MsSqlContext(ConfigSugar.GetConfigString("SchoolContext"))) { var list = context.SqlQuery<Student>("select * from(select *,row_number() over(order by id) as r from Student ) t where t.r between @b and @e", new DbParam[] { DbParam.Create("@b", "181"), DbParam.Create("@e", "200") }).ToList(); } }); PerHelper.Execute(count, " EF6.0 ", () => { using (SchoolContext sc = new SchoolContext()) { var list = sc.Students.OrderBy(it=>it.id).Skip(180).Take(20).AsNoTracking().ToList(); } }); PerHelper.Execute(count, " Dapper Sql ", () => { using (SqlConnection conn = new SqlConnection(ConfigSugar.GetConfigString("SchoolContext"))) { var list = conn.Query<Student>("select * from(select *,row_number() over(order by id) as r from Student ) t where t.r between @b and @e", new { b = 181, e = 200 }).ToList(); ; } }); Console.WriteLine(""); } } }查詢分頁
using System.Data.Entity; using System.Data.Entity.ModelConfiguration.Conventions; using System.Linq; using System.Text; using System.Threading.Tasks; using SqlSugar; using SyntacticSugar; using Dapper; using System.Data.SqlClient; using System; using Chloe.SqlServer; using Chloe; namespace ORMTest { /// <summary> /// 查詢單條 /// </summary> public class SelectSingle { public static void Execute(int count,string title) { Console.WriteLine(title); PerHelper.Execute(count, " SqlSugar Queryable ", () => { using (SqlSugarClient db = new SqlSugarClient(ConfigSugar.GetConfigString("SchoolContext"))) { var list = db.Queryable<Student>().Where(it=>it.id==1).ToList(); } }); PerHelper.Execute(count, " SqlSugar Sqlable ", () => { using (SqlSugarClient db = new SqlSugarClient(ConfigSugar.GetConfigString("SchoolContext"))) { var list = db.Sqlable().From<Student>("t").Where("id=@id").SelectToList<Student>("*", new { id = 1 }); } }); PerHelper.Execute(count, " SqlSugar SqlQuery ", () => { using (SqlSugarClient db = new SqlSugarClient(ConfigSugar.GetConfigString("SchoolContext"))) { var list = db.SqlQuery<Student>("select * from Student where id=@id",new { id=1}); } }); PerHelper.Execute(count, " Chloe ORM Queryable", () => { using (MsSqlContext context = new MsSqlContext(ConfigSugar.GetConfigString("SchoolContext"))) { var list = context.Query<Student>().Where(it=>it.id==1).ToList(); } }); PerHelper.Execute(count, " Chloe ORM SqlQuery", () => { using (MsSqlContext context = new MsSqlContext(ConfigSugar.GetConfigString("SchoolContext"))) { var list = context.SqlQuery<Student>("select * from Student where id=@id", DbParam.Create("@id", "1")).ToList(); } }); PerHelper.Execute(count, " EF6.0 ", () => { using (SchoolContext sc = new SchoolContext()) { var list = sc.Students.Where(it=>it.id==1).AsNoTracking().ToList(); } }); PerHelper.Execute(count, " Dapper Sql ", () => { using (SqlConnection conn = new SqlConnection(ConfigSugar.GetConfigString("SchoolContext"))) { var list = conn.Query<Student>("select * from Student where id=@id",new { id=1}).ToList(); ; } }); Console.WriteLine(""); } } }查詢單條
using System; using System.Collections.Generic; using System.Data.Entity; using System.Data.Entity.ModelConfiguration.Conventions; using System.Linq; using System.Text; using System.Threading.Tasks; using SqlSugar; using SyntacticSugar; namespace ORMTest { public class PerHelper { public static void Execute(int count, string title, Action fun) { PerformanceTest ptef = new PerformanceTest(); ptef.SetIsMultithread(false);//開啟多線程 ptef.SetCount(count);//執行count次 ptef.Execute( i => { fun(); }, res => { Console.WriteLine($"執行{count}次,{title}{res}"); }); } } }性能測試通用函數
代碼下載
測試代碼地址:https://pan.baidu.com/s/1kVnR97D