赞
踩
二级索引主要分为全局索引和本地索引,其中全局索引包含覆盖索引和函数索引(Phoeinx 4.3 版本之后新增的);
Global Indexes(全局索引)。
全局索引将索引表和数据表分开存储,都会创建一张新的索引表,因此每条数据和其索引数据可能会分布在不同的数据节点上,数据表的添加、删除和修改都会更新相关的索引表,所以写入数据时由于额外的网络开销会带来较大的性能消耗。而查询数据的时候,Phoenix 会通过索引表来快速低损耗的获取数据。因此全局索引更适合读多写少的使用场景。
Local Indexes(本地索引)
本地索引与全局索引相反,在 4.8.0 版本之后会将索引数据以特定的列簇存储在同一张数据表中,并通过特定的 rowkey 设置,将每条数据及其索引数据存储在同一 region 中,因此在数据写入时防止了额外的网络开销,而在读取数据时因无法提前判断索引数据的准确位置,则会在所有的 region 中检索索引数据,而非常影响读取性能。所以本地索引更适合于写多读少的使用场景。
覆盖索引是在索引表中直接存储某些常用的查询字段,当查询时相关字段时,无需再在基于 rowkey 索引的数据表中查询,提高了查询的效率。
注意观察同样的查询语句,创建二级索引前后的查询用时;
(1)创建索引前
- 0: jdbc:phoenix:master,slaves1,slaves2:2181> select count(*) from STU where "cf"."age" between 23 and 26;
- +-----------+
- | COUNT(1) |
- +-----------+
- | 144 |
- +-----------+
- 1 row selected (0.128 seconds)
(2)创建索引
- 0: jdbc:phoenix:master,slaves1,slaves2:2181> create index stu_age on STU("cf"."age");
- 600 rows affected (7.42 seconds)
(3)创建索引后
- 0: jdbc:phoenix:master,slaves1,slaves2:2181> select count(*) from STU where "cf"."age" between 23 and 26;
- +-----------+
- | COUNT(1) |
- +-----------+
- | 144 |
- +-----------+
- 1 row selected (0.04 seconds)
(1)创建索引前
- 0: jdbc:phoenix:master,slaves1,slaves2:2181> select * from STU where "cf"."name"='name555';
- +------+----------+------+----------+
- | ID | name | age | address |
- +------+----------+------+----------+
- | 555 | name555 | 32 | add555 |
- +------+----------+------+----------+
- 1 row selected (0.08 seconds)
(2)创建索引
- 0: jdbc:phoenix:master,slaves1,slaves2:2181> create index stu_age_02 on STU("cf"."age") INCLUDE("cf"."name");
- 600 rows affected (7.363 seconds)
(3)创建索引后
- 0: jdbc:phoenix:master,slaves1,slaves2:2181> select * from STU where "cf"."name"='name555';
- +------+----------+------+----------+
- | ID | name | age | address |
- +------+----------+------+----------+
- | 555 | name555 | 32 | add555 |
- +------+----------+------+----------+
- 1 row selected (0.066 seconds)
函数索引是在 Phoeinx 4.3 版本之后新增的,它使得索引的建立不仅仅只限于基于列,而可以使用任意的表达式来创建索引,在查询时,如出现相同的表达式查询条件,则会自动优先检索索引表。
(1)创建索引前
- 0: jdbc:phoenix:master,slaves1,slaves2:2181> select count(*) from STU where substr("cf"."address",4,6) between '200' and '300';
- +-----------+
- | COUNT(1) |
- +-----------+
- | 101 |
- +-----------+
- 1 row selected (0.083 seconds)
(2)创建索引
- 0: jdbc:phoenix:master,slaves1,slaves2:2181> create index stu_age_03 on STU(substr("cf"."address",4,6));
- 600 rows affected (7.378 seconds)
(3)创建索引后
- 0: jdbc:phoenix:master,slaves1,slaves2:2181> select count(*) from STU where substr("cf"."address",4,6) between '200' and '300';
- +-----------+
- | COUNT(1) |
- +-----------+
- | 101 |
- +-----------+
- 1 row selected (0.057 seconds)
本地索引只要在原来索引创建时增加 local 关键字即可;
(1)创建索引前
- 0: jdbc:phoenix:master,slaves1,slaves2:2181> select * from STU where "cf"."address"='add566';
- +------+----------+------+----------+
- | ID | name | age | address |
- +------+----------+------+----------+
- | 566 | name566 | 26 | add566 |
- +------+----------+------+----------+
- 1 row selected (0.225 seconds)
(2)创建索引
- 0: jdbc:phoenix:master,slaves1,slaves2:2181> create local index stu_add on STU("cf"."address");
- 600 rows affected (12.468 seconds)
(3)创建索引后
- 0: jdbc:phoenix:master,slaves1,slaves2:2181> select * from STU where "cf"."address"='add566';
- +------+----------+------+----------+
- | ID | name | age | address |
- +------+----------+------+----------+
- | 566 | name566 | 26 | add566 |
- +------+----------+------+----------+
- 1 row selected (0.151 seconds)
- 0: jdbc:phoenix:master,slaves1,slaves2:2181> !tables
- +------------+--------------+-------------+---------------+----------+------------+----------------------------+-----------------+----+
- | TABLE_CAT | TABLE_SCHEM | TABLE_NAME | TABLE_TYPE | REMARKS | TYPE_NAME | SELF_REFERENCING_COL_NAME | REF_GENERATION | IN |
- +------------+--------------+-------------+---------------+----------+------------+----------------------------+-----------------+----+
- | | | STU_ADD | INDEX | | | | | AC |
- | | | STU_AGE | INDEX | | | | | AC |
- | | | STU_AGE_02 | INDEX | | | | | AC |
- | | | STU_AGE_03 | INDEX | | | | | AC |
- | | SYSTEM | CATALOG | SYSTEM TABLE | | | | | |
- | | SYSTEM | FUNCTION | SYSTEM TABLE | | | | | |
- | | SYSTEM | SEQUENCE | SYSTEM TABLE | | | | | |
- | | SYSTEM | STATS | SYSTEM TABLE | | | | | |
- | | | STU | TABLE | | | | | |
- | | | STUDENT | TABLE | | | | | |
- | | | STUDENT01 | TABLE | | | | | |
- | | | student01 | TABLE | | | | | |
- +------------+--------------+-------------+---------------+----------+------------+----------------------------+-----------------+----
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。