Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Linq to Entities so slow the first time it's referenced

using Entity Framework 4.0, it seems that the first time an operation is done (read or write) against an entity framework object context it takes orders of magnatude longer than the second time. For example a query the first time may take 10 seconds (yes seconds) and the second time .1 seconds.

I'm guessing that the first time the objectcontext is constructed it has to build some sort of behind the scene data structures? Is it parsing the EDMX file (I thought would have been done at compile time?)

like image 351
Ralph Shillington Avatar asked Feb 02 '26 20:02

Ralph Shillington


2 Answers

It is building views that get cached on subsequent calls.

You can pre-generate views to avoid the first time performance hit:

http://www.dotnetspark.com/kb/3706-optimizing-performance.aspx

like image 68
John Egbert Avatar answered Feb 04 '26 14:02

John Egbert


EF has start-up expense of loading the Entity Data Model (EDM) metadata into memory, pre-compiling views and other one-time operations, you could try using warm-up query in order to get past that.

like image 39
Kris Ivanov Avatar answered Feb 04 '26 14:02

Kris Ivanov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!