Database normalization design - single or multiple tables

后端 未结 7 1770
臣服心动
臣服心动 2021-01-14 15:28

Should this be represented in the database as 1 table or 3 tables? I and my friend have different opinions about this so I\'d like to see the general views on this. (Maybe

7条回答
  •  猫巷女王i
    2021-01-14 16:06

    As the volume increases selection from two tables may be a lot faster than one. Sometimes this kind of refactoring (partition) is done on mature databases to increase performance.

    Imagine using this for a multi table join, where some criteria are on this table, but others are in different tables.

    select from order join customer using (customer_id)
    where
        order.order_date between ? and ?
        and customer.name = ?
    

    It may end up fetching all order rows for the dates from disk, then throwing many of them away because they don't match the join. This fetch from disk is bound to be slow and may to spoil your RAM cache.

    select from order join order_detail using (order_id) join customer using (customer_id)
    where
        order.order_date between ? and ?
        and customer.name = ?
    

    In this case when it loads all order rows from disk it's not going to hurt as bad as previously, because the table is narrower and smaller. It doesn't need to load all the lengthy fields which are irrelevant for filtering. Eventually, after join to customer, it will only fetch those order_detail rows which match all criteria.

    If you expect this to be large, you should consider splitting the table so that the fields which are most critical for searches are in one table, and "data" fields in other one-to-one table(s).

    The bottom line is: Normal form and domain is one thing, but performance often requires tradeoffs. You can hide some of them (cover the split with a view), but not all (duplicate/aggregate fields for the sake of faster selects).

提交回复
热议问题