I'm calling ExecuteSqlCommand several thousands times per minute and each one is a round-trip to the database. The command itself calls a stored procedure and the parameters vary from INTs to NVARCHARS to "Structured" table-value parameters. When I look at the SQL commands generated by Entity Framework via SQL Profiler quite a sizeable chunk of data is being sent to the DB.
Given that there is no return value from the SP, it's very much "fire and forget", I'd like to try to batch up the SQL commands and send, say, a batch of 10 at once. Is this possible using Entity Framework? Is there a way to get Entity Framework to return me the full SQL command per call and then I could concatenate the SQL myself and call ExecuteSqlCommand myself?
EntityFramework currently does not support batching. However if you are just executing SQL queries then I don't see a benefit of using EntityFramework. You can go one level below to pure ADO.NET where it is supported to send Sql commands in batches. Take a look at this MSDN post for more details: http://msdn.microsoft.com/en-us/library/aadf8fk2.aspx
Related
I'm experiencing some performance issues with EF and was wondering... well ... why.
The query I am running is simply:
var procs = ctx.Procedures
.Include(p => p.ProcedureProcedureFields.Select(ppf => ppf.ProcedureField))
.Where(p => p.IsActive)
.Where(p => !p.ProcedureLogbookTypes1.Any()).ToList();
So, not even passing in any parameters, which rules out a out of the issues. If I take the SQL from SQL Profiler and run it directly in SSMS, it takes less than 1s.
The EF call takes about 12s for the procs variable to be populated.
A few more things.
I have not just run the sql after the EF for comparison. I've made sure that there was no plan in cache. In fact, I've done both. I've run the SQL when a plan is in cache and when not. Any and every combination of running the 2 things yields the same results. The raw SQL is a fraction of the time of the EF query.
I'm all for using Stored Procedures where the query is uber-complex and the requirement for customizing the SQL is required for performance.
But the query above is simple. The SQL generated is simple.
I'd rather not litter my database with a million little stored procs, just because I cannot figure out how to make EF perform.
Is there a way to speed this up?
Thanks
You can use AsNoTracking as described by this answer (which is sourced from this article):
Entity Framework exposes a number of performance tuning options to help you optimise the performance of your applications. One of these tuning options is .AsNoTracking(). This optimisation allows you to tell Entity Framework not to track the results of a query. This means that Entity Framework performs no additional processing or storage of the entities which are returned by the query. However, it also means that you can't update these entities without reattaching them to the tracking graph.
I use MS data access application block for interaction with database and I saw its performance is good. When I like to add 100 or more records then I send those 100 records in xml format to a stored procedure and from there I do a bulk insert. Now I have to use Entity Framework. I haven't ever used EF before so I am not familiar with EF and how it works.
In another forum I asked a question like "How Entity Framework works in case of batch insert and update data" and got answer
From my experience, EF does not support batch insert or batch update.
What it does is that it will issue an individual insert or update statement, but it will wrap all of them in a transaction if you add all of your changes to the dbcontect before calling SaveChanges().
Is it true that EF can not handle batch insert/update? In case of batch insert/update EF inserts data in loop? If there are 100 records which we need to commit at once then EF can not do it?
If it is not right then please guide me how one should write code as a result EF can do batch insert/update. Also tell me the trick how to see what kind of SQL it will generate.
If possible please guide me with sample code for batch insert/update with EF. also tell me which version of EF support true batch operation. Thanks
Yes EF is not a Bulk load, Update tool.
You can of course put a a few K entries and commit (SaveChanges)
But when you have serious volumes of speed is critical, use SQL.
see Batch update/delete EF5 as an example on the topic
I'm using ADO.NET Entity Framework for our business application in ASP.NET website. We're using WCF and LINQ to query the data source. My problem is that data loading from database (for e.g. to load data in gridview) is taking much more time than expected, so we want to log statements in ado.net generated sql statement so we can see which query is taking more time .
How to do this?
I would strongly suggest that you use SQL Profiler rather than creating your own logging mechanisms.
Microsoft SQL Server Profiler is a graphical user interface to SQL
Trace for monitoring an instance of the Database Engine or Analysis
Services. You can capture and save data about each event to a file or
table to analyze later. For example, you can monitor a production
environment to see which stored procedures are affecting performance
by executing too slowly.
In your C# application, in your ConnectionString, add Application Name=yourApp. This will make it easier to locate in SQL Profiler.
Is there a way to see the underlying SQL sentence when executing a stored procedure in Entity Framework (3.5)?
To use the stored procedure I did from the diagram: Add, Function Import… etc
Thanks
UPDATE 1
I downloaded 'AnjLab Sql Profiler' from.
http://code.google.com/p/sqlexpressprofiler/downloads/list
And was able to see that the stored procedure is executed correctly.
You can use any type of database profiler - for example SQL profiler for SQL server or you can use either EFTracingProvider or any EF profiler (these tools are usually commercial). Here is whole article about these techniques.
In Link-2-SQL, I can use the DataContext.Log property to see the exact queries that are getting thrown to SQL Server.
Is there an equivalent to this in Entity Framework?
ObjectQuery.ToTraceString()
Since Entity Framework supports multiple backends (as opposed to Linq-to-SQL which is SQL Server only), you can't really get the actual SQL being sent to the backend server from EF.
In order to really see what's going on, I'd recommend firing up SQL Profiler on the SQL Server backend, and see what queries get sent its way.
See this article on Simple-Talk and possibly this video series on becoming a SQL Profiler master if you're not familiar with the SQL Profiler tool.
Marc