The Use of Views Has No Impact on Query Performance in PostgreSQL

More evidence that PostgreSQL is the best database. The documentation supports one of my strongest beliefs about database design:
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.
Views can be used in almost any place a real table can be used. Building views upon other views is not uncommon.
In answer to a question about the impact of views on query performance, veteran PostgreSQL core developer Bruce Momjian gave the following answer: They have no impact whatsoever.
The parser thinks the view is a real table. The query structure gets passed to the re-writer and the re-writer wipes out the view and substitutes the underlying tables. And that re-write is what gets passed to the query optimizer. The optimizer doesn't see the view; it's optimizing the whole thing.
Some other systems have a problem optimizing views. We don't because by the time the views gets through the re-writer it's been wiped away and you get a fresh optimization of the underlying tables, and the view has disappeared because now that view is not a black box. We can join and use whatever indexes we want because we see the underlying data by the time we get to the optimizer, and that's why we have to do the re-writing before we get to the optimizer.
I worked at a job not long ago where I arrived in my new position, started creating views, and then the CTO saw what I was I was doing and disallowed the use of views.Things quickly went downhill from there.

Source of Bruce Momjian's quote:
"Tutorial Presented by Bruce Momjian of EnterpriseDB at PGConf Silicon Valley 2015"
https://www.youtube.com/watch?v=JFh22atXTRQ
Presentation URL: http://momjian.us/main/writings/pgsql/internalpics.pdf
Quoted from time starting at 1:00:57
                                   

Comments

Popular posts from this blog

Date Dimension Table Definition

Stop Flickering Pointer in Vista's Windows Explorer

In Appreciation of Procedural Programming