简体   繁体   中英

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. I use a projection that only selects the necessary fields (about 15) and then puts them into a list of a custom class. It takes almost 3 seconds, which seems like a long time for 12,000 records. I've tried wrapping the entire thing in a transaction scrope with "read uncommitted", and I've also tried using "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, ....

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. There are some performance considerations you can look https://msdn.microsoft.com/en-us/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.

To improve speed you case use many others ORM like Dapper , the one used by Stackoverflow that is pretty fast .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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