none
First query to a local sql server run very slow , others almost instantly RRS feed

  • Question

  • Hello !

    I'm working with entity framework 6.0.2 and sql server 2008r2 ( installed on local machine).

    I'm facing a strange situation :

    The first query that is executed on my application , run very slow  ( For example query=(from t in context.MyDB1 select t).tolist)For the second time , even if I execute a different query (query2=(From t in context.MyDB2 select t).tolist ) , this run almost instantly.

    Why !

    What can I do ?

    Thank you !

    Friday, March 21, 2014 2:20 PM

Answers

  • Hi decode25;

    In the link I posted you will find a table at the top and under the column called Operation find an entry "Generating views", which is posted below.

    Before the Entity Framework can execute a query against a conceptual model or save changes to the data source, it must generate a set of local query views to access the database. Because of the high cost of generating these views, you can pre-generate the views and add them to the project at design-time. For more information, see How to: Pre-Generate Views to Improve Query Performance (Entity Framework).

    Follow the link to, "How to: Pre-Generate Views to Improve Query Performance (Entity Framework)", to see if pre generating the model helps.

    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, March 26, 2014 8:42 PM

All replies

  • Hi dcode25;

    To your first question, "Why?", The very first time your application needs to build the model in memory of the entities a large amount of entities will take a bit longer then just one or two. Once that is done then it needs to build the query, into SQL, then connect to the server and get the results then it needs to materialize all the results into objects and return it to the caller. The second time and thereafter it does not build the model but everything else still needs to be done.

    To your second question, "What can I do?", please see this Microsoft documentation, Performance Considerations (Entity Framework)

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, March 21, 2014 3:06 PM
  • Thank you !

    But is any way that I can force the application to build the model at application start (for example on my login page) , so after the users begin to work the queries are faster ?

    Thank you !

    Wednesday, March 26, 2014 6:41 PM
  • Hi decode25;

    In the link I posted you will find a table at the top and under the column called Operation find an entry "Generating views", which is posted below.

    Before the Entity Framework can execute a query against a conceptual model or save changes to the data source, it must generate a set of local query views to access the database. Because of the high cost of generating these views, you can pre-generate the views and add them to the project at design-time. For more information, see How to: Pre-Generate Views to Improve Query Performance (Entity Framework).

    Follow the link to, "How to: Pre-Generate Views to Improve Query Performance (Entity Framework)", to see if pre generating the model helps.

    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, March 26, 2014 8:42 PM