有没有办法大大提高在Alfresco升级中使用的MySQL查询的速度

时间:2016-09-19 11:11:44

标签: mysql alfresco

作为升级过程的一部分,Alfresco会执行此查询

INSERT INTO ACT_HI_VARINST(
    ID_,
    PROC_INST_ID_,
    EXECUTION_ID_,
    TASK_ID_,
    NAME_,
    VAR_TYPE_,
    REV_,
    BYTEARRAY_ID_,
    DOUBLE_,
    LONG_,
    TEXT_,
    TEXT2_
    )
SELECT
    (@cnt := @cnt + 1),
    PROC_INST_ID_,
    EXECUTION_ID_,
    TASK_ID_,
    NAME_,
    VAR_TYPE_,
    REV_,
    BYTEARRAY_ID_,
    DOUBLE_,
    LONG_,
    TEXT_,
    TEXT2_
FROM ACT_HI_DETAIL AHD 
CROSS JOIN (SELECT @cnt := 177401 + 1) AS dummy
WHERE AHD.PROC_INST_ID_ not in (select PROC_INST_ID_ from ACT_HI_VARINST)
AND
    (AHD.PROC_INST_ID_ , AHD.NAME_, AHD.REV_, AHD.time_) IN
    (SELECT PROC_INST_ID_, NAME_, MAX(REV_), MAX(time_)
    FROM ACT_HI_DETAIL 
    GROUP BY PROC_INST_ID_ , NAME_);

运行需要12个多小时。

在查询上使用说明

explain SELECT
    (@cnt := @cnt + 1),
    PROC_INST_ID_,
    EXECUTION_ID_,
    TASK_ID_,
    NAME_,
    VAR_TYPE_,
    REV_,
    BYTEARRAY_ID_,
    DOUBLE_,
    LONG_,
    TEXT_,
    TEXT2_
FROM ACT_HI_DETAIL AHD
CROSS JOIN (SELECT @cnt := 177401 + 1) AS dummy
WHERE AHD.PROC_INST_ID_ not in (select PROC_INST_ID_ from ACT_HI_VARINST)
AND
    (AHD.PROC_INST_ID_ , AHD.NAME_, AHD.REV_, AHD.time_) IN
    (SELECT PROC_INST_ID_, NAME_, MAX(REV_), MAX(time_)
    FROM ACT_HI_DETAIL
    GROUP BY PROC_INST_ID_ , NAME_)\G

结果

*************************** 1. row ***************************
          id: 1
  select_type: PRIMARY
        table: <derived2>
        type: system
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
        rows: 1
        Extra: 
*************************** 2. row ***************************
          id: 1
  select_type: PRIMARY
        table: AHD
        type: ALL
possible_keys: ACT_IDX_HI_DETAIL_PROC_INST,ACT_IDX_HI_DETAIL_TIME,ACT_IDX_HI_DETAIL_NAME
          key: NULL
      key_len: NULL
          ref: NULL
        rows: 70669
        Extra: Using where
*************************** 3. row ***************************
          id: 1
  select_type: PRIMARY
        table: <subquery4>
        type: eq_ref
possible_keys: distinct_key
          key: distinct_key
      key_len: 976
          ref: alfresco.AHD.PROC_INST_ID_,alfresco.AHD.NAME_,alfresco.AHD.REV_,alfresco.AHD.TIME_
        rows: 1
        Extra: 
*************************** 4. row ***************************
          id: 4
  select_type: MATERIALIZED
        table: ACT_HI_DETAIL
        type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
        rows: 70669
        Extra: Using temporary
*************************** 5. row ***************************
          id: 3
  select_type: MATERIALIZED
        table: ACT_HI_VARINST
        type: index
possible_keys: ACT_IDX_HI_PROCVAR_PROC_INST
          key: ACT_IDX_HI_PROCVAR_PROC_INST
      key_len: 197
          ref: NULL
        rows: 41504
        Extra: Using index
*************************** 6. row ***************************
          id: 2
  select_type: DERIVED
        table: NULL
        type: NULL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
        rows: NULL
        Extra: No tables used

添加额外索引

ALTER TABLE ACT_HI_DETAIL ADD INDEX `ACT_HI_DETAIL_MULTI` (PROC_INST_ID_, TIME_, NAME_);

导致这里的行数减半

*************************** 2. row ***************************
          id: 1
  select_type: PRIMARY
        table: AHD
        type: range
possible_keys: act_hi_detail_multi_1
          key: act_hi_detail_multi_1
      key_len: 195
          ref: NULL
        rows: 35761
        Extra: Using index condition; Using where

数据库正在使用innodb。 将innodb_buffer_pool_size设置为4G没有任何区别。

表格定义:

MariaDB [alfresco]> desc act_hi_detail;
+---------------+---------------+------+-----+---------+-------+
| Field         | Type          | Null | Key | Default | Extra |
+---------------+---------------+------+-----+---------+-------+
| ID_           | varchar(64)   | NO   | PRI | NULL    |       |
| TYPE_         | varchar(255)  | NO   |     | NULL    |       |
| PROC_INST_ID_ | varchar(64)   | YES  | MUL | NULL    |       |
| EXECUTION_ID_ | varchar(64)   | YES  |     | NULL    |       |
| TASK_ID_      | varchar(64)   | YES  | MUL | NULL    |       |
| ACT_INST_ID_  | varchar(64)   | YES  | MUL | NULL    |       |
| NAME_         | varchar(255)  | NO   | MUL | NULL    |       |
| VAR_TYPE_     | varchar(255)  | YES  |     | NULL    |       |
| REV_          | int(11)       | YES  |     | NULL    |       |
| TIME_         | datetime      | NO   | MUL | NULL    |       |
| BYTEARRAY_ID_ | varchar(64)   | YES  |     | NULL    |       |
| DOUBLE_       | double        | YES  |     | NULL    |       |
| LONG_         | bigint(20)    | YES  |     | NULL    |       |
| TEXT_         | varchar(4000) | YES  |     | NULL    |       |
| TEXT2_        | varchar(4000) | YES  |     | NULL    |       |
+---------------+---------------+------+-----+---------+-------+
15 rows in set (0.00 sec)


MariaDB [alfresco]> desc act_hi_varinst;
+---------------+---------------+------+-----+---------+-------+
| Field         | Type          | Null | Key | Default | Extra |
+---------------+---------------+------+-----+---------+-------+
| ID_           | varchar(64)   | NO   | PRI | NULL    |       |
| PROC_INST_ID_ | varchar(64)   | YES  | MUL | NULL    |       |
| EXECUTION_ID_ | varchar(64)   | YES  |     | NULL    |       |
| TASK_ID_      | varchar(64)   | YES  |     | NULL    |       |
| NAME_         | varchar(255)  | NO   | MUL | NULL    |       |
| VAR_TYPE_     | varchar(100)  | YES  |     | NULL    |       |
| REV_          | int(11)       | YES  |     | NULL    |       |
| BYTEARRAY_ID_ | varchar(64)   | YES  |     | NULL    |       |
| DOUBLE_       | double        | YES  |     | NULL    |       |
| LONG_         | bigint(20)    | YES  |     | NULL    |       |
| TEXT_         | varchar(4000) | YES  |     | NULL    |       |
| TEXT2_        | varchar(4000) | YES  |     | NULL    |       |
+---------------+---------------+------+-----+---------+-------+
12 rows in set (0.00 sec)

当前索引是

MariaDB [alfresco]> show indexes from act_hi_detail;
+---------------+------------+-----------------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table         | Non_unique | Key_name                    | Seq_in_index | Column_name   | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------------+------------+-----------------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| act_hi_detail |          0 | PRIMARY                     |            1 | ID_           | A         |       71244 |     NULL | NULL   |      | BTREE      |         |               |
| act_hi_detail |          1 | ACT_IDX_HI_DETAIL_PROC_INST |            1 | PROC_INST_ID_ | A         |        3238 |     NULL | NULL   | YES  | BTREE      |         |               |
| act_hi_detail |          1 | ACT_IDX_HI_DETAIL_ACT_INST  |            1 | ACT_INST_ID_  | A         |        5937 |     NULL | NULL   | YES  | BTREE      |         |               |
| act_hi_detail |          1 | ACT_IDX_HI_DETAIL_TIME      |            1 | TIME_         | A         |        8905 |     NULL | NULL   |      | BTREE      |         |               |
| act_hi_detail |          1 | ACT_IDX_HI_DETAIL_NAME      |            1 | NAME_         | A         |         147 |     NULL | NULL   |      | BTREE      |         |               |
| act_hi_detail |          1 | ACT_IDX_HI_DETAIL_TASK_ID   |            1 | TASK_ID_      | A         |        5480 |     NULL | NULL   | YES  | BTREE      |         |               |
| act_hi_detail |          1 | act_hi_detail_multi_1       |            1 | PROC_INST_ID_ | A         |         199 |     NULL | NULL   | YES  | BTREE      |         |               |
| act_hi_detail |          1 | act_hi_detail_multi_1       |            2 | TIME_         | A         |         199 |     NULL | NULL   |      | BTREE      |         |               |
| act_hi_detail |          1 | act_hi_detail_multi_1       |            3 | NAME_         | A         |         199 |     NULL | NULL   |      | BTREE      |         |               |
+---------------+------------+-----------------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
9 rows in set (0.01 sec)

MariaDB [alfresco]> show indexes from act_hi_varinst;
+----------------+------------+------------------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table          | Non_unique | Key_name                     | Seq_in_index | Column_name   | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+----------------+------------+------------------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| act_hi_varinst |          0 | PRIMARY                      |            1 | ID_           | A         |       42358 |     NULL | NULL   |      | BTREE      |         |               |
| act_hi_varinst |          1 | ACT_IDX_HI_PROCVAR_PROC_INST |            1 | PROC_INST_ID_ | A         |        1925 |     NULL | NULL   | YES  | BTREE      |         |               |
| act_hi_varinst |          1 | ACT_IDX_HI_PROCVAR_NAME_TYPE |            1 | NAME_         | A         |         184 |     NULL | NULL   |      | BTREE      |         |               |
| act_hi_varinst |          1 | ACT_IDX_HI_PROCVAR_NAME_TYPE |            2 | VAR_TYPE_     | A         |         219 |     NULL | NULL   | YES  | BTREE      |         |               |
+----------------+------------+------------------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
4 rows in set (0.01 sec)

是否可以添加或修改可以对查询进行更多索引以大大提高速度?

0 个答案:

没有答案