gpt4 book ai didi

MySQL 简单查询需要很长时间才能返回结果

转载 作者:行者123 更新时间:2023-11-29 07:00:07 27 4
gpt4 key购买 nike

我的简单查询花了太长时间才返回结果。这是我的查询:

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 的表,它用于记录文件。我想返回表中的记录总数。

来 self 的示例表数据。它应该返回“14”,因为第 9 行和第 13 行中的 2-EC 和 3-EC 被标记为不在存储中。
而 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 | | |
+--------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

最佳答案

您的解释表明表上可能没有索引。

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

关于MySQL 简单查询需要很长时间才能返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43774030/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com