文章目录

  • 函数索引 Functional Indexes
  • 全局索引Global Indexes
    • 配置hbase-site.xml
    • 测试索引
  • 特别注意
    • 强制使用索引表
    • 创建覆盖索引 covered index
    • 使用Local Indexing创建索引
  • 本地索引 Local Indexes
    • 配置hbase-site.xml
    • 创建本地索引
  • 异步创建索引
  • 索引重建
  • 参考资料

Phoenix5.0 安装部署 搭建了Phoenix环境,及一些简单shell使用,本文介绍一下Phoenix二级索引。

Phoenix二级索引分为全局索引和本地索引

函数索引 Functional Indexes

在4.3及以上的版本中,Phoenix提供了函数索引。
函数索引可以使用函数表达式来创建索引,在查询时,如果使用这个函数表达式查询,则可以使用到索引。

CREATE INDEX UPPER_NAME_IDX ON EMP (UPPER(FIRST_NAME||' '||LAST_NAME))SELECT EMP_ID FROM EMP WHERE UPPER(FIRST_NAME||' '||LAST_NAME)='JOHN DOE'

全局索引Global Indexes

Global indexing适用于多读少写的业务场景。使用Global indexing的话在写数据的时候会消耗大量开销,因为所有对数据表的更新操作(DELETE, UPSERT VALUES and UPSERT SELECT),会引起索引表的更新,而索引表是分布在不同的数据节点上的,跨节点的数据传输带来了较大的性能消耗。在读数据的时候Phoenix会选择索引表来降低查询消耗的时间。在默认情况下如果想查询的字段不是索引字段的话索引表不会被使用,也就是说不会带来查询速度的提升。

配置hbase-site.xml

如果想使用全局索引,需要配置hbase-site.xml:

<property><name>hbase.regionserver.wal.codec</name><value>org.apache.hadoop.hbase.regionserver.wal.IndexedWALEditCodec</value>
</property>

测试索引

create table company(id varchar primary key, name varchar, address varchar);
--- 查看索引
!indexes company;
-- 创建索引
create index my_index on company(name);
--删除索引表
drop index my_index on company
-- 查看表就会发现多了一张索引表
!tables
SELET * FROM MY_INDEX-- 插入数据
upsert into company(id, name, address) values('001', 'dimensoft', 'nanjing');
-- 查询数据
select name,address from company where name='dimensoft';-- 查询索引表MY_INDEX
SELECT * FROM MY_INDEX
或者
scan 'MY_INDEX'

特别注意

select name,address from company where name='dimensoft';这样的查询语句是不会用到索引表的.

Global mutable index will not be used unless all of the columns referenced in the query are contained in the index.

name字段虽然是索引字段但是address字段并不是索引字段!也就是说需要查询出来的字段必须都是索引字段如:
select name from company where name='dimensoft';

如果希望使用索引表进行查询的话可以使用以下三种方式来解决这个问题:

强制使用索引表

SELECT /*+ INDEX(company my_index) */ name,address FROM company WHERE name = 'dimensoft';

This will cause each data row to be retrieved when the index is traversed to find the missing address column value. This hint should only be used if you know that the index has good selective (i.e. a small number of table rows have a value of ‘dimensoft’ in this example), as otherwise you’ll get better performance by the default behavior of doing a full table scan.

这样的查询语句会导致二次检索数据表,第一次检索是去索引表中查找符合name为dimensoft的数据,这时候发现address字段并不在索引字段中,会去company表中第二次扫描,因此只有当用户明确知道符合检索条件的数据较少的时候才适合使用,否则会造成全表扫描,对性能影响较大。

创建覆盖索引 covered index

创建索引的时候指定一个covered字段,先删除my_index索引 drop index my_index on company;
创建covered index: create index my_index on company(name) include(address);

This will cause the address column value to be copied into the index and kept in synch as it changes. This will obviously increase the size of the index.
使用这种方式创建的所有会导致address字段的值被拷贝到索引中,缺点就是会导致索引表大小有一定的增加。

查询索引表my_index数据。select * from my_index;
这个时候就再使用select name,address from company where name='dimensoft';查询语句就会使用到索引来进行查询了。

使用Local Indexing创建索引

与Global Indexing不同,当使用Local Indexing的时候即使查询的所有字段都不在索引字段中时也会用到索引进行查询(这是由Local Indexing自动完成的)

本地索引 Local Indexes

Local indexing targets write heavy, space constrained use cases. Just like with global indexes, Phoenix will automatically select whether or not to use a local index at query-time. With local indexes, index data and table data co-reside on same server preventing any network overhead during writes. Local indexes can be used even when the query isn’t fully covered (i.e. Phoenix automatically retrieve the columns not in the index through point gets against the data table). Unlike global indexes, all local indexes of a table are stored in a single, separate shared table.At read time when the local index is used, every region must be examined for the data as the exact region location of index data cannot be predetermined.Thus some overhead occurs at read-time.
Local indexing适用于写操作频繁的场景。与Global indexing一样,Phoenix会自动判定在进行查询的时候是否使用索引。使用Local indexing时,索引数据和数据表的数据是存放在相同的服务器中的避免了在写操作的时候往不同服务器的索引表中写索引带来的额外开销。使用Local indexing的时候即使查询的字段不是索引字段索引表也会被使用,这会带来查询速度的提升,这点跟Global indexing不同。一个数据表的所有索引数据都存储在一个单一的独立的可共享的表中。在使用本地索引的读取时,必须检查每个区域region 的数据,因为不能预先确定索引数据的确切区域位置。因此在读取时发生一些开销。

配置hbase-site.xml

同样需要修改 hbase-site.xml文件,添加以下几个配置:

<property><name>hbase.master.loadbalancer.class</name><value>org.apache.phoenix.hbase.index.balancer.IndexLoadBalancer</value>
</property>
<property><name>hbase.coprocessor.master.classes</name><value>org.apache.phoenix.hbase.index.master.IndexMasterObserver</value>
</property>
<property><name>hbase.coprocessor.regionserver.classes</name><value>org.apache.hadoop.hbase.regionserver.LocalIndexMerger</value>
</property>

The above properties prevent deadlocks from occurring during index maintenance for global indexes (HBase 0.98.4+ and Phoenix 4.3.1+ only) by ensuring index updates are processed with a higher priority than data updates. It also prevents deadlocks by ensuring metadata rpc calls are processed with a higher priority than data rpc calls.
上述属性通过确保以比数据更新更高的优先级处理索引更新来防止在全局索引(HBase 0.98.4+和Phoenix 4.3.1+)的索引维护期间发生死锁。 它还通过确保以比数据rpc调用更高的优先级处理元数据rpc调用来防止死锁。
简而言之就是更新索引的优先级比数据更新和rpc调用的优先级高

创建本地索引

create local index my_index on company(name);

查看当前所有表会发现多一张MY_INDEX索引表,查询该表数据。

通过squirrel来查看company的索引字段。

从HBase的CLI界面查看当前所有表。

这里的索引表并不叫MY_INDEX,而是叫_LOCAL_IDX_COMPANY,但是在Phoenix的CLI中进行数据查询的时候仍然是使用MY_INDEX进行查询,应该是做了映射。

插入数据upsert into company(id, name, address) values('001', 'dimensoft', 'nanjing');

从HBase的CLI界面查看索引表_LOCAL_IDX_COMPANY。

3个索引字段_INDEX_ID、NAME和ID的值被合并为索引表的rowKey,其中_INDEX_ID并没有值(\x000是十六进制表示,转换为字符串是空格)。

异步创建索引

一般我们可以使用CREATE INDEX来创建一个索引,这是一种同步的方法。但是有时候我们创建索引的表非常大,我们需要等很长时间。Phoenix 4.5以后有一个异步创建索引的方式,使用关键字ASYNC来创建索引:

CREATE INDEX index1_c ON hao1 (age) INCLUDE(name) ASYNC;
这时候创建的索引表中不会有数据。你还必须要单独的使用命令行工具来执行数据的创建。当语句给执行的时候,后端会启动一个map reduce任务,只有等到这个任务结束,数据都被生成在索引表中后,这个索引才能被使用。启动工具的方法:

${HBASE_HOME}/bin/hbase org.apache.phoenix.mapreduce.index.IndexTool--schema MY_SCHEMA --data-table MY_TABLE --index-table ASYNC_IDX--output-path ASYNC_IDX_HFILES

这个任务不会因为客户端给关闭而结束,是在后台运行。你可以在指定的文件ASYNC_IDX_HFILES中找到最终实行的结果。

索引重建

有两种方式:

drop index ind on "testTable";
create index ind on "testTable"("ext"."userId");

或者

alter index ind on "testTable" REBUILD;

参考资料

  • 官网Phoenix 二级索引

Phoenix 5索引相关推荐

  1. 2021年大数据HBase(十二):Apache Phoenix 二级索引

    全网最详细的大数据HBase文章系列,强烈建议收藏加关注! 新文章都已经列出历史文章目录,帮助大家回顾前面的知识重点. 目录 系列历史文章 前言 Apache Phoenix 二级索引 一.索引分类 ...

  2. HBase phoenix二级索引

    1. 为什么需要用二级索引? 对于HBase而言,如果想精确地定位到某行记录,唯一的办法是通过rowkey来查询.如果不通过rowkey来查找数据,就必须逐行地比较每一列的值,即全表扫瞄.对于较大的表 ...

  3. Phoenix二级索引那些事儿(下)

    http://www.icaijing.com/hot/article4940159/ Phoenix二级索引那些事儿(下) 作者:中兴大数据| 发表时间:2015-7-30 03:31:18 索引配 ...

  4. 直播:Phoenix 全局索引原理与实践

    讲师:瑾谦--阿里数据库产品专家 主题:Phoenix 全局索引原理与实践 内容概要:全局索引是Phoneix的核心特性之一,此话题主要内容包括phoenix mutable表全局索引机制原理, 场景 ...

  5. Phoenix 二级索引探究

    版本信息: HDP -> 3.0.0 Hadoop -> 3.0.1 HBase -> 2.0.0 Phoenix -> 5.0.0 HBASE 是 Google-Bigtab ...

  6. Hbase索引( Phoenix二级索引)

    Hbase索引( Phoenix二级索引) 1. Phoenix简介 1.1.Phoenix安装 1.2.常用命令 1.3.phoenix表映射 1.3.1.视图映射 1.3.2.表映射 1.3.3. ...

  7. Phoenix二级索引(Secondary Indexing)的使用(转:https://www.cnblogs.com/MOBIN/p/5467284.html)

    摘要 HBase只提供了一个基于字典排序的主键索引,在查询中你只能通过行键查询或扫描全表来获取数据,使用Phoenix提供的二级索引,可以避免在查询数据时全表扫描,提高查过性能,提升查询效率 测试环境 ...

  8. phoenix创建索引报错“ Mutable secondary indexes must have the hbase.regionserver.wal.codec property”

    phoenix 创建hbase表索引时异常,报错如下 Error: ERROR 1029 (42Y88): Mutable secondary indexes must have the hbase. ...

  9. Phoenix 二级索引 的使用

    二级索引 二级索引是从主访问路径访问数据的一种正交方式.在HBase中,你有一个索引,它按照主行键按字典顺序排序.除了通过主行之外,以任何方式访问记录都可能需要扫描表中的所有行,以便根据筛选器对它们进 ...

最新文章

  1. 人工智能之算法知识与实战篇
  2. 【响应式Web前端设计】:link、:hover、:active和:visited的区别
  3. php重放,Api 接口安全-防篡改,防重放理解总结
  4. 数据库访问性能优化法则
  5. linux中通用GPIO接口的操作
  6. activiti7基础入门、activiti7实战、activiti7运用
  7. matlab 门限回归模型,门限回归及Stata操作汇总与空间门槛回归模型简介
  8. 读史使人明智,读诗使人灵秀,数学使人周密,科学使人深刻,伦理学使人庄重,逻辑修辞之学使人善辩:凡有所学,皆成性格。
  9. ffmpeg连接rtsp流提示Connection refused
  10. compiled.php,laravel compiled.php 缓存 命令行
  11. Computer:教你实用一招,如何实现点击PPT文件即可播放,而避免打开PowerPoint软件
  12. 招商加盟竞价推广,怎么做才会有效果?
  13. 算法设计 (分治法应用实验报告)基于分治法的合并排序、快速排序、最近对问题
  14. 咸鱼的 GitHub 情报 | 20200111 期
  15. 【微机原理与概述】微计算机概述
  16. html+p标签和span,文章段落用span和p标签对seo有影响吗
  17. A_A02_003 ST-LINK驱动安装
  18. PLC编程实现在指定范围内生成一个随机数
  19. 集成热云遇到的奇怪一个奇怪小问题
  20. 页面卡顿的原因及排查

热门文章

  1. oracle xdpyinfo,Oracle 11g安装“无法使用命令/usr/bin/xdpyinfo自动检查显示器颜色”报错解决...
  2. Android RecyclerView多样式列表实践指南
  3. Windows Server 2012中修改光驱盘符
  4. 数据结构与算法——每日一练(12月)
  5. QT显示中文 连接上文
  6. 用python制作水仙花
  7. 关于AI,你最该了解但从没想过的四个问题
  8. 我的奇思妙想隐形的机器人_我的奇思妙想机器人作文
  9. iphone 6分辨率
  10. 三星 9810 android 9,三星S9双版本对比:骁龙845碾压Exynos 9810