Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I need heavy arithmetic and string functions for processing the data captured from mysql database. Which is preferred: mysql functions or programming language (e.g., PHP)?

$result = $mysqli->query("SELECT A, B, C FROM table1");
while($row = $result->fetch_assoc() )
{
$a = str_replace("X", "Y", $row['A']);
$b = ($row['B']/$row['C')*1000;
}

OR

$result = $mysqli->query("SELECT REPLACE(A, 'X', 'Y'), ((B/C)*1000) AS B FROM table1");
while($row = $result->fetch_assoc() )
{
$a = $row['A'];
$b = $row['B'];
}

This just a simple example with no significant difference, but when having heavy calculations, which one is preferred?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

If you're going to bring the same amount of rows from the database anyway, then you had better perform your mathematical functions in your application, not in your database. Math calculations in SQL are notoriously slow.

Also note that this takes computation from a single database server into possibly multiple PHP front ends. Sure, you might also spread the load over multiple MySQL replication servers; generally speaking, you will have more PHP front ends than MySQL back ends.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.