How to archive records with cyclic relationships

  • In my application a single "Shipment" can include "OrderItems" from multiple "Orders"

    This creates a cyclic relationship in the database:

    OrderItem belongs to Shipment which includes other OrderItems which belong to other Orders which have OrderItems in other Shipments

    This creates the problem that I can not archive an Order without archiving its related Shipments

    but I can't archive a Shipment without archiving its related Orders ...

    ... and due to the Orders and Shipments that exist in my business nothing can be archived!

    If I archive Shipments first and only archive Orders when all directly related Shipments have already been archived

    this means the application no longer reflects reality (Orders which have been shipped have no shipping information)

    I could add an "Hidden" flag to "Orders" and alter the SQL to hide soon-to-be-archived records from the application completely

    But I am now curious how others would approach this ...

  • I personally only tend to archive data that's over 2 years old, so at that point the shipments involved and all orders SHOULD be filled. I'd put in some logic to make sure I got flagged if something went horribly awry in that plan though, and just make sure all involved items were both complete and over 2 years old, otherwise they'd wait for everything involved to age correctly.


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • 2 days 2 years 2 millenia won't make any difference as they are all linked in a chain!

  • DataDog (10/22/2012)


    2 days 2 years 2 millenia won't make any difference as they are all linked in a chain!

    Eventually everything in a shipment/order would have to age past the 2 year mark, at which point I'd archive. Even though yes, they chain, at some point everything in some order and some shipment would have to get past 2 years.

    If I needed another alternative, what I'd most likely end up doing is talking with business and finding out when they're unlikely to ever need shipment information again. Once an order archived, I'd copy the shipment header to the archive if it wasn't there and then move that order's pieces of the shipment over, removing them from the primary DB. Eventually a shipment would be empty in the primary and I'd remove the header, finally fully archiving it.

    If neither of those work, then I'm missing something important about your data-design.


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • Evil Kraig F (10/22/2012)

    Eventually everything in a shipment/order would have to age past the 2 year mark, at which point I'd archive. Even though yes, they chain, at some point everything in some order and some shipment would have to get past 2 years.

    Yes but they would always be linked to something less than 2 years old ...

    Evil Kraig F (10/22/2012)

    If I needed another alternative, what I'd most likely end up doing is talking with business and finding out when they're unlikely to ever need shipment information again. Once an order archived, I'd copy the shipment header to the archive if it wasn't there and then move that order's pieces of the shipment over, removing them from the primary DB. Eventually a shipment would be empty in the primary and I'd remove the header, finally fully archiving it.

    Yes, that is what I will do

    Unfortunately this impacts on the business rules so a careful redesign is required to ensure the application never sees a partially archived Order ...

    Moral of the story: always design and test your archiving procedures before application goes live ... not all db designs allow for simple "follow the references" archiving

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply