views:

220

answers:

6

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.

+1  A: 

A well done PHP application should be enought, but keep in mind that it also requires you to do the less calls to the database you can. Store values you'll need later in PHP, shorten queries, cache, etc.

MySQL optimization is always a must, as it will also decrease the amount of databse calls by PHP, and thus getting a better performance. Therefore, there's no way you can't think of stored procedures, etc, if your aim is to increase performance. But MySQL by itself would't be enought if your PHP code isn't well done (lots of unecessary database calls), that's why I think PHP must be well coded, keeping in mind the hole process while developing it, so that unecessary stuff doesn't get in the way. Cache for instance, in "duet" with proper MySQL, is a great boost on performance.

yoda
can you back that up with some personal experience and/or statistical data which specifically answers the question of scalability?
Sabeen Malik
+2  A: 

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.

jeph perro
+5  A: 

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.

pablasso
+1 .. Yes thats exactly what i was thinking ... i can load balance PHP but how to load balance MySQL.
Sabeen Malik
+1  A: 

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.

HLGEM
After so many years of putting everything in the PHP , i have started to feel that having the logic in the DB makes alot of stuff easier, more central and more transparent. However when i think of scalability , somehow it seems that load balancing PHP is easier and cheaper and i dont think i can say the same about MySQL .. can you shed some light on that please?
Sabeen Malik
+1  A: 

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.

Noah Goodrich
All valid points. But the part i really to understand is if having the facility to apply the biz logic at the DB level was a bad idea , why would we see so many highly paid and much sought after DBAs in the world?
Sabeen Malik
A: 

MySQL sucks at using advanced DB techniques, it's simple and fast. PHP, being a dynamic language, makes processing data very easy. Therefore, it usually makes sense to use PHP.

yk4ever
"MySQL sucks at using advanced DB techniques" ... thats a bold statement , can you provide some statistical data or examples? ... also when u say "usually makes sense to use PHP" ... so can u tell me when it makes sense to NOT use PHP ?
Sabeen Malik
What do you mean with "advanced DB techniques"? Which advanced database techniques MySQL does not provide?
Erkan BALABAN