参考:

Phoenix与HBase集成进行数据分析

HBase查询速度慢原因排查

操作1,执行查询,如下:

: jdbc:phoenix:node3::/hbase> SELECT * FROM ASSET_RECORD WHERE ASSET_ID='设345-1149640126759047168';
+-----------------------------------+---------------------------+-------------------+---------------+-----------------------------------+--------+
|                ID                 |         ASSET_ID          | MANAGEMENT_TABLE  | INTRODUCTION  |           MANAGEMENT_ID           |        |
+-----------------------------------+---------------------------+-------------------+---------------+-----------------------------------+--------+
| 0292ebbfdf3e4d97a6e9fc930ed126d4  | 设345-  | ASSET_SEAL        |               | dd9ff0fc0ad4486bb0812e78fa53ce0e  | - |
+-----------------------------------+---------------------------+-------------------+---------------+-----------------------------------+--------+
 row selected (0.081 seconds)

操作2,重复以上查询,如下:

: jdbc:phoenix:node3::/hbase> SELECT * FROM ASSET_RECORD WHERE ASSET_ID='设345-1149640126759047168';
+-----------------------------------+---------------------------+-------------------+---------------+-----------------------------------+--------+
|                ID                 |         ASSET_ID          | MANAGEMENT_TABLE  | INTRODUCTION  |           MANAGEMENT_ID           |        |
+-----------------------------------+---------------------------+-------------------+---------------+-----------------------------------+--------+
| 0292ebbfdf3e4d97a6e9fc930ed126d4  | 设345-  | ASSET_SEAL        |               | dd9ff0fc0ad4486bb0812e78fa53ce0e  | - |
+-----------------------------------+---------------------------+-------------------+---------------+-----------------------------------+--------+
 row selected (0.077 seconds)

操作3,使用explain重复以上查询,如下:

: jdbc:phoenix:node3::/hbase> explain SELECT * FROM ASSET_RECORD WHERE ASSET_ID='设345-1149640126759047168';
+----------------------------------------------------------------------------------------------------+-----------------+----------------+--------+
|                                                PLAN                                                | EST_BYTES_READ  | EST_ROWS_READ  |  EST_I |
+----------------------------------------------------------------------------------------------------+-----------------+----------------+--------+
| CLIENT -CHUNK  ROWS  BYTES PARALLEL -WAY ROUND ROBIN FULL SCAN OVER ASSET_RECORD  |        |          |  |
|     SERVER FILTER BY ASSET_ID =        |          |  |
+----------------------------------------------------------------------------------------------------+-----------------+----------------+--------+
 rows selected (0.015 seconds)

操作4,在表上建索引,如下:

: jdbc:phoenix:node3::/hbase> create index IDX_ASSET_RECORD on ASSET_RECORD(ASSET_ID,MANAGEMENT_TABLE);
, rows affected (6.25 seconds)

操作5,强制使用索引执行查询,如下:

: jdbc:phoenix:node3::/hbase> SELECT /*+ INDEX(ASSET_RECORD IDX_ASSET_RECORD)*/ * FROM ASSET_RECORD WHERE ASSET_ID='设345-1149640126759047168;
+-----------------------------------+---------------------------+-------------------+---------------+-----------------------------------+--------+
|                ID                 |         ASSET_ID          | MANAGEMENT_TABLE  | INTRODUCTION  |           MANAGEMENT_ID           |        |
+-----------------------------------+---------------------------+-------------------+---------------+-----------------------------------+--------+
| 0292ebbfdf3e4d97a6e9fc930ed126d4  | 设345-  | ASSET_SEAL        |               | dd9ff0fc0ad4486bb0812e78fa53ce0e  | - |
+-----------------------------------+---------------------------+-------------------+---------------+-----------------------------------+--------+
 row selected (0.058 seconds)

操作6,强制使用索引执行查询,如下:

: jdbc:phoenix:node3::/hbase> SELECT /*+ INDEX(ASSET_RECORD IDX_ASSET_RECORD)*/ * FROM ASSET_RECORD WHERE ASSET_ID='设345-1149640126759047168';
+-----------------------------------+---------------------------+-------------------+---------------+-----------------------------------+--------+
|                ID                 |         ASSET_ID          | MANAGEMENT_TABLE  | INTRODUCTION  |           MANAGEMENT_ID           |        |
+-----------------------------------+---------------------------+-------------------+---------------+-----------------------------------+--------+
| 0292ebbfdf3e4d97a6e9fc930ed126d4  | 设345-  | ASSET_SEAL        |               | dd9ff0fc0ad4486bb0812e78fa53ce0e  | - |
+-----------------------------------+---------------------------+-------------------+---------------+-----------------------------------+--------+
 row selected (0.033 seconds)

操作7,使用explain强制使用索引执行查询,如下:

: jdbc:phoenix:node3::/hbase> explain SELECT /*+ INDEX(ASSET_RECORD IDX_ASSET_RECORD)*/ * FROM ASSET_RECORD WHERE ASSET_ID='设345-114964012679047168';
+------------------------------------------------------------------------------------------------------------------+-----------------+-----------+
|                                                       PLAN                                                       | EST_BYTES_READ  | EST_ROWS_ |
+------------------------------------------------------------------------------------------------------------------+-----------------+-----------+
| CLIENT -CHUNK  ROWS  BYTES PARALLEL -WAY ROUND ROBIN FULL SCAN OVER ASSET_RECORD                | null            | null      |
|     SKIP-SCAN-JOIN TABLE                                                                                        | null            | null      |
|         CLIENT -CHUNK PARALLEL -WAY ROUND ROBIN RANGE SCAN OVER IDX_ASSET_RECORD ['设345-1149640126759047168']  | null            | null      |
|             SERVER FILTER BY FIRST KEY ONLY                                                                      | null            | null      |
|     DYNAMIC SERVER FILTER BY .$)                                                      | null            | null      |
+------------------------------------------------------------------------------------------------------------------+-----------------+-----------+
 rows selected (0.045 seconds)

操作8,删除索引,如下:

: jdbc:phoenix:node3::/hbase> drop index IDX_ASSET_RECORD on ASSET_RECORD;
No rows affected (3.688 seconds)

计算操作1和操作2的平均执行时间,建索引后,计算操作5和操作6的平均执行时间,经比较发现使用索引确实提高了查询的速度。

Phoenix具有索引同步更新机制,增删改一条或多条数据以后,索引会自动更新;但是,如果原来的表增加了字段,那就需要更新建在表上的索引。

表的属性越多,条目越多,建索引节约的时间越多,如下是82个属性和195821条记录的表:

: jdbc:phoenix:node3::/hbase> SELECT COUNT(*) FROM ASSET_NORMAL;
+-----------+
| COUNT()  |
+-----------+
|     |
+-----------+
 row selected (4.54 seconds)
: jdbc:phoenix:node3::/hbase> create index IDX_ASSET_NORMAL on ASSET_NORMAL(ASSET_ID,ASSET_NAME,USER_ID);
, rows affected (8.887 seconds)
: jdbc:phoenix:node3::/hbase> SELECT /*+ INDEX(ASSET_NORMAL IDX_ASSET_NORMAL)*/ * FROM ASSET_NORMAL WHERE ASSET_ID='仪1-1151470269278326784';
+-----------------------------------+-------------------------+-------------+------------------------+--------------------------+----------------+
|                ID                 |        ASSET_ID         | ASSET_NAME  | ASSET_FIRST_DEGREE_ID  | ASSET_FIRST_DEGREE_NAME  | ASSET_SECOND_D |
+-----------------------------------+-------------------------+-------------+------------------------+--------------------------+----------------+
| 002e028151e24b07a21e0a0e9ce7f74c  | 仪1-  | 测量仪器        |                 | 仪表                       |      |
+-----------------------------------+-------------------------+-------------+------------------------+--------------------------+----------------+
 row selected (0.209 seconds)
: jdbc:phoenix:node3::/hbase> SELECT * FROM ASSET_NORMAL WHERE ASSET_ID='仪1-1151470269278326784';
+-----------------------------------+-------------------------+-------------+------------------------+--------------------------+----------------+
|                ID                 |        ASSET_ID         | ASSET_NAME  | ASSET_FIRST_DEGREE_ID  | ASSET_FIRST_DEGREE_NAME  | ASSET_SECOND_D |
+-----------------------------------+-------------------------+-------------+------------------------+--------------------------+----------------+
| 002e028151e24b07a21e0a0e9ce7f74c  | 仪1-  | 测量仪器        |                 | 仪表                       |      |
+-----------------------------------+-------------------------+-------------+------------------------+--------------------------+----------------+
 row selected (4.306 seconds)

参考:

https://my.oschina.net/puwenchao/blog/1935302

05-28 23:25