Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prepared Statement vs. Stored Procedure

Tags:

If you are using php5 and mysql5, is there a substantial advantage to using stored procs over prepared statements? ( i read somewhere you may not get substantial performance gains from mysql5 stored proc)

like image 687
John Avatar asked Oct 13 '08 03:10

John


People also ask

Which is better stored procedure or prepared statement?

The difference is you cant store prepared statements. You must "prepare" them every time you need to execute one. Stored procedures, on the other hand, can be stored, associated to a schema, but you need to know PL/SQL to write them. You must check if your DBMS supports them.

Can we call stored procedure using prepared statement?

Calling stored procedures in JDBC applications The Informix JDBC driver provides the Statement , PreparedStatement , and CallableStatement methods, which can be used to execute stored procedures.

Should I always use prepared statements?

You should always prefer working with prepared statements for the security benefits. They all but eliminate vulnerability to SQL injection, without you having to worry about SQL-escaping values. If you have a query that doesn't run often, though (less than once per request), a prepared statement can take longer to run.

What is the difference between a prepared statement and a statement?

The Key Difference between Statement and PreparedStatement is that Statement is used for executing simple SQL Statements whereas PreparedStatement is used for executing dynamic and pre-compiled SQL Statements.

What is a stored procedure in SQL Server?

A stored procedure (SP) is a server-side procedure written in SQL, which is stored in the database and is directly accessible by the database engine. It is also exposed to client applications via programming language connections. Server-side stored procedures offer many advantages over typical client side logic processing.

What is the difference between statement and PreparedStatement in SQL?

Statement : It is used for accessing your database. Statement interface cannot accept parameters and useful when you are using static SQL statements at runtime. If you want to run SQL query only once then this interface is preferred over PreparedStatement. 2. PreparedStatement : It is used when you want to use SQL statements many times.

What are pre-prepared statements in SQL Server?

Prepared statements are queries written with placeholders instead of actual values. You write the query and it is compiled just once by the DBMS, and then you just pass values to place into the placeholders. The advantage of using prepared statements is that you enhance the performance considerably, and protect your applications from SQL Injection.

What are the pros and cons of a stored procedure?

The stored procedure can never do anything that you didn't tell it to do, at an earlier time. The worst thing to happen is that someone trying to exploit the server manages to insert some garbage as his username or such. Who cares. So, you get much better security, and the server needs to do less work (i.e. queries run faster), too.


2 Answers

They are not really the same thing - with stored procedures, your database logic resides inside the database. Prepared statements basically avoid re-parsing queries if they are called multiple times - the performance benefit can vary greatly.

The choice to use one or the other is really dependent on your specific situation. I don't really use stored procs anymore as I like having all of my logic in one place.

like image 81
Toby Hede Avatar answered Sep 21 '22 18:09

Toby Hede


Stored procedures make sense for professional-grade (IE enterprise-grade) applications where you:

  1. Want to allow your database engineer to optimize queries for performance
  2. Want to abstract complexity of queries to simple API's
  3. WANT your logic distributed, because some of what happens in the database might be intellectual property that you don't want to expose to other parties
  4. WANT your logic distributed, because that is the nature of distributed, n-tier computing
  5. you might want the database engineer or DBA to modify schema without modifying application code (stored procs, by virtue of providing API's, provide a layer of abstraction)

There are other reasons.

Prepared statements are better for work done within a session. But if you are taking the time to create a prepared statement, you have essentially done everything necessary to create a stored procedure. The difference is that the stored procedure is available across multiple sessions (subject to GRANTS in the database).

What I can not figure out is that if you have the option for stored proc vs. prepared statement, why you would bother with prepared statements. Most of the SP vs PS discussions seem to focus on what the differences are between them, not on why to use one vs the other. That always appears to boil down to "depends on what you are trying to do." But I haven't seen a well organized description of: use a proc if you need VS use a statement if you need....

like image 32
user101124 Avatar answered Sep 24 '22 18:09

user101124