Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do we still need stored procedures when using compiled queries?

When using compiled queries in entity framework (or linq-to-sql) in combination with SQL Server, is there actually still any performance benefit in using stored procedures?

Compiled queries will be cached as parameterized queries, so performance should be near equal to stored procedures. Is there any situation where stored procedures would perform significantly better?

-- EDIT --

In response to Yakimych's answer below, I didn't mean to imply that compiled queries are the same as stored procedures. I am trying to figure out if sprocs are still necessary if you have done all possible optimizations on the application side (in this case compiled queries). So I guess I'm looking for reasons why a stored procedure would be better than the combination of application-side optimizations and parameterized queries (which is what compiled queries effectively are).

One of the reasons I'm asking this, is because there are many people who seem to think that stored proedures are no longer necessary for different reasons (i.e. this post).

like image 941
Carvellis Avatar asked Sep 27 '10 08:09

Carvellis


People also ask

Do we still use stored procedures?

Stored procedures have been falling out of favour for several years now. The preferred approach these days for accessing a relational database is via an O/R mapper such as NHibernate or Entity Framework. Stored procedures require much more work to develop and maintain.

Which is better stored procedure or query?

Stored procedures (SPs) are a better choice than views, but views are much better than SQL queries embedded in reports.

Why we use stored procedure instead of query?

A stored procedure is invoked as a function call instead of a SQL query. Stored procedures can have parameters for both passing values into the procedure and returning values from the call. Results can be returned as a result set, or as an OUT parameter cursor.

What happens when a stored procedure is compiled?

Stored Procedure is nothing but a precompiled SQL statement. This means that once we compile a query, it stores the result and the next time we don't need to compile it again and again. It means it is prepared SQL code that you can save and reuse later multiple times.

What is a natively compiled stored procedure in SQL?

This means that when a stored procedure is executed for the first time, the SQL Server relational engine first compiles it, and then executes it. By contrast, when we create a natively compiled stored procedure, SQL Server compiles it into machine code immediately, and stores it in a DLL.

What is a stored procedure in SQL?

What are the Benefits of using a Stored Procedure in SQL? How to Create a Simple Stored Procedure in SQL? Why Do You Use SET NOCOUNT ON? How to Create a Stored Procedure with Parameters? How to Create a Stored Procedure in SQL with Default Parameters? A stored procedure in SQL is a group of SQL statements that are stored together in a database.

Is compiledquery faster than a stored procedure?

But for maximum performance you should also evaluate Stored Procedures where you do all the processing on the database server, even if Linq tries to push as much of the work to the db as possible you will have situations where a stored procedure will be faster. No, CompiledQuery is compiled at runtime, just before the first execution.

Should I use compiled or compiled queries?

Compiled queries are compiled when the application is compiled and every time you reuse a query often or it is complex you should definitely try compiled queries to make execution faster. But I would not go for it on all queries as it is a little more code to write and for simple queries it might not be worthwhile.


2 Answers

First of all, compiling EF queries has nothing to do with performance benefits that can be achieved by using stored procedures.

According to http://msdn.microsoft.com/en-us/library/cc853327.aspx - the following operations occur when a query executes against a conceptual model:

  • Loading metadata
  • Opening the database connection
  • Generating views
  • Preparing the query
  • Executing the query
  • Loading and validating types
  • Tracking
  • Materializing the objects

And the explanation regarding Preparing the query:

Includes the costs to compose the query command, generate a command tree based on model and mapping metadata, and define the shape of the returned data. Because Entity SQL query commands are cached, later executions of the same query take even less time. You can also use compiled LINQ queries to reduce this cost in later executions.

So, if you compile the query and re-use it later, what happens is that you save time on this operation in your application during every subsequent query execution. What you don't do, however, is you don't influence the generated SQL code that is executed against the database. The performance benefits you get when compiling queries are at the application level.

On the other hand, you would typically use stored procedures in case you aren't satisfied with the generated SQL code and would like to optimize performance at the database level.

EDIT in response to your comment and edit.

It seems to me that you are under the impression that compiling an EF query would somehow change the generated SQL code that will be run against the database (you mention that compiled queries result in parametrized SQL queries?). That is not the case. No matter whether you run the query directly or use compiledQuery.Invoke, the same SQL code will be run against the DB. Furthermore, you don't have full control over it, you rather rely on the ORM to generate it in the best possible way. In some cases it is not optimal, and this is where SP's come in.

So to sum up:

  • Compiling queries is purely an application-side optimization. It saves the time to compile a query that gets re-used in the code.
  • Stored procedures can be used to tweak your SQL code and get it to match your goal as closely as possible, thus providing a possibility to get the best performance at the database level.

In no way is one technique a substitute for the other.

like image 105
Yakimych Avatar answered Oct 16 '22 16:10

Yakimych


"Is there any situation where stored procedures would perform significantly better?"

Given a comparable piece of parametrized SQL generated in either EF or a stored proc, they will perform equally.

However, a DBA always has the opportunity to further optimise a query based on their experience with the DB schema and its usage patterns. A stored procedure allows them to do this easily in isolation of the applications using it, whereas an ORM doesn't.

We have an extremely complicated SQL Server DB that has many external systems replicating data in and out via triggers. The issue for us with EF is that the responsibility for the SQL that gets fired at the DB will become the application developers responsibility when using any ORM rather than the DBAs.

like image 5
Darren Lewis Avatar answered Oct 16 '22 16:10

Darren Lewis