我想知道是否有什么我可以做的来加快这个查询。
该机器是具有 5900RPM HD 的 i7。有什么我可以做的吗?碰撞 sha1 确实有一个索引,其余的连接都在主键上
mysql> select count(*) from (
-> select f.id, d1.name as dir, n1.name as name, c.sha1, size from file f
-> join (select sha1 from collision group by sha1 having count(*)>1) msha on 1=1
-> join collision c on c.id = f.id and c.sha1 = msha.sha1
-> join NameList n1 on n1.id=f.name
-> join directory d1 on d1.id=f.dir
-> ) z;
+----------+
| count(*) |
+----------+
| 66469 |
+----------+
1 row in set (4 min 16.80 sec)
mysql> select count(*) from Collision;
+----------+
| count(*) |
+----------+
| 205713 |
+----------+
1 row in set (0.07 sec)
mysql> select count(*) from (select sha1 from collision group by sha1 having count(*)>1) z;
+----------+
| count(*) |
+----------+
| 29915 |
+----------+
1 row in set (1.98 sec)
mysql> explain select count(*) from (
-> select f.id, d1.name as dir, n1.name as name, c.sha1, size from file f
-> join (select sha1 from collision group by sha1 having count(*)>1) msha on 1=1
-> join collision c on c.id = f.id and c.sha1 = msha.sha1
-> join NameList n1 on n1.id=f.name
-> join directory d1 on d1.id=f.dir
-> ) z;
+----+-------------+------------+--------+------------------+---------+---------+---------------+--------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+--------+------------------+---------+---------+---------------+--------+---------------------------------+
| 1 | PRIMARY | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Select tables optimized away |
| 2 | DERIVED | <derived3> | ALL | NULL | NULL | NULL | NULL | 29915 | |
| 2 | DERIVED | c | ALL | PRIMARY | NULL | NULL | NULL | 265854 | Using where; Using join buffer |
| 2 | DERIVED | f | eq_ref | PRIMARY,dir,name | PRIMARY | 8 | filedb.c.id | 1 | |
| 2 | DERIVED | n1 | eq_ref | PRIMARY | PRIMARY | 8 | filedb.f.name | 1 | |
| 2 | DERIVED | d1 | eq_ref | PRIMARY | PRIMARY | 8 | filedb.f.dir | 1 | |
| 3 | DERIVED | collision | ALL | NULL | NULL | NULL | NULL | 265854 | Using temporary; Using filesort |
+----+-------------+------------+--------+------------------+---------+---------+---------------+--------+---------------------------------+
7 rows in set (4 min 11.97 sec)