2017-05-04 89 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

相關問題