Question

I read this somewhere:

When you’re new to MySQL, it's tempting to solve problems in the language you know. That can lead to unnecessary and slower code. For example, rather than using MySQL’s native AVG() function, you use a PHP loop to calculate an average by summing all values in a record-set.

Watch out also for SQL queries within PHP loops. Normally, it’s more effective to run a query then loop through the results.

In general, utilize the strengths of your database when analyzing data. A little SQL knowledge goes a long way.

My verdict: I don't think it makes any sense. If a table got millions of records then instead of fetching millions of records to get an avg. I get its better to fetch an avg.

Note: if the application and database server are different them it makes even more sense as transfer time will be high.

Just want to know of MySQL expert verdicts on this.

Was it helpful?

Solution

As my perspective When table having lacs of records at that time the AVG() calculation will be done on database side only and if you fetch those data into PHP and calculate then it will take too much process.And it makes performance down. So its good to calculate average inside database only.

And one more thing you told that Utilizing the strength of database when analyzing data, it perfectly true. Otherwise what is the use of database functions and all.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top