I am creating a web app which has some complex underlying associations. In order to solve several issues I was having I created a UNION View. There are probably a lot of other ways this could be solved.
But I am now considering the efficiency of my design, and I wanted to know if a VIEW is newly created each time it is queried, or is it only created once, and kept updated.
To elaborate, if I have table_a (100 records) and table_b (100 records) and make a UNION View, then I have created a view with 200 records.
Does this whole process occur each time I do a select against the View?
Again, obviously each time I update the underlying table records the view is updated, but does the view update this one record or does it recreate the whole view from scratch?
Dale
A view is nothing more than a query with a name. There are possible perf-related optimizations, that some DBMS realize better than others (pgSQL seems to be on the better side), like reusing the query plan, cached access control etc.
However, at the end of they day, almost always, you can expect a view to behave like issuing the SQL directly. With the difference that you can grant access to this query w/o granting access to the underlying tables.
There are optimizations that you could do which change the behavior (make them half table-like) and that might or might not exist in pgSQL like materialized views (sorry no idea about pgSQL), but this is just nitpicking.