views:

1148

answers:

5

Hi, I am trying to develop my first web project using the entity framework, while I love the way that you can use linq instead of writing sql, I do have some severe performance issuses. I have a lot of unhandled data in a table which I would like to do a few transformations on and then insert into another table. I run through all objects and then inserts them into my new table. I need to do some small comparisons (which is why I need to insert the data into another table) but for performance tests I have removed them. The following code (which approximately 12-15 properties to set) took 21 seconds, which is quite a long time. Is it usually this slow, and what might I do wrong?

        DataLayer.MotorExtractionEntities mee = new DataLayer.MotorExtractionEntities();
        List<DataLayer.CarsBulk> carsBulkAll = ((from c in mee.CarsBulk select c).Take(100)).ToList();


        foreach (DataLayer.CarsBulk carBulk in carsBulkAll)
        {

                DataLayer.Car car = new DataLayer.Car();
                car.URL = carBulk.URL;

                car.color = carBulk.SellerCity.ToString();
                car.year = ... more properties is set this way

     mee.AddToCar(car);
    }
    mee.SaveChanges();
A: 

I have been experimenting with the entity framework quite a lot and I haven't seen any real performance issues.

Which row of your code is causing the big delay, have you tried debugging it and just measuring which method takes the most time?

Also, the complexity of your database structure could slow down the entity framework a bit, but not to the speed you are saying. Are there some 'infinite loops' in your DB structure? Without the DB structure it is really hard to say what's wrong.

Wim Haanstra
Wim, the database structure is very simple, there is no relations at all, and only about 15 fields in it, which is why I think it is wierd it takes 10-20 sec to save changes for only 100 rows?
Since your post, I have been speedtesting it a bit more over here, with a database we have running for client. This is a pretty complex database, and creating a 100 new rows, with all kinds of relations takes just 3 seconds.I must say, I have the database running locally, so that speeds up thiings
Wim Haanstra
A: 

Wrong post.

Do not respond to people as an answer. Respond to them as a comment on their answer. Answers are for answers. Please delete your answer, then repost it as a comment.
Mystere Man
A: 

can you try the same in straight SQL?

The problem might be related to your database and not the Entity Framework. For example, if you have massive indexes and lots of check constraints, inserting can become slow.

I've also seen problems at insert with databases which had never been backed-up. The transaction log could not be reclaimed and was growing insanely, causing a single insert to take a few seconds.

Trying this in SQL directly would tell you if the problem is indeed with EF.

Denis Troller
A: 

I think I solved the problem. I have been running the app locally, and the database is in another country (neighbor, but never the less). I tried to load the application to the server and run it from there, and it then only took 2 seconds to run instead of 20. I tried to transfer 1000 records which took 26 seconds, which is quite an update, though I don't know if this is the "regular" speed for saving the 1000 records to the database?

A: 

You cannot create batch updates using Entity Framework.

Imagine you need to update rows in a table with a SQL statement like this:

UPDATE table SET col1 = @a where col2 = @b

Using SQL this is just one roundtrip to the server. Using Entity Framework, you have (at least) one roundtrip to the server loading all the data, then you modify the rows on the client, then it will send it back row by row.

This will slow things down especially if your network connection is limited, and if you have more than just a couple of rows.

So for this kind of updates a stored procedure is still a lot more efficient.

markus