使用任何方法进行查询优化

Query optimization using any method

我需要加快这个查询。我能做什么?

select i.resp_id as id from int_result i, response_set rs, cx_store_child cbu 
where rs.survey_id IN(5550512,5550516,5550521,5550520,5590351,5590384,5679615,5679646,5691634,5699259,5699266,5699270)
        and i.q_id IN(52603091,52251250,52250724,52251333,52919541,52920117,54409178,54409806,54625102,54738933,54739117,54739221) 
        and rs.t >= '2017-08-30 00:00:00' and rs.t <= '2017-09-30 00:00:00' 
        and i.response_set_id = rs.id and rs.cx_business_unit_id = cbu.child_bu_id 
        and cbu.business_unit_id = 30850 
group by rs.cx_business_unit_id, i.a_id, extract(day from rs.t)
------------+------------+-----------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table      | Non_unique | Key_name        | Seq_in_index | Column_name     | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+------------+------------+-----------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| int_result |          0 | PRIMARY         |            1 | id              | A         |   240843099 |     NULL | NULL   |      | BTREE      |         |               |
| int_result |          1 | q_id            |            1 | q_id            | A         |     1442174 |     NULL | NULL   |      | BTREE      |         |               |
| int_result |          1 | a_id            |            1 | a_id            | A         |    20070258 |     NULL | NULL   |      | BTREE      |         |               |
| int_result |          1 | resp_id         |            1 | resp_id         | A         |   120421549 |     NULL | NULL   |      | BTREE      |         |               |
| int_result |          1 | response_set_id |            1 | response_set_id | A         |    26760344 |     NULL | NULL   |      | BTREE      |         |               |
| int_result |          1 | survey_id       |            1 | survey_id       | A         |      503855 |     NULL | NULL   | YES  | BTREE      |         |               |
| int_result |          1 | survey_id_2     |            1 | survey_id       | A         |     1459655 |     NULL | NULL   | YES  | BTREE      |         |               |
| int_result |          1 | survey_id_2     |            2 | q_id            | A         |     2736853 |     NULL | NULL   |      | BTREE      |         |               |
+------------+------------+-----------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+------
--+---------+---------------+
| Table        | Non_unique | Key_name             | Seq_in_index | Column_name         | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+--------------+------------+----------------------+--------------+---------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| response_set |          0 | PRIMARY              |            1 | id                  | A         |    14307454 |     NULL | NULL   |      | BTREE      |         |               |
| response_set |          1 | survey_id            |            1 | survey_id           | A         |      223553 |     NULL | NULL   |      | BTREE      |         |               |
| response_set |          1 | id                   |            1 | id                  | A         |    14307454 |     NULL | NULL   |      | BTREE      |         |               |
| response_set |          1 | external_id          |            1 | external_id         | A         |        2921 |     NULL | NULL   | YES  | BTREE      |         |               |
| response_set |          1 | panel_member_id      |            1 | panel_member_id     | A         |      357686 |     NULL | NULL   | YES  | BTREE      |         |               |
| response_set |          1 | email_group          |            1 | email_group         | A         |       21259 |     NULL | NULL   | YES  | BTREE      |         |               |
| response_set |          1 | survey_timestamp_idx |            1 | survey_id           | A         |      433559 |     NULL | NULL   |      | BTREE      |         |               |
| response_set |          1 | survey_timestamp_idx |            2 | t                   | A         |    14307454 |     NULL | NULL   | YES  | BTREE      |         |               |
| response_set |          1 | bu_id                |            1 | cx_business_unit_id | A         |        2246 |     NULL | NULL   | YES  | BTREE      |         |               |
----------------+------------+------------------+--------------+------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table          | Non_unique | Key_name         | Seq_in_index | Column_name      | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+----------------+------------+------------------+--------------+------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| cx_store_child |          0 | PRIMARY          |            1 | id               | A         |       13667 |     NULL | NULL   |      | BTREE      |         |               |
| cx_store_child |          0 | bu_child_ref     |            1 | business_unit_id | A         |       13667 |     NULL | NULL   | YES  | BTREE      |         |               |
| cx_store_child |          0 | bu_child_ref     |            2 | child_bu_id      | A         |       13667 |     NULL | NULL   | YES  | BTREE      |         |               |
| cx_store_child |          1 | cx_feedback_id   |            1 | cx_feedback_id   | A         |         506 |     NULL | NULL   | YES  | BTREE      |         |               |
| cx_store_child |          1 | business_unit_id |            1 | business_unit_id | A         |       13667 |     NULL | NULL   | YES  | BTREE      |         |               |
| cx_store_child |          1 | child_bu_id      |            1 | child_bu_id      | A         |       13667 |     NULL | NULL   | YES  | BTREE      |         |               |
+----------------+------------+------------------+--------------+------------------+-----------+-------------+----------+--------+------+------------+

您似乎在 response_set(survey_id, t) 上有一个索引。

尝试在

上创建所谓的 compound covering index
response_set(t, survey_id, cx_business_unit_id)

这可能有助于使用 table 优化您的查询部分。为什么?您的查询要求在 t 上进行范围扫描,范围扫描中使用的列必须在其复合索引中排在第一位。

同样,int_result (q_id, resp_id, response_set_id) 上的索引可能有助于从 table 中提取您需要的数据。

一些注意事项:

  1. 很难说清你的查询是做什么的。也许一些解释可以帮助您在这里获得更好的结果?
  2. and rs.t >= '2017-08-30 00:00:00' and rs.t <= '2017-09-30 00:00:00' 对于时间范围的结束可能是不正确的。它可能包含一个差一错误。你想要 < 代替 <= 吗?您提供的内容包括时间戳恰好在 2017 年 9 月 30 日午夜的记录,但没有当天午夜后的记录。
  3. 您在 int_result(survey_id, q_id) 上有一个索引,在 int_result(survey_id) 上有另一个索引。后一个索引与前一个完全多余,您可以删除它。
  4. 您似乎有很多单列索引。专业提示:除非您知道需要它们,否则不要添加此类索引。它们很少有助于加快任意查询的速度,并且总是减慢插入和更新的速度。你为什么需要它们?如果您有一个查询,您知道需要它们,或者您需要强制执行唯一性。删除不需要的索引。
  5. 使用 21 世纪的 JOIN 语法代替老式的逗号连接语法,如下所示。更容易阅读。
   from int_result i 
   join response_set rs    on i.response_set_id = rs.id
   join cx_store_child cbu on  rs.cx_business_unit_id = cbu.child_bu_id

读这个。您正在维护一个大型数据库,花时间学习很多关于索引的知识是值得的。 http://use-the-index-luke.com/

优化器可以通过多种方式尝试执行查询。以下索引使它可以灵活地找到击中表格的最佳顺序:

cbu:  INDEX(business_unit_id, child_bu_id)
rs:   INDEX(t, cx_business_unit_id, survey_id)
rs:   INDEX(survey_id, t, cx_business_unit_id)
rs:   INDEX(cx_business_unit_id, survey_id, t)
i:    INDEX(response_set_id, q_id)
i:    INDEX(q_id, response_set_id)

我安排 rscbu 在所有情况下都有 "covering" 索引;这对一些人有帮助。

(是的,您应该按照 O. Jones 的建议更改为 JOIN...ON。以及他的其他建议。)

在进一步讨论之前,请提供SHOW CREATE TABLE;也可能是数据类型问题。

一个 PRIMARY KEY 是一个 UNIQUE 键是一个 INDEX -- 所以 rs 中的 INDEX(id) 是多余的。