Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Business Logic in PHP or MySQL?

On a site with a reasonable amount of traffic , would it matter if the application/business logic is written as stored procedures ,triggers and views , instead of inside the PHP code itself?

What would be the best way to go keeping scalability in mind.

like image 264
Sabeen Malik Avatar asked Nov 09 '09 21:11

Sabeen Malik


4 Answers

I can't provide you statistics, but unless you plan to change PHP for another language in the future, i can say keeping the business logic in PHP is more "scalability friendly".

Its always easier and cheaper to solve web server load problems than having them in the database. Your database will always need to be lighting quick and just throwing mirrors at it won't solve the problem. The more database slaves you have, the more writes you have to do.

like image 64
pablasso Avatar answered Oct 19 '22 01:10

pablasso


In my experience, you should put business logic in PHP code rather than move it onto the database. Assuming your database is on a separate server, you don't want your database to be busy calculating formulas when requests come in.

Keep your database lightning fast to handle selects, inserts and updates.

like image 31
jeph perro Avatar answered Oct 19 '22 01:10

jeph perro


I think you will have far better scalibility keeping database code in the database where it can be performance tuned as the number of records gets larger. You will also have better data integrity which is critical to the data even being useful. You don't see a lot of terrabyte sized relational dbs with all their code in the application.

Read some books on database performance tuning and then decide if you want to risk your company's data on application code.

like image 2
HLGEM Avatar answered Oct 18 '22 23:10

HLGEM


There are several things to consider when trying to decide whether to place the business logic in the database or in the application code.

Will the same database be accessed from different websites / web applications? Will the sites / applications be written in the same language or in a different language?

If the database will be used from a single site, and the site is written in a single language then this becomes a non-issue. Otherwise, you'll need to consider the added complexity of stored procedures, triggers, etc vs trying to maintain database access logic etc in multiple code bases.

What are relational databases in general good for and what is MySQL good for specifically? What is PHP best at?

This consideration is fairly straight-forward. Relational databases across the board and specifically in any variant of SQL are going to do a great job at inserting, updating, and deleting data. Generally they also handle ATOMIC transactions well. However, most variants of SQL (including MySQL) are not good at complex calculations, on-the-fly date handling, file system access etc.

PHP on the other hand is very fast at handling calculations, dates, file system accesses. By taking a little time you can even design your PHP code to work in such a way that records are only retrieved once and then stored when necessary.

What are you most familiar / comfortable with using?

Obviously it tends to make more sense to use the tool with which you are most familiar.

As a last point consider that just because a drill can be used to cut sheet rock or because a hammer can be used to drive a screw doesn't mean that they should be used for these things. Sometimes I think that programmers do more potential damage by trying to make more powerful tools that do everything rather than making simpler tools that do one thing really, really well.

like image 2
Noah Goodrich Avatar answered Oct 19 '22 00:10

Noah Goodrich