0

Why does EF(4.0) take so long to insert rows into a table? When i queried the data just after closing the application, it returned some 20,000 rows and later on I noticed, though the app was closed the data was not done inserting and the insertions were happening in the background. Why is this? What are the ways to inform the application (I am planing to run these inserts as a windows service since they are long-running) that the inserts are really completed? What are the ways to increase the speed of these inserts? I just have one foreign key relationship..so it is not a very complex data model as well. Thanks for any help!

The application is ASP.Net application. Here is the piece of code where I am calling the SaveChanges().

do
{
    if (token != null)
    {
        response = context.Execute<NetflixService.Title>(token)
                       as QueryOperationResponse<NetflixService.Title>;
    }

    using (Models.MovieContext  movieCtxt = new Models.MovieContext())
    {
        foreach (var item in response)
        {
            IList<Models.MovieGenre> genres = new List<Models.MovieGenre>();
            foreach (var genreIterator in item.Genres)
            {
                var genre = new Models.MovieGenre
                {
                    Genre = genreIterator.Name
                };
                genres.Add(genre);
            }

            //genres.Add(new Models.MovieGenre { Genre = "filmy" });
            var movie = new Models.MovieCatalog
            {
                MovieTitle = item.Name,
                MovieGenres = genres
            };

            movieCtxt.MovieCatalogs.Add(movie);
        }

        movieCtxt.SaveChanges();
    }

}
while ((token = response.GetContinuation()) != null);
Rup
  • 33,765
  • 9
  • 83
  • 112
learnnew
  • 1
  • 1
  • If it's continuing after the program has closed then it'll have to be at the database, e.g. catching up on its transaction log. What's the database? How large are the rows, how many / how complex indices do you have on the table, etc.? Isn't there a better way to insert all that data? – Rup Mar 14 '12 at 17:20
  • How do you "close" an ASP.NET application? Are you killing the web server or do you just stop debugging in Visual Studio? Stopping to debug won't stop the web server to run. The inserts are completed when `SaveChanges()` returns. For the performance you must show a sketch of your code. And what does "*take so long*" mean exactly? – Slauma Mar 14 '12 at 19:27
  • Thanks for the replies. I have added the code. Pls check. – learnnew Mar 14 '12 at 20:48
  • Slauma: I realised my mistake, I was just closing the browser.I killed the web server and it did stop. I was about to go in a different direction to fix this. Thats not necessary now. Thanks. – learnnew Mar 14 '12 at 21:08

1 Answers1

1

My guess is that your are actually using EF >= 4.1 (DbContext) and not EF 4.0 as you said (because ObjectSet<T> does not have an Add method but you are using movieCtxt.MovieCatalogs.Add(movie). In EF 4.0 you would use AddObject.)

In this case disable automatic change detection to improve the performance of bulk inserts:

using (Models.MovieContext movieCtxt = new Models.MovieContext())
{
    movieCtxt.Configuration.AutoDetectChangesEnabled = false;

    //...
}

More details are here:

As already said in the comment, it is necessary to kill the web server to really stop your application. Closing the browser or stop debugging in Visual Studio does not cause the application to terminate.

Inserting the data into the database is finished when SaveChanges() returns. SaveChanges does not batch all the INSERT statement, it actually sends them one by one to the database and finishes when the last statement is sent.

Community
  • 1
  • 1
Slauma
  • 175,098
  • 59
  • 401
  • 420