Ormlite非常慢

时间:2019-04-16 20:23:45

标签: c# ormlite-servicestack

我有一个简单的表,只有6个非常简单的字段,并且表中只有2行。运行查询

选择* FROM PersonsPreferences

来自MS SQL Server Management Studio,“客户端统计信息”报告运行时间约为0-1毫粘胶。符合预期。

但是,当我使用Ormlite时,运行时间会大大增加-大约30毫秒。我的C#Ormlite代码如下:

Stopwatch lSW = new Stopwatch();
lSW.Start();
PersonPreference lPMPExisting = db.Select<PersonPreference>().FirstOrDefault();
var e = lSW.ElapsedMilliseconds;

现在,在上面的代码中,“ e”的值约为30,远远超过了应有的值。 “ PersonPreference”是用于访问相应数据库表的非常简单的类。我使用Ormlite 5.0,最新版本是5.5,但是我的数据库访问是如此基础,以至于5.0应该已经足够好了?有什么建议吗?

最好, 比约恩

1 个答案:

答案 0 :(得分:0)

There are a couple of things. First the stopwatch:

  • use Stop() before retrieving elapsed time
  • check the Stopwatch.IsHighResolution to make sure your tests are accurate
  • run multiple tests, eliminate warm ups, calculate average timing

For the ORM itself, your Management Studio usually connects to the server instance and retains a session. As such the client statistics don’t contain connecting to the server, authentication and handshake. The code you posted hides if you open a connection to the db before initializing the stopwatch. In your example you ‘select’ / retrieve multiple PersonPreference s and apply LINQ FirstOrDefault afterwards on the collection, which has a negative impact on your performance. When you say that the execution time

is far more than it should be.

what range did you expect and did you compare the query to EF, dapper, Nhibernate or anything else?

Any ORM no matter if heavy or micro, handles multiple responsibilities and will perform poorer than a direct sql query using a dbms.