Hive索引

1、        Hive索引概述

Hive的索引目的是提高Hive表指定列的查询速度。

没有索引时,类似‘WHERE tab1.col1 = 10‘ 的查询,Hive会加载整张表或分区,然后处理所有的rows,但是如果在字段col1上面存在索引时,那么只会加载和处理文件的一部分。

与其他传统数据库一样,增加索引在提升查询速度时,会消耗额外资源去创建索引和需要更多的磁盘空间存储索引。

Hive 0.7.0版本中,加入了索引。Hive 0.8.0版本中增加了bitmap索引。

2、        索引相关的配置参数

hive.index.compact.file.ignore.hdfs

Default Value: false

Added In: Hive 0.7.0 withHIVE-1889

在索引文件中存储的hdfs地址将在运行时被忽略,如果开启的话;如果数据被迁移,那么索引文件依然可用,默认是false

hive.optimize.index.filter

Default Value: false

Added In: Hive 0.8.0 withHIVE-1644

是否自动使用索引, 默认是false

hive.optimize.index.filter.compact.minsize

Default Value: 5368709120

Added In: Hive 0.8.0 withHIVE-1644

压缩索引自动应用的最小输入大小

hive.optimize.index.filter.compact.maxsize

Default Value: -1

Added In: Hive 0.8.0 withHIVE-1644

压缩索引自动应用的最大输入大小,负值代表正无穷

hive.index.compact.query.max.size

Default Value: 10737418240

Added In: Hive 0.8.0 withHIVE-2096

一个使用压缩索引做的查询能取到的最大数据量,默认是10737418240 个byte;负值代表无穷大;

hive.index.compact.query.max.entries

Default Value: 10000000

Added In: Hive 0.8.0 withHIVE-2096

使用压缩索引查询时能读到的最大索引项数,默认是10000000;负值代表无穷大;

hive.exec.concatenate.check.index

Default Value: true

Added In: Hive 0.8.0 withHIVE-2125

如果设置为true,那么在做ALTER TABLE tbl_name CONCATENATE on a table/partition(有索引) 操作时,抛出错误;可以帮助用户避免index的删除和重建;

hive.optimize.index.groupby

Default Value: false

Added In: Hive 0.8.1 withHIVE-1694

hive.index.compact.binary.search

Default Value: true

Added In: Hive 0.8.1with HIVE-2535

在索引表中是否开启二分搜索进行索引项查询,默认是true;

3、        索引示例

注意:在Hive 0.12.0以及之前版本中,索引名称在create index和drop index语句中是大小写敏感的。然而,alter index 需要一个小写的索引名字。

此bug在Hive 0.13.0解决,此版本开始使索引名字大小写不敏感。

对于Hive 0.13.0之前的版本,最好使用小写的索引名字。

下面介绍索引的常见用法:

A、       Create/build,show和drop index

create index table01_index ontable table01(column2) as ‘COMPACT‘ with deferred rebuild;

show index on table01;

drop index table01_index ontable01;

B、       Create then build,show formatted和drop index

create index table02_index ontable table02(column3) as ‘compact‘ with deferred rebuild;

alter index table02_index ontable02 rebuild;

show formatted index ontable02;

drop index table02_index ontable02;

C、       创建bitmap索引,build,show 和drop

createindex table03_index on table table03 (column4) as
‘bitmap‘ with deferred rebuild;

alter index table03_index ontable03 rebuild;

show formatted index ontable03;

drop index table03_index on table03;

D、       在一张新表上创建索引

createindex table04_index on table table04 (column5)
as ‘compact‘
with deferred rebuild in tabletable04_index_table;

E、        创建索引,存储格式为RCFile

create index table05_index ontable table05 (column6) as ‘compact‘
with deferred rebuildstored as rcfile;

F、        创建索引,存储格式为TextFile

create index table06_index ontable table06 (column7) as ‘compact‘
with deferredrebuild row format delimited fields terminated by ‘\t‘ stored as textfile;

G、       创建带有索引属性的索引

create index table07_index ontable table07 (column8) as ‘compact‘
with deferred rebuild idxproperties("prop1"="value1", "prop2"="value2");

H、       创建带有表属性的索引

create index table08_index ontable table08 (column9) as ‘compact‘
withdeferred rebuild tblproperties("prop3"="value3", "prop4"="value4");

I、        如果索引存在,则删除

drop index if exists table09_indexon table09;

J、        在分区上重建索引

alter index table10_index on table10partition (columnx=‘valueq‘, columny=‘valuer‘) rebuild;

4、        索引测试

(1)  查询表中行数

hive (hive)> select count(1)from userbook;

4409365

(2)  表中未创建索引前查询

hive (hive)> select * fromuserbook where book_id = ‘15999998838‘;

Query ID =hadoop_20150627165551_595da79a-0e27-453b-9142-7734912934c4

Total jobs = 1

Launching Job 1 out of 1

Number of reduce tasks is setto 0 since there‘s no reduce operator

Starting Job =job_1435392961740_0012, Tracking URL =http://gpmaster:8088/proxy/application_1435392961740_0012/

Kill Command =/home/hadoop/hadoop-2.6.0/bin/hadoop job -kill job_1435392961740_0012

Hadoop job information forStage-1: number of mappers: 2; number of reducers: 0

2015-06-27 16:56:04,666 Stage-1map = 0%,  reduce = 0%

2015-06-27 16:56:28,974 Stage-1map = 50%,  reduce = 0%, Cumulative CPU4.36 sec

2015-06-27 16:56:31,123 Stage-1map = 78%,  reduce = 0%, Cumulative CPU6.21 sec

2015-06-27 16:56:34,698 Stage-1map = 100%,  reduce = 0%, Cumulative CPU7.37 sec

MapReduce Total cumulative CPUtime: 7 seconds 370 msec

Ended Job =job_1435392961740_0012

MapReduce Jobs Launched:

Stage-Stage-1: Map: 2   Cumulative CPU: 7.37 sec   HDFS Read: 348355875 HDFS Write: 76 SUCCESS

Total MapReduce CPU Time Spent:7 seconds 370 msec

OK

userbook.book_id    userbook.book_name    userbook.author      userbook.public_date     userbook.address

15999998838     uviWfFJ KwCrDOA    2009-12-27  3b74416d-eb69-48e2-9d0d-09275064691b

Time taken: 45.678 seconds, Fetched: 1 row(s)

(3)  创建索引

hive (hive)> create indexuserbook_bookid_idx on table userbook(book_id) as ‘COMPACT‘ WITH DEFERREDREBUILD;

(4)  创建索引后再执行查询

hive (hive)> select * fromuserbook where book_id = ‘15999998838‘;

Query ID =hadoop_20150627170019_5bb5514a-4c8e-4c47-9347-ed0657e1f2ff

Total jobs = 1

Launching Job 1 out of 1

Number of reduce tasks is setto 0 since there‘s no reduce operator

Starting Job =job_1435392961740_0013, Tracking URL = http://gpmaster:8088/proxy/application_1435392961740_0013/

Kill Command =/home/hadoop/hadoop-2.6.0/bin/hadoop job -kill job_1435392961740_0013

Hadoop job information forStage-1: number of mappers: 2; number of reducers: 0

2015-06-27 17:00:30,429 Stage-1map = 0%,  reduce = 0%

2015-06-27 17:00:54,003 Stage-1map = 50%,  reduce = 0%, Cumulative CPU7.43 sec

2015-06-27 17:00:56,181 Stage-1map = 78%,  reduce = 0%, Cumulative CPU9.66 sec

2015-06-27 17:00:58,417 Stage-1map = 100%,  reduce = 0%, Cumulative CPU10.83 sec

MapReduce Total cumulative CPUtime: 10 seconds 830 msec

Ended Job =job_1435392961740_0013

MapReduce Jobs Launched:

Stage-Stage-1: Map: 2   Cumulative CPU: 10.83 sec   HDFS Read: 348356271 HDFS Write: 76 SUCCESS

Total MapReduce CPU Time Spent:10 seconds 830 msec

OK

userbook.book_id    userbook.book_name    userbook.author      userbook.public_date     userbook.address

15999998838     uviWfFJ KwCrDOA    2009-12-27  3b74416d-eb69-48e2-9d0d-09275064691b

Time taken: 40.549 seconds, Fetched: 1 row(s)

可以看到创建索引后,速度还是稍快一点的。

其实对于这种简单的查询,通过我们的设置,可以不用启动Map/Reduce的,而是启动Fetch task,直接从HDFS文件中filter过滤出需要的数据,需要设置如下参数:

set hive.fetch.task.conversion=more;

hive (hive)> select * fromuserbook where book_id = ‘15999998838‘;

OK

userbook.book_id    userbook.book_name    userbook.author      userbook.public_date     userbook.address

15999998838     uviWfFJ KwCrDOA    2009-12-27  3b74416d-eb69-48e2-9d0d-09275064691b

Time taken: 0.093 seconds,Fetched: 1 row(s)

可以看到速度更快了,毕竟省略掉了开启MR任务,执行效率提高不少。

参考:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Indexing

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-10 23:50:00

Hive索引的相关文章

HIVE—索引、分区和分桶的区别

一.索引 简介 Hive支持索引,但是Hive的索引与关系型数据库中的索引并不相同,比如,Hive不支持主键或者外键. Hive索引可以建立在表中的某些列上,以提升一些操作的效率,例如减少MapReduce任务中需要读取的数据块的数量. 为什么要创建索引? Hive的索引目的是提高Hive表指定列的查询速度.没有索引时,类似'WHERE tab1.col1 = 10' 的查询,Hive会加载整张表或分区,然后处理所有的rows,但是如果在字段col1上面存在索引时,那么只会加载和处理文件的一部分

hive 索引

一.Hive的索引 索引是标准的数据库技术,hive 0.7版本之后支持索引.Hive提供有限的索引功能,这不像传统的关系型数据库那样有"键(key)"的概念,用户可以在某些列上创建索引来加速某些操作,给一个表创建的索引数据被保存在另外的表中. Hive的索引功能现在还相对较晚,提供的选项还较少.但是,索引被设计为可使用内置的可插拔的java代码来定制,用户可以扩展这个功能来满足自己的需求. 当然不是说有的查询都会受惠于Hive索引.用户可以使用EXPLAIN语法来分析HiveQL语句

hive里的索引

Hive支持索引,但是Hive的索引与关系型数据库中的索引并不相同,比如,Hive不支持主键或者外键. Hive索引可以建立在表中的某些列上,以提升一些操作的效率,例如减少MapReduce任务中需要读取的数据块的数量. 在可以预见到分区数据非常庞大的情况下,索引常常是优于分区的. 博主我推荐各位博文们通过查阅Hive文档对Hive表的索引进行更深入的了解. 需要时刻记住的是,Hive并不像事物数据库那样针对个别的行来执行查询.更新.删除等操作.这些操作依赖高效的索引来实现高性能. Hive是一

第53课:Hive 第一课:Hive的价值、Hive的架构设计简介

一. Hive的历史价值 1, 大数据因Hadoop而知名,而Hadoop又因Hive而实用.Hive是Hadoop上的Killer Application,Hive是Hadoop上的数据仓库,同时Hive兼具有数据仓库中的存储和查询引擎.而Spark SQL是一个更加出色和高级的查询引擎,并不提供存储功能.所以Spark SQL无法取代Hive,在现在企业级应用中Spark SQL+Hive成为了业界使用的大数据最为高效和流行的趋势. 2,Hive是Facebook推出的,主要是为了让不懂ja

Hive基础(1)---Hive是什么

1. Hive是什么 Hive是基于Hadoop的数据仓库解决方案.由于Hadoop本身在数据存储和计算方面有很好的可扩展性和高容错性,因此使用Hive构建的数据仓库也秉承了这些特性. 这是来自官方的解释. 简单来说,Hive就是在Hadoop上架了一层SQL接口,可以将SQL翻译成MapReduce去Hadoop上执行,这样就使得数据开发和分析人员很方便的使用SQL来完成海量数据的统计和分析,而不必使用编程语言开发MapReduce那么麻烦. 先上一张经典的Hive架构图: Hive架构图 如

Hive学习之路 (三)Hive元数据信息对应MySQL数据库表

概述 Hive 的元数据信息通常存储在关系型数据库中,常用MySQL数据库作为元数据库管理.上一篇hive的安装也是将元数据信息存放在MySQL数据库中. Hive的元数据信息在MySQL数据中有57张表 一.存储Hive版本的元数据表(VERSION) VERSION   -- 查询版本信息 该表比较简单,但很重要. VER_ID SCHEMA_VERSION VERSION_COMMENT ID主键 Hive版本 版本说明 1 0.13.0 Set by MetaStore 如果该表出现问题

Hive 官方手册翻译 -- Hive DDL(数据定义语言)

Hive DDL(数据定义语言) Confluence Administrator创建, Janaki Lahorani修改于 2018年9月19日 原文链接 https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL 翻译:Google Google翻译,金山软件 金山词霸 校对:南大通用 范振勇 (2018.9.26) 一.概述 这里是HiveQL DDL语句的文档,其中包括: CREATE 数据库/SCHEMA,表

老钱说大数据(1)----大数据OLAP与OLTP分析

1. 首先,咱们先不拿大数据说事,先分析一下OLAP及OLTP. OLAP: 联机分析处理(OLAP)系统是数据仓库系统最主要的应用,专门设计用于支持复杂的分析操作,侧重对决策人员和高层管理人员的决策支持. OLTP: 联机事务处理(OLTP,On-line Transaction Processing)应用,它所存储的数据被称为操作数据或者业务数据. 所以从定位上来讲,OLAP的定位是用来做数据分析(类BI),OLTP适合做一些事务的类的数据管理如查询如订单数据的产生. 举个通俗的例子,一个小

hive创建索引

索引是hive0.7之后才有的功能,创建索引需要评估其合理性,因为创建索引也是要磁盘空间,维护起来也是需要代价的 hive> create index user_index on table user(id) > as 'org.apache.hadoop.hive.ql.index.compact.CompactIndexHandler' > with deferred rebuild > IN TABLE user_index_table; hive> alter ind