Two different tables or just one with bool column?

前端 未结 7 2344
自闭症患者
自闭症患者 2021-02-19 15:06

We have two tables: OriginalDocument and ProcessedDocument. In the first one we put an original, not processed document. After it\'s validated and processed (converted to our XM

7条回答
  •  情书的邮戳
    2021-02-19 15:34

    Do try to make distinction between logical and physical modelling.

    Even if the difference between the two entities is only seven properties, they are logically a different thing in those seven items. At the same time they are a same thing in other properties.

    The way to logically represent that is this have one-to-one-or-zero relationship between the two tables, and to have one table store all the common properties (superclass) and in the other (subclass) you would only store the ID from the superclass.

    In terms of performance this is not so bad:

    • when you don't care about what type of document you work with you will query the superclass table (gain)
    • when you know you want only specific properties found in the subclass table you will work only with that table (this might be real gain)
    • you will pay a price only when you need to join the two tables (joins have a price compared to denormalized structures such as storing everything in a single table)
    • you will also pay a price when inserting subclass records, because you will be inserting into two tables (this might be very low and/or justified)

    Depending on the processes you are modelling, the frequency of these queries and other things (such as security for both entities, ownership, difference in integrity rules) you might decide to store this information in one table in the database or in two (either can be much faster in border-line cases and two table solution can also be denormalized a bit; for example you could still store information in a main table about the type of the document to avoid the join if that kind of query is all you care).

    Or maybe your implementation decisions might be driven by your choice of application framework and for that reason you might really prefer working with single table or the other way around (for example automatic creation of data entry forms in frameworks such as django-admin).

    Whatever you do, realize the difference between the logical and physical design. In your logical design normalize everything - it will pay off. In physical implementation make different scenarios and - test, test, test with your own data. Never confuse the order of the two (logical-conceptual and physical-practical modelling).

提交回复
热议问题