简体   繁体   English

小型结果集的实体框架查询速度慢

[英]Entity Framework Query Slow for Small Result Set

So I have the below EF query that returns about 12,000 records from a flat table. 所以我有下面的EF查询,它从一个平面表返回大约12,000条记录。 I use a projection that only selects the necessary fields (about 15) and then puts them into a list of a custom class. 我使用的投影仅选择必要的字段(大约15个),然后将其放入自定义类的列表中。 It takes almost 3 seconds, which seems like a long time for 12,000 records. 它花费了将近3秒,对于12,000条记录来说似乎很长一段时间。 I've tried wrapping the entire thing in a transaction scrope with "read uncommitted", and I've also tried using "AsNoTracking()". 我尝试用“读取未提交”将整个内容包装在事务scrope中,也尝试使用“ AsNoTracking()”。 Neither made any difference. 两者都没有任何区别。 Anyone have any idea why the performance on this would be so lousy? 任何人都知道为什么这样做的表现如此糟糕吗?

List<InfoModel> results = new List<InfoModel>();

        using (InfoData data = new InfoData())
        {
             results = (from S in data.InfoRecords
                        select new
                        {
                            ...bunch of entity fields...
                        }).AsEnumerable().Select(x => new InfoModel()
                        {
                            ...bunch of model fields...
                        }).ToList();
        }

It's difficult to answer, because there are soy many things that can affect, your network, the number of other request in your sqlserver or Windows server, your model, .... 很难回答,因为有很多因素会影响大豆,您的网络,sqlserver或Windows服务器中其他请求的数量,您的模型...。

Despite of the lastest versions the quality of generated queries and the performance of Entity framework has improved a lot, is far from others in terms of speed. 尽管使用了最新版本,但生成查询的质量和Entity Framework的性能已大大提高,在速度方面远远不及其他。 There are some performance considerations you can look https://msdn.microsoft.com/en-us/data/hh949853.aspx 您可以查看一些性能注意事项https://msdn.microsoft.com/zh-cn/data/hh949853.aspx

It speed matters and 3 seconds is too much for you, probably I wouldn't use Entity Framework to retrieve so many rows, for me Entity Framework is great when you need just some items but not thousands if speed is important. 它的速度很重要,而3秒对您来说太长了,可能我不会使用Entity Framework来检索这么多行,对我来说,Entity Framework非常有用,当您只需要一些项目时,如果速度很重要,则不需要数千。

To improve speed you case use many others ORM like Dapper , the one used by Stackoverflow that is pretty fast . 为了提高速度,您可以使用许多其他ORM,例如Dapper ,它是Stackoverflow使用的非常快的 ORM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM