CREATE VIEW Statement

The CREATE VIEW statement lets you create a shorthand abbreviation for a more complicated query. The base query can involve joins, expressions, reordered columns, column aliases, and other SQL features that can make a query hard to understand or maintain.

Because a view is purely a logical construct (an alias for a query) with no physical data behind it, ALTER VIEW only involves changes to metadata in the metastore database, not any data files in HDFS.

Syntax:

  1. CREATE VIEW [IF NOT EXISTS] view_name [(column_list)]
  2. AS select_statement

Statement type: DDL

Usage notes:

The CREATE VIEW statement can be useful in scenarios such as the following:

  • To turn even the most lengthy and complicated SQL query into a one-liner. You can issue simple queries against the view from applications, scripts, or interactive queries in impala-shell. For example:

    1. select * from view_name;
    2. select * from view_name order by c1 desc limit 10;

    The more complicated and hard-to-read the original query, the more benefit there is to simplifying the query using a view.

  • To hide the underlying table and column names, to minimize maintenance problems if those names change. In that case, you re-create the view using the new names, and all queries that use the view rather than the underlying tables keep running with no changes.
  • To experiment with optimization techniques and make the optimized queries available to all applications. For example, if you find a combination of WHERE conditions, join order, join hints, and so on that works the best for a class of queries, you can establish a view that incorporates the best-performing techniques. Applications can then make relatively simple queries against the view, without repeating the complicated and optimized logic over and over. If you later find a better way to optimize the original query, when you re-create the view, all the applications immediately take advantage of the optimized base query.
  • To simplify a whole class of related queries, especially complicated queries involving joins between multiple tables, complicated expressions in the column list, and other SQL syntax that makes the query difficult to understand and debug. For example, you might create a view that joins several tables, filters using several WHERE conditions, and selects several columns from the result set. Applications might issue queries against this view that only vary in their LIMIT, ORDER BY, and similar simple clauses.

For queries that require repeating complicated clauses over and over again, for example in the select list, ORDER BY, and GROUP BY clauses, you can use the WITH clause as an alternative to creating a view.

Complex type considerations:

For tables containing complex type columns (ARRAY, STRUCT, or MAP), you typically use join queries to refer to the complex values. You can use views to hide the join notation, making such tables seem like traditional denormalized tables, and making those tables queryable by business intelligence tools that do not have built-in support for those complex types. See Accessing Complex Type Data in Flattened Form Using Views for details.

Because you cannot directly issue SELECT col_name against a column of complex type, you cannot use a view or a WITH clause to “rename” a column by selecting it with a column alias.

If you connect to different Impala nodes within an impala-shell session for load-balancing purposes, you can enable the SYNC_DDL query option to make each DDL statement wait before returning, until the new or changed metadata has been received by all the Impala nodes. See SYNC_DDL Query Option for details.

Security considerations:

If these statements in your environment contain sensitive literal values such as credit card numbers or tax identifiers, Impala can redact this sensitive information when displaying the statements in log files and other administrative contexts. See the documentation for your Apache Hadoop distribution for details.

Cancellation: Cannot be cancelled.

HDFS permissions: This statement does not touch any HDFS files or directories, therefore no HDFS permissions are required.

Examples:

  1. -- Create a view that is exactly the same as the underlying table.
  2. create view v1 as select * from t1;
  3. -- Create a view that includes only certain columns from the underlying table.
  4. create view v2 as select c1, c3, c7 from t1;
  5. -- Create a view that filters the values from the underlying table.
  6. create view v3 as select distinct c1, c3, c7 from t1 where c1 is not null and c5 > 0;
  7. -- Create a view that that reorders and renames columns from the underlying table.
  8. create view v4 as select c4 as last_name, c6 as address, c2 as birth_date from t1;
  9. -- Create a view that runs functions to convert or transform certain columns.
  10. create view v5 as select c1, cast(c3 as string) c3, concat(c4,c5) c5, trim(c6) c6, "Constant" c8 from t1;
  11. -- Create a view that hides the complexity of a view query.
  12. create view v6 as select t1.c1, t2.c2 from t1 join t2 on t1.id = t2.id;

The following example creates a series of views and then drops them. These examples illustrate how views are associated with a particular database, and both the view definitions and the view names for CREATE VIEW and DROP VIEW can refer to a view in the current database or a fully qualified view name.

  1. -- Create and drop a view in the current database.
  2. CREATE VIEW few_rows_from_t1 AS SELECT * FROM t1 LIMIT 10;
  3. DROP VIEW few_rows_from_t1;
  4. -- Create and drop a view referencing a table in a different database.
  5. CREATE VIEW table_from_other_db AS SELECT x FROM db1.foo WHERE x IS NOT NULL;
  6. DROP VIEW table_from_other_db;
  7. USE db1;
  8. -- Create a view in a different database.
  9. CREATE VIEW db2.v1 AS SELECT * FROM db2.foo;
  10. -- Switch into the other database and drop the view.
  11. USE db2;
  12. DROP VIEW v1;
  13. USE db1;
  14. -- Create a view in a different database.
  15. CREATE VIEW db2.v1 AS SELECT * FROM db2.foo;
  16. -- Drop a view in the other database.
  17. DROP VIEW db2.v1;

Related information:

Overview of Impala Views, ALTER VIEW Statement, DROP VIEW Statement

Parent topic: Impala SQL Statements