What is the best approach to retrieve records from one table that stores it's history?

[亡魂溺海] 提交于 2019-12-06 09:39:16

In general case, you would include an integer version field in the child table.1

But in the specific case of SQL Server, including the explicit foreign key in the parent table would allow you to create an indexed view and effectively cache JOIN between the parent and most recent child. Consider carefully whether you actually need that, and if you do, be very careful not to let this FK go out of sync!2


1 Date/time can be problematic if your database server's clock ever goes out of sync, but you can include it (just) for informative purposes in addition to the integer version.

2 You'd have to lock the parent before inserting new child, to avoid race conditions in concurrent environment and you'd have to make sure all clients follow that. Fortunately, the mere attempt to UPDATE the parent row (to set the new FK value) also locks it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!