我的简单查询花了太长时间才返回结果。 这是我的疑问:
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 |
+----+-------+------------+------------+-----------------+-------------+----------------+-------------+
使用explain返回以下内容:
+----+--------------------+--------+------------+------+---------------+------+---------+------+-------+----------+----------------------------------------------+
| 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 | | |
+--------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
答案 0 :(得分:2)
您的解释显示表格中可能没有索引。
ALTER TABLE f_logs ADD INDEX (id), ADD INDEX (f_id);