查看執行計劃:explain select kind, count(*) from table_name group by kind
常見名詞示意:
STAGE DEPENDENCIES:階段的依賴關系
FETCH Operator :抓取操作
limit: -1 未對數據做限制
TableScan:掃描的表
alias:查詢的表名
Select Operator:查詢操作
expressions:查詢的列名
outputColumnNames:輸出的別名
詳細執行計劃:explain extended select kind, count(*) from table_name group by kind
2.分區表:分區對應不同文件夾。
查詢時用where語句可以指定分區目錄dt='20211112'。
建表時用partitioned by(dt string)。
加載時需要指定分區信息 into table partition_table partition(dt='20211112')。
增加分區alter partition_table add partition(dt='20211122')。
刪除分區alter partition_table drop partition(dt='20211122')。
可同時增加或刪除多個,增加只需空格,刪除中間需要逗號隔開。
查看分區 show partitions partition_table;
分區字段可以指定多個。
3.動態分區:
動態分區:set hive.exec.dynamic.partition=true;
非嚴格狀態:set hive.exec.dynamic.partition.mode=nonstrict;
最大可創建動態分區:set hive.exec.max.dynamic.partitions=1000;
單個MR最大可創建動態分區:set hive.exec.max.dynamic.partitions.pernode=100
MR Job 中,最大可以創建多少個 HDFS 文件:set hive.exec.max.created.files=100000
空分區時否需要拋出異常:set hive.error.on.empty.partition=false
4.分桶表:將數據放到不同的文件
創建表clustered by(id)
用於抽樣tablesample(bucket 1 out of 4 on id);
5.文件存儲和壓縮格式
行存儲TEXTFILE、SEQUENCEFILE
列存儲ORC、PARQUET
LZO和SNAPPY有優秀的壓縮比和壓縮速度
6.裁剪
列裁剪,只讀取需要的列
分區裁剪,只讀取需要的分區
7.group by數據傾斜
Map端進行聚合:set hive.map.aggr = true;
Map端進行聚合操作的條目數目 set hive.groupby.mapaggr.checkinterval = 100000;
有數據傾斜的時候進行負載均衡:set hive.groupby.skewindata = true;
8.矢量計算,可以在似scan, filter, aggregation進行批量處理
set hive.vectorized.execution.enabled = true;
set hive.vectorized.execution.reduce.enabled = true;
9.left semi join用來替代exist/in
10.CBO優化
成本優化器
set hive.cbo.enable=true;
set hive.compute.query.using.stats=true;
set hive.stats.fetch.column.stats=true;
set hive.stats.fetch.partition.stats=true;
11.謂詞下推
set hive.optimize.ppd = true;
12.mapjoin:大表left join小表
set hive.auto.convert.join=true;
set hive.mapjoin.smalltable.filesize=25000000;
13.大表和大表join(極少用到)
Sort Merge Bucket Join
建表時
clustered by(id)
sorted by(id)
into 3 buckets
開啟桶join
set hive.optimize.bucketmapjoin = true;
set hive.optimize.bucketmapjoin.sortedmerge = true;
set hive.input.format=org.apache.hadoop.hive.ql.io.BucketizedHiveInputFormat;
14.笛卡爾積
hive.mapred.mode=strict;開啟後不會出現笛卡爾積