Creating a view requires a view name and a SQL statement: | |
Once the view is created, you can query it in the same way as any other table. | |
The CREATE statement: | |
| |
When creating a view, you can use the OR REPLACE syntax to replace an existing view. | |
The algorithm attributes have some control over how MySQL executes the query. | |
These attributes are MERGE, TEMPTABLE, and UNDEFINED. | |
A MERGE algorithm attempts to combine the incoming SQL statement with the SELECT statement that defines the view and create one SQL statement to process. | |
MERGE isn't allowed in a non-one-to-one relationship with records in the underlying tables. | |
Non-one-to-one relationship is created by using aggregation functions (SUM(), MIN(), MAX(), and so on) or by using the DISTINCT, GROUP BY, HAVING, and UNION keywords. | |
In instances where the MERGE algorithm isn't allowed, the database switches the ALGORITHM value to UNDEFINED. | |
| |
With a MERGE algorithm, MySQL combines the query with the SELECT statement in the view definition to come up with a single query to execute: | |
| |
The TEMPTABLE algorithm forces a view to load the data from the underlying tables into a temporary table. | |
The incoming statement is executed against the temporary table. | |
Moving the data to a temporary table means the underlying tables can be released from any locks. | |
The TEMPTABLE option means the least amount of lock time for the view's underlying tables. | |
Views that reference only literal values are required to use a temporary table. | |
Views that use temporary tables are not updatable. | |
Setting the view algorithm to UNDEFINED tells the query parser to make the choice between the MERGE and TEMPTABLE algorithms. | |
The parser will use MERGE unless a condition forces it to use a temporary table. | |
UNDEFINED is the default, and it will be used if you omit the ALGORITHM keyword. | |
UNDEFINED is also used if the view specifies MERGE but can be processed only by using a temporary table. | |
Quote from www.mysql.com | |
|