在我添加第二个左连接之前,下面的select工作得很好。现在显示表格的时间是18秒。
当我使用group\u concat和group by时,它可以很好地处理这两个连接。创建视图没有帮助。表1和表2之间是一对多的关系,因此我需要使用group\u concat在每一行中都有唯一的ticketno。
是否可以编写此查询以更快地执行它?谢谢。
SELECT table1.TICKETNO,
table2.STARTTIMESTAMP,
group_concat(table2.ACTION),
table1.COMPLETION,
table3.MEASURE
FROM table1
LEFT JOIN table2 ON (table1.TICKETNO=table2.TICKETNO)
LEFT JOIN table3 ON (table1.ERRORCODE=table3.ERRCODE)
GROUP BY table1.TICKETNO
索引:
+----------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+----------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| table1 | 0 | PRIMARY | 1 | TICKETNO | A | 21894 | NULL | NULL | | BTREE | | |
+----------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
+-------------------+------------+----------+--------------+----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------------------+------------+----------+--------------+----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| table2 | 0 | PRIMARY | 1 | TICKETNO | A | 76110 | NULL | NULL | | BTREE | | |
| table2 | 0 | PRIMARY | 2 | STARTTIMESTAMP | A | 76110 | NULL | NULL | | BTREE | | |
+-------------------+------------+----------+--------------+----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
+----------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+----------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| table3 | 0 | PRIMARY | 1 | SPRAS | A | 6 | NULL | NULL | | BTREE | | |
| table3 | 0 | PRIMARY | 2 | ERRCODE | A | 1138 | NULL | NULL | | BTREE | | |
+----------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
以下是对我的问题的解释:
+------+-------------+-------------------+------+---------------+---------+---------+------------------------------------+-------+-------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------------------+------+---------------+---------+---------+------------------------------------+-------+-------------------------------------------------+
| 1 | SIMPLE | table1 | ALL | NULL | NULL | NULL | NULL | 21894 | Using temporary; Using filesort |
| 1 | SIMPLE | table2 | ref | PRIMARY | PRIMARY | 8 | mydb.table1.TICKETNO | 1 | |
| 1 | SIMPLE | table3 | ALL | NULL | NULL | NULL | NULL | 1138 | Using where; Using join buffer (flat, BNL join) |
+------+-------------+-------------------+------+---------------+---------+---------+------------------------------------+-------+-------------------------------------------------+
1条答案
按热度按时间t3irkdon1#
尽管它很慢,而且您还没有提供其他细节,但我想问您是否有更好的索引,包括: