Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stored procedures a no-go in the php/mysql world?

I'm quoting part of an answer which I received for another question of mine:

In the PHP/MySQL world I would say stored procedures are no-go

I would like to know: Is that so? Why? Why not?

[edit]I mean this as a general question without a specific need in mind[/edit]

like image 879
markus Avatar asked Sep 17 '08 13:09

markus


People also ask

How can I call stored procedure in PHP?

To call a stored procedure from a PHP application, you prepare and execute an SQL CALL statement. The procedure that you call can include input parameters (IN), output parameters (OUT), and input and output parameters (INOUT).

What is stored procedure in MySQL PHP?

A stored procedure is a subroutine stored in the database catalog. Applications can call and execute the stored procedure. The CALL SQL statement is used to execute a stored procedure. Stored procedures can have IN , INOUT and OUT parameters, depending on the MySQL version.

Where does MySQL store stored procedures?

Where are stored procedures stored? Stored procedures are stored in the mysql. routines and mysql. parameters tables, which are part of the data dictionary.

Does MySQL allow stored procedures?

MySQL permits routines to contain DDL statements, such as CREATE and DROP . MySQL also permits stored procedures (but not stored functions) to contain SQL transaction statements such as COMMIT .


4 Answers

I develop and maintain a large PHP/MySQL application. Here is my experience with stored procedures.

Over time our application has grown very complex. And with all the logic on the php side, some operations would query the database with over 100 short queries.

MySQL is so quick that the performance was still acceptable, but not great.

We made the decision in our latest version of the software to move some of the logic to stored procedures for complex operations.

We did achieve a significant performance gain due to the fact that we did not have to send data back and forth between PHP and MySQL.

I do agree with the other posters here that PL/SQL is not a modern language and is difficult to debug.

Bottom Line: Stored Procedures are a great tool for certain situations. But I would not recommend using them unless you have a good reason. For simple applications, stored procedures are not worth the hassle.

like image 58
Devon Avatar answered Oct 07 '22 11:10

Devon


When using stored procedures with MySQL, you will often need to use the mysqli interface in PHP and not the regular mysql interface.

The reason for this is due to the fact that the stored procedures often will return more than 1 result set. If it does, the mysql API can not handle it and will you get errors.

The mysqli interface has functions to handling these multiple result sets, functions such as mysqli_more_results and mysqli_next_result.

Keep in mind that if you return any result set at all from the stored procedure, then you need to use these APIs, as the stored procedure generates 1 result set for the actual execution, and then 1 additional one for each result set intentionally returned from the stored procedure.

like image 26
Harrison Fisk Avatar answered Oct 07 '22 13:10

Harrison Fisk


I generally stay away from stored procedures because it adds load to the database which is 99% of the time, your biggest bottleneck. Adding a new php server is nothing compared to making your MySQL db replicate.

like image 39
Brent Avatar answered Oct 07 '22 11:10

Brent


Do you have a specific need in mind which makes you consider them? Stored procedures are much less portable than "plain" SQL, that's usually why people don't want to use them. Also, having written a fair share of PL/SQL, I must say that the procedural way of writing code adds complexity and it's just not very modern or testable. They might be handy in some special cases where you need to optimize, but I'd certainly think twice. Jeff has similar opinions.

like image 40
auramo Avatar answered Oct 07 '22 12:10

auramo