使用任何方法查询优化

时间:2017-09-29 10:38:57

标签: mysql datetime join query-performance

我需要加快这个查询。我该怎么办?

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      |         |               |
+----------------+------------+------------------+--------------+------------------+-----------+-------------+----------+--------+------+------------+

2 个答案:

答案 0 :(得分:2)

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

尝试在

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

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

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

一些注意事项:

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

答案 1 :(得分:1)

优化程序可能会尝试执行查询的方法有很多种。以下索引可以灵活地找到命中表的最佳顺序:

from bqplot import pyplot as plt
import numpy as np
fig=plt.figure()
lin=plt.plot([0,1],
             [0,0])
scatt1=plt.scatter([0],[0],colors=['Red'])
scatt2=plt.scatter([1],[0],enable_move=True)

plt.xlim(-3,3)
plt.ylim(-3,3)
fig.layout.height = '500px'
fig.layout.width = '500px'
plt.show()
def call_back2(name, value):
   #print(value,name)
   if value['point']:
       X=value['point']['x']
       Y=value['point']['y']
       lin.x=[scatt1.x[0],(X-scatt1.x)[0]]
       lin.y=[scatt1.y[0],(Y-scatt1.y)[0]]

scatt2.on_drag_start(call_back2)
scatt2.on_drag(call_back2)
scatt2.on_drag_end(call_back2)

我安排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) rs在所有情况下都有“覆盖”索引;这有助于一些人。

(是的,你应该像O. Jones建议的那样改为cbu。其余的建议。)

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

SHOW CREATE TABLE PRIMARY KEY密钥是UNIQUE - 因此INDEX中的INDEX(id)是多余的。