因为行慢查询?

时间:2013-06-07 03:41:22

标签: mysql query-optimization

注意:返回0行。

我的查询是17.88秒。我不知道为什么。知道我能做些什么来加快速度吗?我特别有点困惑为什么derived2没有索引。我知道子查询返回3887行。也许它说有数(*)没有索引?其余的可能有索引。我尝试在一个新的mysqld实例上运行子查询(所以没有缓存),它花了< 1sec。我之后立即在同一个连接上运行完整查询,花了17.58秒。

我被困了,因为它必须通过115K行?我跑了select count(*) from file;并获得了2457062(大约5%的行)。读115K行是慢吗?我不认为我可以加快速度吗?

select f.id, d.name as dir, n.name as name, d.volume from file f 
join NameList n on n.id=f.name 
join directory d on d.id=f.dir 
join (
    select size 
    from file 
    where size>123 and size<456000
    group by size 
    having count(*)>1
) cs on cs.size=f.size
left join Collision c on c.id=f.id
where c.id is null

+----+-------------+------------+--------+------------------+---------+---------+----------------+--------+--------------------------------------+
| id | select_type | table      | type   | possible_keys    | key     | key_len | ref            | rows   | Extra     |
+----+-------------+------------+--------+------------------+---------+---------+----------------+--------+--------------------------------------+
|  1 | PRIMARY     | <derived2> | ALL    | NULL             | NULL    | NULL    | NULL           |   3887 |     |
|  1 | PRIMARY     | f          | ref    | dir,name,ix_size | ix_size | 8       | cs.size        |      1 |     |
|  1 | PRIMARY     | n          | eq_ref | PRIMARY          | PRIMARY | 8       | thedb.f.name   |      1 |     |
|  1 | PRIMARY     | d          | eq_ref | PRIMARY          | PRIMARY | 8       | thedb.f.dir    |      1 |     |
|  1 | PRIMARY     | c          | eq_ref | PRIMARY          | PRIMARY | 8       | thedb.f.id     |      1 | Using where; Using index; Not exists |
|  2 | DERIVED     | file       | range  | ix_size          | ix_size | 8       | NULL           | 115394 | Using where; Using index     |
+----+-------------+------------+--------+------------------+---------+---------+----------------+--------+--------------------------------------+


+--------------------------------+-----------+
| Status                         | Duration  |
+--------------------------------+-----------+
| starting                       |  0.000020 |
| Waiting for query cache lock   |  0.000007 |
| checking query cache for query |  0.000179 |
| checking permissions           |  0.000006 |
| checking permissions           |  0.000007 |
| checking permissions           |  0.000001 |
| checking permissions           |  0.000002 |
| checking permissions           |  0.000005 |
| Opening tables                 |  0.629606 |
| System lock                    |  0.000204 |
| optimizing                     |  0.000030 |
| statistics                     |  0.060076 |
| preparing                      |  0.000026 |
| executing                      |  0.000009 |
| Sorting result                 |  0.000002 |
| Sending data                   |  0.378205 |
| Waiting for query cache lock   |  0.000011 |
| Sending data                   |  0.000034 |
| init                           |  0.000032 |
| optimizing                     |  0.000024 |
| statistics                     |  0.000050 |
| preparing                      |  0.000026 |
| executing                      |  0.000002 |
| Sending data                   | 16.806435 |
| end                            |  0.000012 |
| query end                      |  0.000004 |
| closing tables                 |  0.000002 |
| removing tmp table             |  0.000013 |
| closing tables                 |  0.000021 |
| freeing items                  |  0.000021 |
| Waiting for query cache lock   |  0.000002 |
| freeing items                  |  0.000049 |
| Waiting for query cache lock   |  0.000002 |
| freeing items                  |  0.000001 |
| storing result in query cache  |  0.000003 |
| logging slow query             |  0.000001 |
| logging slow query             |  0.000002 |
| cleaning up                    |  0.000008 |
+--------------------------------+-----------+
38 rows in set (0.00 sec)

0 个答案:

没有答案