If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > General > Database Concepts & Design > calculate or store?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-17-04, 14:02
cameronjones cameronjones is offline
Registered User
 
Join Date: Apr 2004
Posts: 1
Question calculate or store?

hello forum,

i am developing an ordering system with the usual accounts, orders, transactions, products, etc but have been having some trouble with the complexities of some queries and so have now been thinking about changing the design somewhat.

currently, i am calculating the account balance from all the transactions and orders made on it but am wondering if anyone would recommend having an account balance field instead.

this would seem to be a simpler and possibly more flexible approach, but was wondering how other people design this sort of system. what are the pros and cons of both approaches? when is it a good idea to store and update fields manually and when is it good to calculate?

here are some of my tables:

Account
-----------
id (pk)
name
accountStatus
merchant (fk)


AccountGroup
----------------
account (fk)
customer(fk)

Transaction
--------------
id (pk)
account (fk)
amount
customer (fk)
transactionType


Order
------------
id (pk)
account (fk)
discount
merchant (fk)
orderStatus


OrderItem
--------------
id (pk)
order (fk)
product (fk)
unitPrice (stored from product)
quantity


Product
----------
id (pk)
unitPrice
unitCost
inventory
...

so, if i wanted to calculate the account balance based on my current design, that would require several queries (i'm using mysql version 3 so i can't do sub-queries).

thanks in advance,
cam
Reply With Quote
  #2 (permalink)  
Old 04-17-04, 14:47
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
"when is it a good idea to store and update fields manually and when is it good to calculate?"

this is based entirely on performance history

storing data always introduces the possibility of integrity problems

a payment comes in and gets into the transaction table, but the account balance update query dies for some reason

therefore you need transaction locking, rollback, etc.

so you pay a price with increased complexity, and a heckuva lot more code, and in return you get back some cycles when looking up the balance

your call
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On