Database optimization orders

前端 未结 4 527
心在旅途
心在旅途 2020-11-29 14:01

In a database where users can place orders, is it better to have a new table with addresses or each order has the address data in its header.

4条回答
  •  有刺的猬
    2020-11-29 14:47

    i can't think of any reason for an address to be in the same table as an order except that it saves you a small amount of work now.

    arguments for having a separate table include:

    • being able to associate multiple delivery addresses with a user without having to search through all orders (so it's easy to offer users a drop-down list of addresses they have previously used).

    • you can use the same table for billing and delivery addresses, avoiding duplication

    • you can extend/change how addresses are stored in future (eg by adding a country field when you go international) without having to update every order.

    none of this is much related to optimization. not sure why that is in the title?

    [Branko has a good point about preserving order data. however, you don't need to make the database completely versioned. you can simply have an "expired" flag on things that are referenced from the orders (like users and addresses) but which no longer have current values. in other words you only need two "versions" - current and historical. as long as you make references in the order table explicit (so you don't go to the delivery address via the user, but instead link directly to the address table from the order table, that can be made to work. fully versioning a database, including relationships, is a lot of work.]

提交回复
热议问题