Database Structure Advice Needed

前端 未结 8 706
别跟我提以往
别跟我提以往 2021-01-29 23:15

Im currently working on a site which will contain a products catalog. I am a little new to database design so I\'m looking for advice on how best to do this. I am familiar wit

8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-30 00:01

    My suggestions

    • put a many-to-many relation between Item and Category so that a product can be displayed in many hierarchy node (used in ebay, sourceforge ...)
    • keep the category hierarchy

    Performance on the category hierarchy

    If your category hierarchy is depth, then you could generate an "Ancestors" table. This table will be generated by a batch work and will contains :

    • ChildId (the id of a category)
    • AncestorId (the id of its parent, grand parent ... all ancestors category)

    It means that if you have 3 categories : 1-Propeller > 2-aircraft > 3-wood

    Then the Ancestor table will contain :

    ChildId  AncestorId
    1        2
    1        3
    2        3
    

    This means that to have all the children of category1, you just need 1 query and you don't have do nested query. By the way this would work not matter what is the depth of you category hierarchy.

    Thanks to this table, you will need only 1 join to query against a category (with its childrens).

    If you need help on how to create the Ancestor table, just let me know.

提交回复
热议问题