1. 程式人生 > >hive sql分割槽和條件優化

hive sql分割槽和條件優化

分割槽過濾

  1. 如果不加分割槽,預設會掃描整個表的資料
  2. 如何查看錶有哪些分割槽:show partitions databaseName.tableName
  3. 如何確認分割槽是否生效:explain dependency sql

分割槽放置位置

  1. 普通查詢,分割槽過濾放在where後面,如
    select * from table1 t1 where t1.date between '20151205' and '20151206'
    • 說明:var between ‘a’ and ‘b’意思是var>=’a’ and var<=’b’
  2. inner join,分割槽過濾放在where後面,如
    select * from table1 t1 join table t2 on (t1.id=t2.id) where t1.date between '20151205' and '20151206' and t2.date between '20151205' and '20151206'
  3. left join,左邊表的分割槽過濾放在where後面,右邊表分割槽過濾放在on後面,如
    select * from table1 t1 left join table t2 on (t1.id=t2.id and t2.date between '20151205' and '20151206') where t1.date between '20151205' and '20151206'

    • 說明:right join相反

除了分割槽條件的放置之外其他的條件也類似,如t2.order_type=’3’放置在where後面則是在join之後進行過濾,放在on後面則是在join之前過濾