mysql: subtract multiple columns inside the query or out in php?
which has the best performance? Thanks!
1) SELECT field01 - (field02 + field03) as estoque FROM table
2) $estoque = $rs['field01'] - ($rs['field02'] + $rs['field03'])
This is a good question and it would depend on a few considerations, For most general use, the times would be negligible. SQL code would be faster when math operations have to be done repeatedly to large datasets, and for processing data and adding it back into the database. Part of this involves the fact that large datasets dont need to be transferred to the application/application memory. MySQL i optimized to process data on the db like this, other db's have procedures, etc. Operations that involve storing the results for further processing, and that need to be really dynamic would be best in PHP code, things such as integrals, fatorials, amortization tables etc. The caveat to SQL code is that its sometimes more complex than just using PHP, and unless you're beyond needing to process tens of thousands of rows, you probably wont see any speed gains