Hive對錶建立索引

1.首先創建表

create table user(

  id int,

  name string,

  address string

)

ROW FORMAT DELIMITED

FIELDS TERMINATED BY ','

STORED AS TEXTFILE;

2.創建索引

create index user_index on table user(id)

as 'org.apache.hadoop.hive.ql.index.compact.CompactIndexHandler' 

with deferred rebuild 

idxproperties('creator' = 'Alex','create_at' = 'sometimes') 

in table user_index_table; -- 生成user_index_table一張額外的表,該表裏面 包括索引字段,以及該值所對應的HDFS文件路徑,和該值在文件中的偏移量。

alter index user_index on user rebuild;

這樣就對user表加了索引了,索引字段爲id。

3.刪除索引

drop index [if exists] user_index  on user;

4.加載索引數據

alter index  user_index on user [partition dt] rebuild;

5.查詢索引

show index on user;

6.使用索引的目的

在執行索引字段查詢的時候,首先生成一個額外的MR Job,根據對索引列的過濾條件,從索引表中過濾出索引列的值對應的HDFS文件路徑以及偏移量,輸出帶hdfs一個文件中,然後根據這些文件的hdfs路徑和偏移量篩選原始的input文件生成新的split,作爲這個job的split,這樣就可以不用全表掃描。使得查詢效率更高,速度更快。

7.使用索引的缺點

需要生成索引表,然後每次執行查詢要先跑個mr獲取所要的字段的信息從索引表中,會有一定的開銷。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章