一、視圖
1、視圖定義
視圖其實是一個虛表,視圖可以允許保存一個查詢,並像對待表一樣對這個查詢進行操作,視圖是一個邏輯結構,並不會存儲數據。
2、視圖的創建
通過創建視圖來限制數據訪問可以用來保護信息不被隨意查詢。
create table userinfo(
id int,name string,age int,address string
);
create view some_userinfo as
select id,name from userinfo;
3、刪除視圖
drop view if exists userinfo;
4、關於視圖的知識
show tables 可以查看視圖,不能使用insert和load命令來操作視圖。
視圖是只讀的,對視圖的操作都是操作元數據。
二、索引
1、索引的概念:
hive中只有有限的索引功能,hive中沒有主鍵和外鍵的概念,可以通過對一些字段建立索引來加速某些操作。一張表的索引數據存儲在另外一張表中。
當邏輯分區太多太細時而無法使用時,建立索引也就成為分區的另外一個選擇。維護索引和創建索引需要消耗計算資源。
除了s3文件系統的數據,對外部表和視圖都是可以建立索引的。
目前hive通過實現索引接口的Java類(CompactIndexHandler)來指定索引處理器,還增加了Bitmap索引處理器(應用於排重后較少的列)
不能使用drop table語句之前刪除索引。
若有索引表,刪除一個索引將會刪除這個索引表。
若被索引的表被刪除了,那么對應的索引和索引表也會被刪除;若原始表的某個分區被刪除了,那么這個分區對應的分區索引同時會被刪除掉。
2、創建索引:
表結構:create table_employee(
name string,
salary float,
subordinates array<string>,
deductions map<string,float>
address struct<street:string,city:string,state:string,zip:int>
)partitioned by(country string,state string)
索引結構:create index employee_index on table_employee(country)
as ‘org.apache.hadoop.hive.ql.index.compact.CompactIndexHandler’ // 'Bitmap'
with deferred rebuild
in table employee_index_table
partitioned by (country,name) //此分區是針對於employee_index_table
使用索引:
SELECT 字段名表
FROM 表名
WITH (INDEX(索引名))
WHERE 查詢條件