I'm trying to understand which of the following is a better option:
- Data calculation using Python from the output of a MySQL query.
- Perform the calculations in the query itself.
For example, the query returns 20 rows with 10 columns. In Python, I compute the difference or division of some of the columns.
Is it a better thing to do this in the query or in Python ?
It is probably a matter of taste but...
... to give you an exact opposite answer as the one by Alma Do Mundo, for (not so) simple calculation made on the
SELECT ...
clause, I generally push toward using the DB "as a calculator".Calculations (in the
SELECT ...
clause) are performed as the last step while executing the query. Only the relevant data are used at this point. All the "big job" has already been done (processingJOIN
, where clauses, aggregates, sort).At this point, the extra load of performing some arithmetic operations on the data is really small. And that will reduce the network traffic between your application and the DB server.
It is probably a matter of taste thought...