Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

where should I do the calculating stuff,PHP or Mysql?

Tags:

php

mysql

I've been doing a lot of calculating stuff nowadays. Usually I prefer to do these calculations in PHP rather than MySQL though I know PHP is not good at this. I thought MySQL may be worse. But I found some performance problem: some pages were loaded so slowly that 30 seconds' time limit is not enough for them! So I wonder where is the better place to do the calculations, and any principles for that? Suggestions would be appreciated.

like image 575
Young Avatar asked Apr 12 '10 16:04

Young


2 Answers

Anything that can be done using a RDBMS (GROUPING, SUMMING, AVG) where the data can be filtered on the server side, should be done in the RDBMS.

If the calculation would be better suited in PHP then fine, go with that, but otherwise don't try to do in PHP what a RDBMS was made for. YOU WILL LOSE.

like image 99
Adriaan Stander Avatar answered Sep 30 '22 21:09

Adriaan Stander


I would recommend doing any row level calculations using the RDBMS.

Not only are you going to benefit from better performance but it also makes your applications more portable if you need to switch to another scripting language, let's say PHP to Python, because you've already sorted, filtered and processed the data using your RBDMS.

It also helps separate your application logic, it has helped me keep my controllers cleaner and neater when working in an MVC environment.

like image 39
The website-lab Avatar answered Sep 30 '22 19:09

The website-lab