0

Scenario

I am working on a web application that handles financial transactions, and my code will need to be reusable for everything from consumer carts to manufacturer/distributor shipments. The project uses Laravel in a fairly typical LAMP environment.

I can use PHP's BCMath functions or MySQL queries to handle math operations, but the Eloquent ORM tends to create a lot of queries and db transactions, so I am unsure which will be better. I will need to do addition, subtraction, multiplication, and division with precision up to a the equivalent of a MySQL DECIMAL(10,4).

Question 1: performance

Which will have better performance for precision maths: MySQL queries or BCMath? The answer should include considerations for network overhead. MySQL is running on a Rackspace 8GB standard instance (8GB ram, 4 vCPUs, 600 Mbps, 320GB SSD); the PHP server is running on a Rackspace 2GB standard instance (2GB ram, 2 vCPUs, 240 Mbps, 80GB SSD). Server setup is adaptable, and there is a high likelihood of moving to Azure or AWS before or just after launch. The main reason that the MySQL server has better specs is that we were doing some big data analysis (multiple worker servers, 1 db) last year and still have large storage requirements.

Question 2: philosophy

If one is better than the other, does it make sense to worry about optimization during such an early stage of development (core functionality is still lacking), or is it better to just use whatever is convenient and optimize later?

Jonathan Voss
  • 234
  • 1
  • 6
  • Related: [Is it always wrong to optimize before profiling?](http://programmers.stackexchange.com/questions/63986/is-it-always-wrong-to-optimize-before-profiling) –  Jun 25 '15 at 19:13
  • Sounds like your actual question is BCMath vs your ORM, *not* MySQL. – GrandmasterB Jun 25 '15 at 19:20
  • MichaelT: sort of, but not quite. This is a matter of "write sloppy first, optimize later" vs "write slightly optimized code now". – Jonathan Voss Jun 25 '15 at 19:28
  • "Write good code always, optimize when necessary" –  Jun 25 '15 at 19:28
  • GrandmasterB: it is both. I can perform raw database queries within the ORM and can also optimize/modify the way it handles transactions, so I am more interested in MySQL vs BCMath. – Jonathan Voss Jun 25 '15 at 19:30
  • MichaelT: yep. So the question of "which is better to write now: BCMath or MySQL" is still relevant. – Jonathan Voss Jun 25 '15 at 19:31
  • I'd use MySQL. I'd be very surprised if returning all the necessary rows to perform your calculations within PHP would be faster than having MySQL handle it. But like virtually every other optimization question posted here, there's only one way to know for sure and that's to try both. – GrandmasterB Jun 25 '15 at 19:36

0 Answers0