MySQL: ¿Usar un disparador DB o simplemente usar PHP?

I'm creating a web app for sellers that sell on Amazon's marketplace. This app downloads their orders using Amazon's API then calculates the fees that they're paying to Amazon. This is the main function of the app. It is used to generate easy to understand reports based on this data. Each order has 1 or more order items inside of it and each order item has specific attributes such as weight, dimensions, etc.

There are a slew of different fees Amazon charges it's sellers such as a fee per order, fees per order item, fees for order item fulfillment if using the Fulfillment by Amazon program, weight fees based on how heavy the order items are, etc. In total, I count around 30 different variables in regards to the different scenarios in which a seller would pay some kind of fee.

For the DB, I currently have 6 tables that relate to my question: the amazon_order_items table which contains every order and every order item in that order along with all of the necessary attributes to calculate the fees. I also have 4 other tables specifically created for just storing fees. I created separate tables for all these fees due to the different types they are.

Currently, I have an INSERT trigger setup that fires whenever a new order item row is inserted into the amazon_order_items table. It then pulls from these 4 other fee tables and inserts a matching row into the amazon_order_items_fees table. For awhile, it worked fine albeit an extremely ugly trigger. I chose a trigger because I thought it would be very beneficial to have the fee inserted into the DB automatically at the time the order item was inserted. This is turning into a disaster.

Amazon constantly changes their fee structure and keeping accurate fee numbers is proving to be extremely difficult with the triggers I've used. My question is, should I continue down the path of using DB triggers to calculate fees and insert them into a DB table or should I scrap the design all together and simply use PHP to insert the order item and the corresponding fees?

preguntado el 09 de septiembre de 13 a las 23:09

1 Respuestas

Triggers are better suited to situations where your application isn't necessarily aware of changes/events in the database. It's always going to be more transparent what's going on at application level if you keep that logic to the actual application.

Generally speaking, triggers are harder to maintain, and harder to debug if something isn't behaving as it should. For those reasons alone I'd recommend making the inserts to the amazon_order_items_fees table part of the app, especially since you've already mentioned that they frequently change their fee structure.

Hope that helps you decide!

Respondido el 10 de Septiembre de 13 a las 00:09

Thanks for the help! I do the inserts into the amazon_order_items via part of the app. It's the fees that are calculated from those inserts is what's in question. I simply don't know the best design here. Should I calculate the fees via trigger when it's inserted or should I put that logic into the app and insert the fees via the app? - Adán Bertram

If all the inserts to the db happen from within the app (nothing external or another app), it's probably a good idea to hook it in the app. Your idea of storing the fees in a table in the database is still the best option, although you probably just don't want to use a trigger to apply them. - calcinado

Thank you. This is what I'm thinking as well. - Adán Bertram

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.