Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a major performance gain by using stored procedures?

Is it better to use a stored procedure or doing it the old way with a connection string and all that good stuff? Our system has been running slow lately and our manager wants us to try to see if we can speed things up a little and we were thinking about changing some of the old database calls over to stored procedures. Is it worth it?

like image 985
Josh Mein Avatar asked Nov 28 '08 13:11

Josh Mein


People also ask

What is the main advantage of stored procedure?

To help you build powerful database applications, stored procedures provide several advantages including better performance, higher productivity, ease of use, and increased scalability.

How stored procedures and functions improve performance?

They reduce the number of calls to the database and decrease network traffic by bundling commands.

Which is more performance efficient query or stored procedure?

“Stored procedures are precompiled and cached so the performance is much better.” The stored procedure is stored in a pre-compiled form. we can't require write code again and again.


3 Answers

The first thing to do is check the database has all the necessary indexes set up. Analyse where your code is slow, and examine the relevant SQL statements and indexes relating to them. See if you can rewrite the SQL statement to be more efficient. Check that you aren't recompiling an SQL (prepared) statement for every iteration in a loop instead of outside it once.

Moving an SQL statement into a stored procedure isn't going to help if it is grossly inefficient in implementation. However the database will know how to best optimise the SQL and it won't need to do it repeatedly. It can also make the client side code cleaner by turning a complex SQL statement into a simple procedure call.

like image 51
JeeBee Avatar answered Oct 08 '22 13:10

JeeBee


I would take a quick look at Stored Procedures are EVIL.

like image 31
mattruma Avatar answered Oct 08 '22 12:10

mattruma


So long as your calls are consistent the database will store the execution plan (MS SQL anyway). The strongest remaining reason for using stored procedures are for easy and sure security management.

If I were you I'd first be looking for adding indices where required. Also run a profiling tool to examine what is taking long and if that sql needs to changed, e.g. adding more Where clauses or restricting result set.

You should consider caching where you can.

like image 33
dove Avatar answered Oct 08 '22 13:10

dove