The danger with using views is filtering a query against a view,
expecting to read a very small portion of a very large table. Any filtering should be done within
the view because any filtering against the view itself is applied after the query in the view has
completed execution. Views are typically useful for speeding up the development process but
in the long run can completely kill database performance.
以下是Postgresql 9.5文档的摘录:
Making liberal use of views is a key aspect of good sql database design. Views allow you to encapsulate
the details of the structure of your tables,which might change as your application evolves,
behind consistent interfaces.
这两个来源似乎相互矛盾(“不设计与观点”与“设计与观点”).
但是,PG视图是使用规则系统实现的.因此,可能(这是我的问题)对视图的任何过滤都被重写为视图中的过滤器,从而导致针对基础表的单个查询执行.
我的解释是否正确,PG将WHERE子句组合进出视图?或者它是一个接一个地单独运行它们?任何简短,自包含,正确(可编译)的例子?
从视图中进行选择与运行基础sql语句完全一样快或慢 – 您可以使用explain analyze轻松检查.
Postgres优化器(以及许多其他现代DBMS的优化器)将能够将视图上的谓词下推到实际的视图语句中 – 只要这是一个简单的语句(同样,这可以使用explain analyze进行验证).
关于性能的“坏名声” – 我认为 – 从您过度使用视图并开始构建使用视图的视图的视图开始.通常,与没有视图的手工定制的语句相比,这会导致语句过多.因为不需要一些中间表.几乎在所有情况下,优化器都不够智能,无法删除那些不需要的表/连接,或者在多个视图级别上推送谓词(对于其他DBMS也是如此).