Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF6 SQLQuery very slow but database is very fast

I have a performance problem we have done a bunch of analysis and are stuck. Hopefully one of you have seen this before.

I'm calling DbContext.Database.SqlQuery the database portion takes 3ms but the full execution takes 9 seconds.

We've used EF Profiler to discover this and we also run the SQL directly in SQL Server Management Studio and it is instantaneous.

We also used glimpse and couldn't see deep enough into the process.

The result type is not an entity from the model and therefore we are confident that tracking is not involved.

We also know that this is not the first query executed against the context therefore we are not paying EF startup cost on this query.

We tried the .net profiler and had so many problems running it that we decided we should just ask.

Any tips on how to dig in and figure this out ?

EDIT: The result set for this query is 1 row with 4 columns (decimal)

The line of code is just:

var list=contextInstance.Database.SqlQuery<nonEntityType>(sqstring).ToList();

The SQL itself is not a very long string. We will use a more detailed profiler to find out where in the process this is getting hung up.

like image 933
RandyDes Avatar asked Aug 06 '15 20:08

RandyDes


People also ask

Why is my SQL query so slow?

Queries can become slow for various reasons ranging from improper index usage to bugs in the storage engine itself. However, in most cases, queries become slow because developers or MySQL database administrators neglect to monitor them and keep an eye on their performance.

Is Entity Framework faster than SQL query?

Its our job as developers to stay up to date with technology. It is faster than EF. With that said, I would start with EF every time and only replace EF calls with stored procedures (maybe via dapper) in performance critical places. For normal CRUD stuff that is not querying a billion rows, EF is totally fine.

Why is EF so slow?

Answer. Entity Framework loads very slowly the first time because the first query EF compiles the model. If you are using EF 6.2, you can use a Model Cache which loads a prebuilt edmx when using code first; instead, EF generates it on startup.


1 Answers

We've used EF profiler to discover this and we also run the SQL directly in SQL server management studio and it is instantaneous.

This doesn't prove anything. The query might run fast, but the data might result in 100MB of data which is then transported to the client and materialized in objects. This might take more time than you think.

The query in SSMS might return instantaneous because it shows only part of the data. You didn't say what the data was.

Use a real .NET profiler, like dotTrace or Ants. This way you can see where time is lost exactly on the line. EF Prof (or my own ORM Profiler: http://www.ormprofiler.com) will tell you which part of the total route taken (ORM->DB->ORM) takes what time. Even EF prof does ;)

like image 155
Frans Bouma Avatar answered Oct 06 '22 01:10

Frans Bouma