Inventory database design

49,664

Solution 1

I have seen both approaches at my current company and would definitely lean towards the first (calculating totals based on stock transactions).

If you are only storing a total quantity in a field somewhere, you have no idea how you arrived at that number. There is no transactional history and you can end up with problems.

The last system I wrote tracks stock by storing each transaction as a record with a positive or negative quantity. I have found it works very well.

Solution 2

I have Vol 1 and Vol 2 and these have been pretty helpful in the past.

Solution 3

It depends, inventory systems are about far more than just counting items. For example, for accounting purposes, you might need to know accounting value of inventory based on FIFO (First-in-First-out) model. That can't be calculated by simple "totaling inventory received - total of inventory sold" formula. But their model might calculate this easily, because they modify accounting value as they go. I don't want to go into details because this is not programming issue but if they swear by it, maybe you didn't understand fully all their requirements they have to accommodate.

Solution 4

both are valid, depending on the circumstances. The former is best when the following conditions hold:

  • the number of items to sum is relatively small
  • there are few or no exceptional cases to consider (returns, adjustments, et al)
  • the inventory item quantity is not needed very often

on the other hand, if you have a large number of items, several exceptional cases, and frequent access, it will be more efficient to maintain the item quantity

also note that if your system has discrepancies then it has bugs which should be tracked down and eliminated

i have done systems both ways, and both ways can work just fine - as long as you don't ignore the bugs!

Solution 5

It's important to consider the existing system and the cost and risk of changing it. I work with a database that stores inventory kind of like yours does, but it includes audit cycles and stores adjustments just like receipts. It seems to work well, but everyone involved is well trained, and the warehouse staff aren't exactly quick to learn new procedures.

In your case, if you're looking for a little more tracking without changing the whole db structure then I'd suggest adding a tracking table (kind of like from your 'transaction' solution) and then log changes to the inventory level. It shouldn't be too hard to update most changes to the inventory level so that they also leave a transaction record. You could also add a periodic task to backup the inventory level to the transaction table every couple hours or so so that even if you miss a transaction you can discover when the change happened or roll back to a previous state.

If you want to see how a large application does it take a look at SugarCRM, they have and inventory management module though I'm not sure how it stores the data.

Share:
49,664

Related videos on Youtube

nmarmol
Author by

nmarmol

Foxpro Programmer transitioning to .NET

Updated on July 05, 2022

Comments

  • nmarmol
    nmarmol almost 2 years

    This is a question not really about "programming" (is not specific to any language or database), but more of design and architecture. It's also a question of the type "What the best way to do X". I hope does no cause to much "religious" controversy.

    In the past I have developed systems that in one way or another, keep some form of inventory of items (not relevant what items). Some using languages/DB's that do not support transactions. In those cases I opted not to save item quantity on hand in a field in the item record. Instead the quantity on hand is calculated totaling inventory received - total of inventory sold. This has resulted in almost no discrepancies in inventory because of software. The tables are properly indexed and the performance is good. There is a archiving process in case the amount of record start to affect performance.

    Now, few years ago I started working in this company, and I inherited a system that tracks inventory. But the quantity is saved in a field. When an entry is registered, the quantity received is added to the quantity field for the item. When an item is sold, the quantity is subtracted. This has resulted in discrepancies. In my opinion this is not the right approach, but the previous programmers here swear by it.

    I would like to know if there is a consensus on what's the right way is to design such system. Also what resources are available, printed or online, to seek guidance on this.

    Thanks

    • MusiGenesis
      MusiGenesis over 15 years
      When you say "the previous programmers here swear by it", do you mean they swear every time they have to work on it?
  • Steven A. Lowe
    Steven A. Lowe over 15 years
    the drawback to this particular solution is that performance will get worse and worse over time, as you have to keep a record of all inventory ever received or sold, indefinitely, in order to get the correct current quantity!
  • INS
    INS over 13 years
    +1 I had the same dilemma and I think now that this is the best choice
  • aruno
    aruno almost 13 years
    hmm. returns isnt really exceptional is it? unless you're selling purely perishable items or something else that can't be re-sold
  • Steven A. Lowe
    Steven A. Lowe almost 13 years
    @Simon: one of the early inventory systems I wrote was for a custom ice cream shop. Returns were not only exceptional, they were practically impossible ;-)
  • aruno
    aruno almost 13 years
    @Neil - can you comment on performance of this approach. it seems to be the favored approach, but if you have hundreds of products and thousands of transactions how or when are you calculating the cumulative totals. or are you just storing the cumulative total in another place with the peace of mind in knowing you can recalculate if needed?
  • Neil Aitken
    Neil Aitken almost 13 years
    @Simon It stands to reason that it would be considerably less performant than just querying one field. I must say I never profiled it with a large amount of products. It would be best trying it and seeing what kind of performance hit you get on your data set. Then deciding if it's worth caching the quantity somewhere.
  • aruno
    aruno almost 13 years
    I ran a test - I posted about 3 million records (of positive and negative inventory adjustments) over 2000 products. It took only a fraction of a second to count the total sum of all the rows, grouped by SKU. I have to say I was absolutely astounded how quickly it worked, and for the project I'm doing I definitely have orders of magnitude worth of growth left and won't need to worry about it anytime soon. Obviously if you're displaying realtime inventory totals on a website you'd probably want to cache them, but even across all 2000 products I can almost instantly calculate the total sums.
  • Neil Aitken
    Neil Aitken almost 13 years
    @Simon_Weaver that's great to hear. Glad to see this method has decent performance in your situation, as it is definitely my prefered method of handling inventory.
  • ClearCrescendo
    ClearCrescendo almost 10 years
    Banking / accounting systems do something similar with transactions (Debit or Credit) often as separate fields but with the same summation effect. Primarily for operational reasons a monthly balance is created (There is usually a verification month end process) but this is also a method of being able to add up 12 monthly delta numbers in order to quickly add up a year instead of every transaction in a year. When done across years and millions of accounts the performance benefit is real and also allows for a correction several months ago to only require the recalculation of that month's close.
  • Grey Wolf
    Grey Wolf over 8 years
    You should apply both, 1 field is quantity, and 1 table for transaction. The quantity field can recalculate when need. You need this field because performance. When end of month. we should move all number form pre month to new month. This is my ERP system working. and working well
  • buncis
    buncis over 2 years