When to use a View instead of a Table?

前端 未结 8 1249
没有蜡笔的小新
没有蜡笔的小新 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:15

    First of all as the name suggests a view is immutable. thats because a view is nothing other than a virtual table created from a stored query in the DB. Because of this you have some characteristics of views:

    • you can show only a subset of the data
    • you can join multiple tables into a single view
    • you can aggregate data in a view (select count)
    • view dont actually hold data, they dont need any tablespace since they are virtual aggregations of underlying tables

    so there are a gazillion of use cases for which views are better fitted than tables, just think about only displaying active users on a website. a view would be better because you operate only on a subset of the data which actually is in your DB (active and inactive users)

    check out this article

    hope this helped..

提交回复
热议问题