简体   繁体   English

PostgreSQL 9.6查看性能和副作用

[英]PostgreSQL 9.6 View Performance & Side Effects

Our project makes use of different schemas to keep things organized and secure. 我们的项目使用不同的架构来保持事物的组织性和安全性。 We GRANT access to entire schemas rather then selecting individual components which may result in an inadvertent leak of data. 我们GRANT访问整个架构而不是选择这可能导致数据的无意泄漏单个组件。 Additionally, it would make our lives easier if we could write our queries from a single schema as it would help with maintainability and error reduction. 此外,如果我们可以从单个模式编写查询,这将使我们的生活更加轻松,因为这将有助于维护和减少错误。

For example, schema_foo and schema_bar contain raw normalized data (not accessible to the end user) and schema_baz contains functions (accessible to the end user) that return data from schema_foo and schema_bar . 例如, schema_fooschema_bar包含原始规范化数据(最终用户无法访问),而schema_baz包含从schema_fooschema_bar返回数据的函数(最终用户可以访问)。 This makes sense when data in schema_foo and schema_bar need processing prior to delivery to the end user; schema_fooschema_bar数据需要在交付给最终用户之前进行处理时, schema_foo schema_bar however, in some cases no additional processing is required. 但是,在某些情况下,不需要其他处理。

In this case, would it make sense to make a view in schema_baz simply calling the table in schema_foo or schema_bar and if so, what is the performance difference / side effects between 在这种情况下,在schema_baz仅通过调用schema_fooschema_bar的表来创建视图是否schema_bar ,如果这样,两者之间的性能差异/副作用是什么?

SELECT * FROM schema_foo.table_bin;

and

CREATE VIEW schema_baz.view_bin AS SELECT * FROM schema_foo.table_bin;
SELECT * schema_baz.view_bin;

If this is acceptable, is it better (for any reason) to identify each column by name rather then * ? 如果可以接受,那么通过名称而不是*标识每个列是否更好(出于任何原因)?

Note: The resulting view would also be used in other queries. 注意:结果视图也将在其他查询中使用。 I know the query planner is great; 我知道查询计划器很棒; however, I'm concerned there could be unintended consequences of (ab)using views for the sole purpose of making a table accessible from another schema. 但是,我担心(滥用)仅出于使表可从另一模式访问的目的而使用视图可能会产生意想不到的后果。

My experience leans towards "If in doubt, be explicit". 我的经验倾向于“如有疑问,请明确表达”。 Using select * in a production system is usually just asking for trouble down the road. 在生产系统中使用select *通常只是在寻求麻烦。

Also, adding columns to the base table(s) in Postgres won't change the columns in the view-- they won't magically appear. 同样,在Postgres中将列添加到基表中也不会更改视图中的列-它们不会神奇地出现。 As an example: 举个例子:

(postgres@lh:5432) # create table foo (col1 integer, col2 text);
CREATE TABLE

(postgres@lh:5432) # create view bar as select * from foo;
CREATE VIEW

(postgres@lh:5432) # alter table foo add col3 date;
ALTER TABLE

(postgres@lh:5432) # insert into foo values (1, 'One', current_date);
INSERT 0 1

(postgres@lh:5432) [~] # select * from foo;
 col1 | col2 |    col3    
------+------+------------
    1 | One  | 2017-08-17
(1 row)

(postgres@lh:5432) # select * from bar;
 col1 | col2 
------+------
    1 | One
(1 row)

This was with version 9.5 but I would be surprised if other versions worked any differently. 这是9.5版,但如果其他版本的工作方式有所不同,我会感到惊讶。

Yes, tables can and do change, often. 是的,表可以而且确实经常改变。 If you simply write *, then what if a column gets added or deleted in the base table? 如果您只写*,那么如果在基表中添加或删除列怎么办? Your view changes without your knowledge. 您的视图在您不知情的情况下发生了变化。

It is best practise to always list out column names when writing views of any kind. 最佳做法是在编写任何类型的视图时始终列出列名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM