Search code examples
hivehql

hive "with tbl as" vs "create table tbl as"


  1. Does < with tbl as > much faster than < create table tbl as > ?
   with tbl as 
   (
    select 
      id,name 
    from 
      a
   )
   select id from tbl;

   create table tbl 
   as 
   select 
      id,name 
   from 
      a;

   select id from tbl;
  1. If I want use tbl in many querys, how to use < with tbl as >?
    with tbl as 
   (
    select 
      id,name 
    from 
      a
   )
   select id from tbl;

   select name from tbl;


Solution

    1. There's no obvious performance gap.

    2. with tbl as is a common table expression, aka CTE, which is only accessible within a single query. So we CAN NOT use CTE across multiple SQL queries separated by ;.

    3. Favor create temporary table table over create table. The former is visible within a single session, and will be gone when the session ends.