Jump to content

Which is better?


ShadowMage
 Share

Recommended Posts

Hey I need some advice. I have a complex SQL statement (well actually I didn't write it :) ) which pulls a bunch of information from a database and performs several calculations. Here's a snippet:

....SUM(JobOper.EstProdHours + JobOper.EstSetHours) AS 'EstHours', SUM(JobOper.ActProdHours + JobOper.ActSetupHours) AS 'ActHours', case (SUM(JobOper.EstProdHours + JobOper.EstSetHours)) when 0 then 0 else (((SUM(JobOper.ActProdHours + JobOper.ActSetupHours)) / SUM((JobOper.EstProdHours + JobOper.EstSetHours))) * 100) end as 'Percentage' FROM.....

My question:Is it better to perform these kinds of calculations with the query or in PHP? I'm mainly concerned about performance and speed.

Link to comment
Share on other sites

That question doesn't really have a single answer, there are several factors. You may want to benchmark each method though and see how they perform, you can use PHP to keep track of how long things take. The microtime function will let you do that.

Link to comment
Share on other sites

Depends... do you need the data in its original form AND computed form? If yes, then it's probably better to do the computations on the PHP end than to make two queries - one with and one without computations.If you need only the computed form, it's better to do the calculations at whatever the more powerful machine is. If the MySQL server and PHP are on the same computer, there won't be a substantial difference, but I'd guess that using MySQL will be faster since it has direct access to all of the data.As justsomeguy said, benchmark is the best way to make sure of any of those.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...