Which database design gives better performance?

前端 未结 4 363
你的背包
你的背包 2021-01-17 06:18

I want to select to retrieve person and also further make some inserts, deletes and updates.

If I want retrieve person who liv

4条回答
  •  一向
    一向 (楼主)
    2021-01-17 06:55

    This question is very similar to the one you made yestarday:

    Create many tables or just one

    The answer is also similar - that depends on what you want to achieve. Both solutions could work and both have pros and cons and one should do a little trade-off analysis in the light of the specific situation. Out of this context is not possible to answer your question.

    The only difference I see in both version is foreign key id_country in Person table:

    Person(id, name, profession, ****id_country****, id_city)
    cities (id, city, id_country)
    countries (id, country)

    The question is "do we need it?"

    So, the pros and cons of both solutions:

    1. Solution: With id_contry:

    • pros: easier retrival of a Person based on land (simpler query) and better performance of this query
    • cons: more complex underlaying DB and more redundancy, more chance of inreoducing inconsiscenties in the DB, harder updates

    2. Solution: Without id_country:

    • pros: simpler and cleaner model, no redundancy, easier maintenance
    • cons: slower performance and more complex query for retrival of a Person based on land (simpler query)

    So, the 1st solution effectivelly gives you easier query structure and better performance for retrieving Persons by Country (what you wanted), but it has its cost (see pros and cons). On the other side, pragmatic thinking says that country-city data are quite stabile and not often changed and this fact goes in favor of the 1st solution.

    If this denormalization and slight chance of inconsistencies something you can live with, you can take the 1st solution.

提交回复
热议问题