Phoenix二级索引
1、基础配置
-
二级索引配置文件
hbase-site.xml
<!-- phoenix regionserver 配置参数--> <property> <name>hbase.regionserver.wal.codec</name> <value>org.apache.hadoop.hbase.regionserver.wal.IndexedWALEditCodec</value> </property>
-
分发hbase-site.xml
-
重启hbase使其生效。
2、全局索引(Global index)
Global Index是默认的索引格式,创建全局索引时,会在HBase中建立一张新表。也就是说索引数据和数据表是存放在不同的表中的,因此全局索引适用于多读少写的业务场景。
缺点:写数据的时候会消耗大量开销,因为索引表也要更新,而索引表是分布在不同的数据节点上的,跨节点的数据传输带来了较大的性能消耗。
优点:在读数据的时候Phoenix会选择索引表来降低查询消耗的时间。
准备
-
将HBase中的表,通过表映射的方式使得Phoenix中可见
hbase(main):113:0> create 'test','info' hbase(main):024:0> put 'test','1001','info:name','zhangsan' hbase(main):025:0> put 'test','1001','info:addr','beijing' 0: jdbc:phoenix:hadoop102:2181>create table "test"(id varchar primary key,"info"."name" varchar, "info"."addr" varchar) column_encoded_bytes=0;
- 创建单个字段的全局索引
--格式CREATE INDEX my_index ON my_table (my_col);
CREATE INDEX my_index ON "test" ("info"."name");
-
验证全局索引
存在的不足
当select其他字段(addr)时还是全局扫描,效率仍然低。
3、包含索引
--删除index
drop index my_index on "test";
--格式CREATE INDEX my_index ON my_table (v1) INCLUDE (v2);
CREATE INDEX my_index ON "test" ("info"."name") INCLUDE ("info"."addr");
4、本地索引
Local Index适用于写操作频繁的场景。索引数据和数据表的数据是存放在同一张表中(且是同一个Region),避免了在写操作的时候往不同服务器的索引表中写索引带来的额外开销。
--格式
CREATE LOCAL INDEX my_index ON my_table (my_column1,...,my_columnn);