What are views good for?

后端 未结 14 1232
你的背包
你的背包 2020-11-28 03:31

I\'m just trying to get a general idea of what views are used for in RDBMSes. That is to say, I know what a view is and how to make one. I also know what I\'ve used them f

相关标签:
14条回答
  • 2020-11-28 03:57

    In addition to what the others have stated, views can also be useful for removing more complecated SQL queries from the application.

    As an example, instead of in an application doing:

    sql = "select a, b from table1 union select a, b from table2";

    You could abstract that to a view:

    create view union_table1_table2_v as
    select a,b from table1
    union
    select a,b from table2

    and in the app code, simply have:

    sql = "select a, b from union_table1_table2_v";

    Also if the data structures ever change, you won't have to change the app code, recompile, and redeploy. you would just change the view in the db.

    0 讨论(0)
  • 2020-11-28 04:00

    Anytime you need [my_interface] != [user_interface].

    Example:

    TABLE A:

    • id
    • info

    VIEW for TABLE A:

    • Customer Information

    this is a way you might hide the id from the customer and rename the info to a more verbose name both at once.

    The view will use underlying index for primary key id, so you won't see a performance loss, just better abstraction of the select query.

    0 讨论(0)
  • 2020-11-28 04:01

    I remember a very long SELECT which involved several UNIONs. Each UNION included a join to a price table which was created on the fly by a SELECT that was itself fairly long and hard to understand. I think it would have been a good idea to have a view that to create the price table. It would have shortened the overall SELECT by about half.

    I don't know if the DB would evaluate the view once, or once each time in was invoked. Anyone know? If the former, using a view would improved performance.

    0 讨论(0)
  • 2020-11-28 04:03

    I wanted to highlight the use of views for reporting. Often, there is a conflict between normalizing the database tables to speed up performance, especially for editing and inserting data (OLTP uses), and denormalizing to reduce the number of table joins for queries for reporting and analysis (OLAP uses). Of necessity, OLTP usually wins, because data entry must have optimal performance. Creating views, then, for optimal reporting performance, can help to satisfy both classes of users (data entry and report viewers).

    0 讨论(0)
  • 2020-11-28 04:08

    In a way, a view is like an interface. You can change the underlying table structure all you want, but the view gives a way for the code to not have to change.

    Views are a nice way of providing something simple to report writers. If your business users want to access the data from something like Crystal Reports, you can give them some views in their account that simplify the data -- maybe even denormalize it for them.

    0 讨论(0)
  • 2020-11-28 04:09

    A view is simply a stored, named SELECT statement. Think of views like library functions.

    0 讨论(0)
提交回复
热议问题