r/DatabaseHelp • u/_Landmine_ • Jan 20 '16
[Database Design] Tracking Ownership History of an Item
I'm wondering what is the recommended design and why for tracking an item and its parent in an MS SQL database.
To keep it simple, lets say I have 4 people in a table and 2 balls in another table. I'd like to track who has the ball now and also be able to view the entire history of the ball and see who owned it and when.
Currently I have it setup to allow the ball to have an owner but I'm not sure what is going to be the best way to track its ownership history.
I'm thinking a new table ball_ownership
ID, EventDateTime, OwnerID, BallID
Then I can query that table to see who owned the ball at what time and what balls each owner has owned at any time.
Thoughts or recommended best practices?
2
u/[deleted] Jan 20 '16
I think you have it right. You'd need a table that logs each change in ownership.
2
u/muchargh Jan 21 '16
You're on the right track, but ditch the ID column. Your primary key is (BallID, EventDatetime). You will never be able to find the record you need using the ID column - queries tend to be along the lines of "Who owned ball 2 in January?" not "Who was the owner for ID 1241?"
This SQL Fiddle illustrates the right and wrong way to set up the table, and why you should avoid using an ID column. Pay special attention to the execution plan of both queries - using the ID requires a table sort which is very expensive and more so when your table is too large to fit into memory.
2
u/Pritz Jan 21 '16
Look up Slowly Changing Dimensions.
You need time fields like From/To in your table design.
And like others have pointed out, leave out the ID.