如何识别性能问题查询 - MYSQL

时间:2017-05-17 12:55:51

标签: mysql sql mysql-python

我正在尝试构建一个查询,以便从连接到查找表的3个不同表中检索数据。

我的问题是查询需要很长时间才能运行(花了1个小时,我放弃了等待,假设出现问题,python脚本正在运行查询并将结果写入文件中)

查询如下所示:

SELECT a.id,
GROUP_CONCAT(DISTINCT b.product_id SEPARATOR ':') AS b_pids,
GROUP_CONCAT(DISTINCT c.product_id SEPARATOR ':') AS c_pids,
GROUP_CONCAT(DISTINCT d.product_id SEPARATOR ':') AS d_ids
FROM a_table a
LEFT JOIN b_table b ON a.customer_account_guid = b.customer_account_guid 
LEFT JOIN c_table c ON b.customer_account_guid = c.customer_account_guid 
LEFT JOIN d_table d ON c.customer_account_guid = d.customer_account_guid 
WHERE a.category = 'product_category' AND a.location = 'UK' GROUP BY a.id

此查询的解释结果为:

id  | select_type | table  | type       | possible_keys                                                                            |   key                     | key_len | ref                     | rows    |filtered |Extra
1   | SIMPLE      | a      | ref        | PRIMARY,idx_id,idx_customer_account_guid,idx_category_location,idx_category,idx_location | idx_category_location     | 47      | const,const             | 1211986 |100      |Using index condition
1   | SIMPLE      | b      | ref        | idx_customer_account_guid                                                                | idx_customer_account_guid | 300     | a.customer_account_guid | 6       |100      |NULL
1   | SIMPLE      | c      | ref        | idx_customer_account_guid                                                                | idx_customer_account_guid | 300     | b.customer_account_guid | 18      |100      |NULL
1   | SIMPLE      | d      | ref        | idx_customer_account_guid                                                                | idx_customer_account_guid | 300     | c.customer_account_guid | 2       |100      |NULL

表记录(计数)是:

a_table - 3 million

b_table - 8 million

c_table - 2 million

d_table - 4 million

正如你所看到的那样,索引似乎没有问题,而且查询并不复杂,任何想法为什么需要超过1小时?我认为上述计数不应该花那么长时间,或者我错了吗?

1 个答案:

答案 0 :(得分:2)

您正在使用连接生成荒谬的行数。相反,在加入之前聚合

DISTINCT

我从GROUP_CONCAT()删除了SELECT a.id, (SELECT GROUP_CONCAT(b.product_id SEPARATOR ':') as b_pids FROM b_table b WHERE a.customer_account_guid = b.customer_account_guid ) b_pids, (SELECT GROUP_CONCAT(c.product_id SEPARATOR ':') as b_pids FROM c_table c WHERE a.customer_account_guid = c.customer_account_guid ) b_pids, (SELECT GROUP_CONCAT(d.product_id SEPARATOR ':') FROM d_table d WHERE a.customer_account_guid = d.customer_account_guid ) d_pids FROM a_table a LEFT JOIN WHERE a.category = 'product_category' AND a.location = 'UK'; 。你可能仍然需要它。

编辑:

鉴于您在外部查询中进行过滤,相关子查询可能是最佳方法:

a_table(category, location, customer_account_guid)

对于此版本,您需要(customer_account_guid, product_id)上的索引以及其他每个表{1}}的索引。