mysql 7 +加入一个表,如何加速?

时间:2017-06-16 12:13:29

标签: mysql join

我有7张桌子。表1是表2-7的“父表”和has_many关系。

我想创建一个查询表,用表2-7中的id列表来获取表1的行。我已经尝试过以下查询,但是对于大型数据库,我的查询需要15秒。我想知道如何更快地进行此查询?

修改以下几点说明: - >删除Distinct只能节省4秒,我仍然可以获得10-11秒的查询

- >从查询中删除1个连接表(无关紧要)会将时间从15秒减少到2-3秒。删除2个连接表(再次无关紧要)将查询减少到1/2秒。

SELECT  
  table1.table1_id as table1Id, 
  GROUP_CONCAT(DISTINCT table2.table2_id) AS table2Ids,
  GROUP_CONCAT(DISTINCT table3.table3_id) AS table3Ids,
  GROUP_CONCAT(DISTINCT table4.table4_id) AS table4Ids,
  GROUP_CONCAT(DISTINCT table5.table5_id) AS table5Ids,
  GROUP_CONCAT(DISTINCT table6.table6_id) AS table6Ids,
  GROUP_CONCAT(DISTINCT table7.table7_id) AS table7Ids
  FROM table1
  LEFT JOIN table2 ON table1.table1_id = table2.table1_id
  LEFT JOIN table3 ON table1.table1_id = table3.table1_id
  LEFT JOIN table4 ON table1.table1_id = table4.table1_id
  LEFT JOIN table5 ON table1.table1_id = table5.table1_id
  LEFT JOIN table6 ON table1.table1_id = table6.table1_id
  LEFT JOIN table7 ON table1.table1_id = table7.table1_id
  WHERE table1.archived = false
  GROUP BY table1.table1_id LIMIT 1000

我的解释查询:

+----+-------------+------------------+------------+-------+----------------------------------------------------------------------------------------------------+---------------------------+---------+--------------------------+------+----------+-------------+
| id | select_type | table            | partitions | type  | possible_keys                                                                                      | key                       | key_len | ref                      | rows | filtered | Extra       |
+----+-------------+------------------+------------+-------+----------------------------------------------------------------------------------------------------+---------------------------+---------+--------------------------+------+----------+-------------+
|  1 | SIMPLE      | table1           | NULL       | index | PRIMARY,unique_name_in_table8, table8_idx,table9_idx,table10_idx                                   | PRIMARY                   | 4       | NULL                     |    1 |    10.00 | Using where |
|  1 | SIMPLE      | table2           | NULL       | ref   | PRIMARY                                                                                            | PRIMARY                   | 4       | db.table1.table1_id      |   20 |   100.00 | Using index |
|  1 | SIMPLE      | table3           | NULL       | ref   | table3_to_table1_id_idx                                                                            | table3_to_table1_id_idx   | 4       | db.table1.table1_id      |  824 |   100.00 | Using index |
|  1 | SIMPLE      | table4           | NULL       | ref   | table4_word_unique,table4_to_table1_id_idx                                                         | table4_to_table1_id_idx   | 4       | db.table1.table1_id      |    4 |   100.00 | Using index |
|  1 | SIMPLE      | table5           | NULL       | ref   | table5_to_table1_id_idx                                                                            | table5_to_table1_id_idx   | 4       | db.table1.table1_id      |   26 |   100.00 | Using index |
|  1 | SIMPLE      | table6           | NULL       | ref   | table6_to_table1_id_idx                                                                            | table6_to_table1_id_idx   | 4       | db.table1.table1_id      |    3 |   100.00 | Using index |
|  1 | SIMPLE      | table7           | NULL       | ref   | table7_to_table1_id_idx                                                                            | table7_to_table1_id_idx   | 4       | db.table1.table1_id      |  483 |   100.00 | Using index |
+----+-------------+------------------+------------+-------+----------------------------------------------------------------------------------------------------+---------------------------+---------+--------------------------+------+----------+-------------+

2 个答案:

答案 0 :(得分:0)

我试图重写查询。试试让我知道。我还没有测试过它。

SELECT  
  t1.table1Id, 
  GROUP_CONCAT(DISTINCT table2.table2_id) AS table2Ids,
  GROUP_CONCAT(DISTINCT table3.table3_id) AS table3Ids,
  GROUP_CONCAT(DISTINCT table4.table4_id) AS table4Ids,
  GROUP_CONCAT(DISTINCT table5.table5_id) AS table5Ids,
  GROUP_CONCAT(DISTINCT table6.table6_id) AS table6Ids,
  GROUP_CONCAT(DISTINCT table7.table7_id) AS table7Ids
FROM (
    SELECT table1_id as table1Id
    FROM table1
    WHERE table1.archived = false
    GROUP BY table1.table1_id LIMIT 1000
      ) AS t1
  INNER JOIN table2 ON t1.table1Id = table2.table1_id
  INNER JOIN table3 ON t1.table1Id = table3.table1_id
  INNER JOIN table4 ON t1.table1Id = table4.table1_id
  INNER JOIN table5 ON t1.table1Id = table5.table1_id
  INNER JOIN table6 ON t1.table1Id = table6.table1_id
  INNER JOIN table7 ON t1.table1Id = table7.table1_id

答案 1 :(得分:0)

说明table2table1Id上没有索引。

尝试:

create index table2_to_table1_id_idx on table2(table1Id);

这可能会提高性能,但要最大限度地提高此特定查询的性能,请创建覆盖索引:

 create index table2_table1_id_idx on table2(table1Id, table2Id);
 create index table3_table1_id_idx on table3(table1Id, table3Id);
 create index table4_table1_id_idx on table4(table1Id, table4Id);
 create index table5_table1_id_idx on table5(table1Id, table5Id);
 create index table6_table1_id_idx on table6(table1Id, table6Id);
 create index table7_table1_id_idx on table7(table1Id, table7Id);

拥有这些索引意味着您可以避免完全访问基表。