Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Stored Procedure vs PHP script

I'm running a website for a jewellery wholesaler.

The prices on all their products are calculated using the current bullion metal fixes that are updated every night.

Currently, for the website, the calculations are worked out via a php include function, which works fine under current circumstances.

There are around 10,000 products, but the prices are calculated in real-time (ie when the web page is requested). The calculations are simple, but there are lots of them (Around 50+) and i'm worried that an increase in traffic may slow the current script down.

I'm redesigning the site and was wondering whether it would be beneficial to create a procedure in MySQL to do the calculations instead.

Is this likely to be faster that the current php script? Anyone know any good reading reference on using procedures?

like image 547
ticallian Avatar asked May 01 '09 07:05

ticallian


People also ask

Is MySQL faster than PHP?

MySQL is faster in scope of SQL query. PHP is faster in PHP code. If you make SQL query to find out SQRT() it should be definitely slower (unless PHP is broken) because MySQL parser and networking overhead.

Is using stored procedure a good practice?

Stored procedures promote bad development practices, in particular they require you to violate DRY (Don't Repeat Yourself), since you have to type out the list of fields in your database table half a dozen times or more at least. This is a massive pain if you need to add a single column to your database table.

Why we use stored procedure in PHP?

Within a Stored Procedure you can write procedural code that controls the flow of execution. That includes if or else constructs, and error-handling code. A Stored Procedure helps improve performance when performing repetitive tasks because they are compiled the first time they are executed.

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.


2 Answers

Here's a benchmark with stored procedure vs php.

http://mtocker.livejournal.com/45222.html

The stored procedure was slower by 10x.

You might also want to look at this:

http://www.tonymarston.net/php-mysql/stored-procedures-are-evil.html

like image 184
Unknown Avatar answered Sep 19 '22 12:09

Unknown


If the reason you are thinking about this is due to performance and scalability, then I would recommend continuing the calculation in PHP.

The reason for this is that regardless whether there is a performance penalty in your PHP, when you are scaling your web application it is generally much easier to move to multiple web servers than multiple database servers. It is therefore preferable to do more calculation in PHP and less in MySQL.

Other than the performance aspect, I still generally prefer avoiding stored procedures in favour of having the logic in the application because

  • It can be less portable. Stored procedure add to the effort required to deploy a new instance of your application.
  • They are written in a different language than PHP, so a PHP developer may not find them easy to understand.
  • It can be difficult to have them kept in source control.

These problems can of course all be solved, without too much difficulty, but it all adds to the complexity overhead.

like image 37
thomasrutter Avatar answered Sep 20 '22 12:09

thomasrutter