当前位置:   article > 正文

Phoenix为Hbase创建二级索引_hbase phoenix 二级索引

hbase phoenix 二级索引

一、概念

1、索引分类

二级索引主要分为全局索引和本地索引,其中全局索引包含覆盖索引和函数索引(Phoeinx 4.3 版本之后新增的);

2、索引概念

Global Indexes(全局索引)。
全局索引将索引表和数据表分开存储,都会创建一张新的索引表,因此每条数据和其索引数据可能会分布在不同的数据节点上,数据表的添加、删除和修改都会更新相关的索引表,所以写入数据时由于额外的网络开销会带来较大的性能消耗。而查询数据的时候,Phoenix 会通过索引表来快速低损耗的获取数据。因此全局索引更适合读多写少的使用场景。

Local Indexes(本地索引)
本地索引与全局索引相反,在 4.8.0 版本之后会将索引数据以特定的列簇存储在同一张数据表中,并通过特定的 rowkey 设置,将每条数据及其索引数据存储在同一 region 中,因此在数据写入时防止了额外的网络开销,而在读取数据时因无法提前判断索引数据的准确位置,则会在所有的 region 中检索索引数据,而非常影响读取性能。所以本地索引更适合于写多读少的使用场景。

二、造数据

Phoenix单线程psql方式导入csv数据到hbase

三、操作

(一)、覆盖索引

覆盖索引是在索引表中直接存储某些常用的查询字段,当查询时相关字段时,无需再在基于 rowkey 索引的数据表中查询,提高了查询的效率。

1、实例1

注意观察同样的查询语句,创建二级索引前后的查询用时;
(1)创建索引前

  1. 0: jdbc:phoenix:master,slaves1,slaves2:2181> select count(*) from STU where "cf"."age" between 23 and 26;
  2. +-----------+
  3. | COUNT(1) |
  4. +-----------+
  5. | 144 |
  6. +-----------+
  7. 1 row selected (0.128 seconds)

(2)创建索引

  1. 0: jdbc:phoenix:master,slaves1,slaves2:2181> create index stu_age on STU("cf"."age");
  2. 600 rows affected (7.42 seconds)

(3)创建索引后

  1. 0: jdbc:phoenix:master,slaves1,slaves2:2181> select count(*) from STU where "cf"."age" between 23 and 26;
  2. +-----------+
  3. | COUNT(1) |
  4. +-----------+
  5. | 144 |
  6. +-----------+
  7. 1 row selected (0.04 seconds)

2、实例2

(1)创建索引前

  1. 0: jdbc:phoenix:master,slaves1,slaves2:2181> select * from STU where "cf"."name"='name555';
  2. +------+----------+------+----------+
  3. | ID | name | age | address |
  4. +------+----------+------+----------+
  5. | 555 | name555 | 32 | add555 |
  6. +------+----------+------+----------+
  7. 1 row selected (0.08 seconds)

(2)创建索引

  1. 0: jdbc:phoenix:master,slaves1,slaves2:2181> create index stu_age_02 on STU("cf"."age") INCLUDE("cf"."name");
  2. 600 rows affected (7.363 seconds)

(3)创建索引后

  1. 0: jdbc:phoenix:master,slaves1,slaves2:2181> select * from STU where "cf"."name"='name555';
  2. +------+----------+------+----------+
  3. | ID | name | age | address |
  4. +------+----------+------+----------+
  5. | 555 | name555 | 32 | add555 |
  6. +------+----------+------+----------+
  7. 1 row selected (0.066 seconds)

(二)、函数索引

函数索引是在 Phoeinx 4.3 版本之后新增的,它使得索引的建立不仅仅只限于基于列,而可以使用任意的表达式来创建索引,在查询时,如出现相同的表达式查询条件,则会自动优先检索索引表。

1、实例

(1)创建索引前

  1. 0: jdbc:phoenix:master,slaves1,slaves2:2181> select count(*) from STU where substr("cf"."address",4,6) between '200' and '300';
  2. +-----------+
  3. | COUNT(1) |
  4. +-----------+
  5. | 101 |
  6. +-----------+
  7. 1 row selected (0.083 seconds)

(2)创建索引

  1. 0: jdbc:phoenix:master,slaves1,slaves2:2181> create index stu_age_03 on STU(substr("cf"."address",4,6));
  2. 600 rows affected (7.378 seconds)

(3)创建索引后

  1. 0: jdbc:phoenix:master,slaves1,slaves2:2181> select count(*) from STU where substr("cf"."address",4,6) between '200' and '300';
  2. +-----------+
  3. | COUNT(1) |
  4. +-----------+
  5. | 101 |
  6. +-----------+
  7. 1 row selected (0.057 seconds)

(三)、本地索引

本地索引只要在原来索引创建时增加 local 关键字即可;

1、实例

(1)创建索引前

  1. 0: jdbc:phoenix:master,slaves1,slaves2:2181> select * from STU where "cf"."address"='add566';
  2. +------+----------+------+----------+
  3. | ID | name | age | address |
  4. +------+----------+------+----------+
  5. | 566 | name566 | 26 | add566 |
  6. +------+----------+------+----------+
  7. 1 row selected (0.225 seconds)

(2)创建索引

  1. 0: jdbc:phoenix:master,slaves1,slaves2:2181> create local index stu_add on STU("cf"."address");
  2. 600 rows affected (12.468 seconds)

(3)创建索引后

  1. 0: jdbc:phoenix:master,slaves1,slaves2:2181> select * from STU where "cf"."address"='add566';
  2. +------+----------+------+----------+
  3. | ID | name | age | address |
  4. +------+----------+------+----------+
  5. | 566 | name566 | 26 | add566 |
  6. +------+----------+------+----------+
  7. 1 row selected (0.151 seconds)

(四)、所有索引表

  1. 0: jdbc:phoenix:master,slaves1,slaves2:2181> !tables
  2. +------------+--------------+-------------+---------------+----------+------------+----------------------------+-----------------+----+
  3. | TABLE_CAT | TABLE_SCHEM | TABLE_NAME | TABLE_TYPE | REMARKS | TYPE_NAME | SELF_REFERENCING_COL_NAME | REF_GENERATION | IN |
  4. +------------+--------------+-------------+---------------+----------+------------+----------------------------+-----------------+----+
  5. | | | STU_ADD | INDEX | | | | | AC |
  6. | | | STU_AGE | INDEX | | | | | AC |
  7. | | | STU_AGE_02 | INDEX | | | | | AC |
  8. | | | STU_AGE_03 | INDEX | | | | | AC |
  9. | | SYSTEM | CATALOG | SYSTEM TABLE | | | | | |
  10. | | SYSTEM | FUNCTION | SYSTEM TABLE | | | | | |
  11. | | SYSTEM | SEQUENCE | SYSTEM TABLE | | | | | |
  12. | | SYSTEM | STATS | SYSTEM TABLE | | | | | |
  13. | | | STU | TABLE | | | | | |
  14. | | | STUDENT | TABLE | | | | | |
  15. | | | STUDENT01 | TABLE | | | | | |
  16. | | | student01 | TABLE | | | | | |
  17. +------------+--------------+-------------+---------------+----------+------------+----------------------------+-----------------+----

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/656697
推荐阅读
相关标签
  

闽ICP备14008679号