When to use a View instead of a Table?

前端 未结 8 1260
没有蜡笔的小新
没有蜡笔的小新 2020-12-04 07:02

When should a View actually be used over an actual Table? What gains should I expect this to produce?

Overall, what are the advantages of using a view over a table?

8条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-04 07:11

    Views can:

    • Simplify a complex table structure
    • Simplify your security model by allowing you to filter sensitive data and assign permissions in a simpler fashion
    • Allow you to change the logic and behavior without changing the output structure (the output remains the same but the underlying SELECT could change significantly)
    • Increase performance (Sql Server Indexed Views)
    • Offer specific query optimization with the view that might be difficult to glean otherwise

    And you should not design tables to match views. Your base model should concern itself with efficient storage and retrieval of the data. Views are partly a tool that mitigates the complexities that arise from an efficient, normalized model by allowing you to abstract that complexity.

    Also, asking "what are the advantages of using a view over a table? " is not a great comparison. You can't go without tables, but you can do without views. They each exist for a very different reason. Tables are the concrete model and Views are an abstracted, well, View.

提交回复
热议问题