I have a table that tracks inventory data by each individual piece. This is a simplified version of the table (some non-key fields are excluded):
UniqueID,
ProductSKU,
SerialNumber,
OnHandStatus,
Cost,
DateTimeStamp
Every time something happens to a given piece, a new audit record is created. For example, the first time my product ABC gets added to inventory I get a record like this:
1, ABC, 555, OnHand, $500, 01/01/2009 @ 02:05:22
If the cost of ABC serial number 555 changes, I get a new record:
2, ABC, 555, OnHand, $600, 01/02/2009 @ 04:25:11
If the piece is sold, I get yet another record:
3, ABC, 555, Sold, $600, 02/01/2009 @ 5:55:55
If a new piece of ABC is brought in, I get this record:
4, ABC, 888, OnHand, $600, 02/05/2009 @ 9:01:01
I need to be able to get on-hand inventory value for a given set of products at any point in time as fast as possible.
Using my example above, if I wanted to get my inventory value for product ABC as of 01/02/2009, I'd need to select, for each unique Product/SerialNumber combination, the single most recent record prior to 01/03/2009 with a status of "OnHand" and then add up the costs. (I'm not 100% sure what this select statement would look like at this point, but I'm going to experiment a bit).
My questions: is this a good structure for the type of audit table I'm describing? That is, does it lend itself to fast queries if indexed appropriately? (I'm trying to imagine what will happen when this table grows to millions of rows.)
Should I break out historical records into a separate table and only leave the most recent record for each ProductID/SerialNumber combo in the "active" table?
Any feedback/suggestions/comments/links are appreciated.
Thanks!