2017-05-04 92 views
0

我的简单查询需要很长时间才能返回结果。 这里是我的查询:MySQL简单的查询需要很长时间才能返回结果

select count(*) from f_logs as a 
where a.id = (
    select max(id) 
    from f_logs 
    where f_id = a.f_id 
    group by f_id 
) and log_status = 'In storage'; 

这是我的表我的样本数据:

+----+-------+------------+------------+-----------------+-------------+----------------+-------------+ 
| id | f_id | log_date | log_action | log_destination | log_remarks | log_status  | log_account | 
+----+-------+------------+------------+-----------------+-------------+----------------+-------------+ 
| 1 | 1-EC | 2017-05-03 | Receive | Records Unit |    | In storage  | Records  | 
| 2 | 2-EC | 2017-05-03 | Receive | Records Unit |    | In storage  | Records  | 
| 3 | 3-EC | 2017-05-03 | Receive | Records Unit |    | In storage  | Records  | 
| 4 | 4-EC | 2017-05-03 | Receive | Records Unit |    | In storage  | Records  | 
| 5 | 5-EC | 2017-05-03 | Receive | Records Unit |    | In storage  | Records  | 
| 6 | 1-EC | 2017-05-03 | Released | Treasury  |    | Not in Storage | Person A | 
| 7 | 7-EC | 2017-05-03 | Receive | Records Unit |    | In storage  | Records  | 
| 8 | 8-EC | 2017-05-03 | Receive | Records Unit |    | In storage  | Records  | 
| 9 | 2-EC | 2017-05-03 | Released | Registrar  |    | Not in Storage | Person A | 
| 10 | 10-EC | 2017-05-03 | Receive | Records Unit |    | In storage  | Records  | 
| 11 | 11-EC | 2017-05-03 | Receive | Records Unit |    | In storage  | Records  | 
| 12 | 12-EC | 2017-05-03 | Receive | Records Unit |    | In storage  | Records  | 
| 13 | 3-EC | 2017-05-03 | Released | Registrar  |    | Not in Storage | Person B | 
| 14 | 14-EC | 2017-05-03 | Receive | Records Unit |    | In storage  | Records  | 
| 15 | 15-EC | 2017-05-03 | Receive | Records Unit |    | In storage  | Records  | 
| 16 | 16-EC | 2017-05-03 | Receive | Records Unit |    | In storage  | Records  | 
| 17 | 17-EC | 2017-05-03 | Receive | Records Unit |    | In storage  | Records  | 
| 18 | 18-EC | 2017-05-03 | Receive | Records Unit |    | In storage  | Records  | 
| 19 | 19-EC | 2017-05-03 | Receive | Records Unit |    | In storage  | Records  | 
| 20 | 1-EC | 2017-05-03 | Receive | Records Unit |    | In storage  | Records  | 
+----+-------+------------+------------+-----------------+-------------+----------------+-------------+ 


使用说明返回如下:

+----+--------------------+--------+------------+------+---------------+------+---------+------+-------+----------+----------------------------------------------+ 
| id | select_type  | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra          | 
+----+--------------------+--------+------------+------+---------------+------+---------+------+-------+----------+----------------------------------------------+ 
| 1 | PRIMARY   | a  | NULL  | ALL | NULL   | NULL | NULL | NULL | 46439 | 10.00 | Using where         | 
| 2 | DEPENDENT SUBQUERY | f_logs | NULL  | ALL | NULL   | NULL | NULL | NULL | 46439 | 10.00 | Using where; Using temporary; Using filesort | 
+----+--------------------+--------+------------+------+---------------+------+---------+------+-------+----------+----------------------------------------------+ 
2 rows in set, 2 warnings (0.00 sec) 

Note (Code 1276): Field or reference 'gsis_new.a.f_id' of SELECT #2 was resolved in SELECT #1 
Note (Code 1003): 
/* select#1 */ 
select count(0) AS count(*) 
from gsis_new.f_logs a 
where ((gsis_new.a.id = (

    /* select#2 */ 
    select max(gsis_new.f_logs.id) 
    from gsis_new.f_logs 
    where (gsis_new.f_logs.f_id = gsis_new.a.f_id) 
    group by gsis_new.f_logs.f_id) 

) and (gsis_new.a.log_status = 'In storage')) 


这里是我的想要发生: 我有一个名为f_logs的表,它用于日志记录的文件。 我想返回表中记录的总数。

从我的样本表数据。它应该返回“14”,因为2-EC和3-EC被标记为不从第9行和13
虽然1-EC已行再次标记为在存储器20



存储注意:当表未被填充时,我的mySql查询工作正常。
有没有机会优化我的查询?



附加信息: 这里是我的f_logs表

+--------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | 
+--------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 
| f_logs |   0 | PRIMARY  |   1 | id   | A   |  46439 |  NULL | NULL |  | BTREE  |   |    | 
| f_logs |   1 | acountIndex |   1 | log_account | A   |   1 |  NULL | NULL | YES | BTREE  |   |    | 
+--------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 

回答

2

你的解释表明有可能是在表上没有索引的索引。

ALTER TABLE f_logs ADD INDEX (id), ADD INDEX (f_id);

+0

我已经在我的f_logs表中有两个索引。 – MjdeLima

+0

现在我添加了一个索引f_id – MjdeLima

+1

感谢队友,在f_id上添加一个索引为我做了诀窍。 – MjdeLima

相关问题