View (database) - Equivalence

Equivalence

A view is equivalent to its source query. When queries are run against views, the query is modified. For example, if there exists a view named accounts_view with the content as follows:

accounts_view: ------------- SELECT name, money_received, money_sent, (money_received - money_sent) AS balance, address, ... FROM table_customers c JOIN accounts_table a ON a.customer_id = c.customer_id

then the application could run a simple query such as:

Simple query ------------ SELECT name, balance FROM accounts_view

The RDBMS then takes the simple query, replaces the equivalent view, then sends the following to the query optimizer:

Preprocessed query: ------------------ SELECT name, balance FROM (SELECT name, money_received, money_sent, (money_received - money_sent) AS balance, address, ... FROM table_customers c JOIN accounts_table a ON a.customer_id = c.customer_id )

From this point on the optimizer takes the query, removes unnecessary complexity (for example: it is not necessary to read the address, since the parent invocation does not make use of it) and then sends the query to the SQL engine for processing.

Read more about this topic:  View (database)