我有一个相对复杂的查询,这里是小提琴:http ://sqlfiddle.com/#!2/65c66/12/0
SELECT p.title AS title_1,
p2.title AS title_2,
COUNT(DISTINCT s.signature_id) AS num_signers,
group_concat(DISTINCT s.signature_id separator ' ') AS signers
FROM wtp_data_petitions p
JOIN wtp_data_petitions p2 ON (p.serial > p2.serial)
JOIN wtp_data_signatures s
GROUP BY s.signature_id
HAVING sum(s.petition_id=p.id)
AND sum(s.petition_id=p2.id);
这是解释(显示我在真实数据集中的行数,而不是 sqlfiddle):
+----+-------------+-------+-------+---------------+--------------+---------+------+----------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+--------------+---------+------+----------+---------------------------------+
| 1 | SIMPLE | p | ALL | PRIMARY | NULL | NULL | NULL | 1727 | Using temporary; Using filesort |
| 1 | SIMPLE | p2 | ALL | PRIMARY | NULL | NULL | NULL | 1727 | Using where; Using join buffer |
| 1 | SIMPLE | s | index | NULL | signature_id | 105 | NULL | 12943894 | Using index; Using join buffer |
+----+-------------+-------+-------+---------------+--------------+---------+------+----------+---------------------------------+
在这一点上,查询使用了太多的磁盘空间和文件排序,我还没有看到它在出错之前成功完成。我可以执行任何优化以更快或更有效地进行吗?
谢谢!