Entity Framework query performance - linq

I have a problem with a quite complex query executed through Entity Framework that takes so much time, almost 50 seconds. The query is executed with an ad-hoc call to a web service which creates a new ObjectContext, execute the query and returns the result.
The problem is that if I trace with SQL Server Profiler the T-SQL code and try to execute it from SQL Server Management Studio it takes like 2 seconds... what could it be?
Thank you,
Marco

For every ObjectContext that touches the database, Entity does a lot of startup work building an internal representation of the database schema. This can take a long time (our project is about 30 seconds), and is rolled into the expense of the first query made against the database. Subsequent ones are plenty fast, until the process is restarted. Does that apply to you?

Related

Oracle 12c: SQL query hangs forever only occasionally

I have a SQL query that fetches roughly 200 columns from multiple tables and normally runs in a matter of minutes.
A Java program kicked off by cron calls the SQL every 4 hours, but occasionally hangs forever(=not fetching any data. Neither updates nor inserts are involved).
Here are some outputs from V$SESSION.
STATUS: ACTIVE
ROW_WAIT_OBJ#: 22392 ←not changing
ROW_WAIT_FILE#: 6 ←not changing
ROW_WAIT_BLOCK#: 8896642 ←not changing
ROW_WAIT_ROW#: 0 ←not changing
LAST_CALL_ET: 5632 ←keeps incresing
★No other heavy SQL queries are running at the same time
What could be the cause of this and what should I look into to solve it?
You can use TKPROF or SQL Profiler. This reports can help you. We can not replay your question now.
If you attach your tuning reports, we can help you. Because many things can cause performance problems. A comprehensive study is needed to understand this.
Follow this link;
https://docs.oracle.com/cd/E11882_01/server.112/e41573/perf_overview.htm

SQL Server 2000 query performance

We have migrated our few serevr to named instance and I have a situation where a a Stored procedure is taking more time for execution.
stored procedure has some bussiness logic pointing to a table. I have indexes on the table. My doubt how same stored procedure, used on same table with same index in two different production databases taking different times. I understand that database performnce depend upon load onto the datbase. But I am exeuting in non bussiness hours and I think load also almost similar. Its taking 10sec for execution in new named instancwe server and 3 sec in old server. Do I need to defragment the table in new server. will it solve problem. Any idea how shall I check where is the problem in new server.
Edit: when I checked the execution plan it was giving 38% time of exection in eager spool(to create temorary index). Can you please explain me how I can avoid this part in exceutiuon.
I am not getting this whhile executing to the non named instance server(where its taking 3 sec for execution)
Edit: will re building of indexes make any performance improvement
First check : check the execution plans for the queries on either server - do they match?
Edit : Plans do not match, so next thought is the schema (indexes) are not identical, or the stats on the newer instance are not up to date. try an sp_updatestats on the newer instance and see if the plan changes.

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?)
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
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.
Maybe you have issue with a DB table that you are running your query against of. So first time you run EF it compiles your query, creates execution plan, etc, so when you are running second time DB uses cached version of your query. Try to add indexes to your table, and see if this helps.

measure linq to sql performance and stats

I have a web app that creates a DataContext at the beginning of the request and lets go at the end.
I would like to have some handy stats for each page like
- number of inserts and time spent
- number of deletes and time spent
- number of updates and time spent
- number of selects and time spent
I have it all set for inserts/updates/deletes by implementing the partial methods InsertXXX/UpdateXXX/DeleteXXX and keeping track of counts and time spent.
But, how do I count and time the SELECTs ???
I am not sure there is a hook anywhere in Linq to SQL to be able to insert some measuring?
Thanks
To get an idea of how long each of the queries you are running is taking, you can run the SQL Profiler on the database you are working with. You can use the Query Execution Plan to narrow down any performance iussues.
If you need to integrate this more closely with your repository/data access code, you could use the Stopwatch class to time the execution of your Linq-to-SQL methods.
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx

Entity framework and performance

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();
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.
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.
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.
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?

Resources