如何改进长时间“发送数据”的查询

bybem2ql  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(291)

有问题的查询是

select count(*)
from t_fault tf
where err_status = 1 and
      report_type = 2 and
      solve_status = 2 and
      fault_code = 8 and
      tf.record_time between '2018-01-12 00:00:00' and '2018-01-18 23:59:59';

此查询的配置文件数据为

+----------------------+----------+
| Status               | Duration |
+----------------------+----------+
| starting             | 0.000070 |
| checking permissions | 0.000005 |
| Opening tables       | 0.000014 |
| init                 | 0.000021 |
| System lock          | 0.000006 |
| optimizing           | 0.000011 |
| statistics           | 0.000080 |
| preparing            | 0.000017 |
| executing            | 0.000002 |
| Sending data         | 0.500267 |
| end                  | 0.000011 |
| query end            | 0.000006 |
| closing tables       | 0.000011 |
| freeing items        | 0.000086 |
| cleaning up          | 0.000012 |
+----------------------+----------+

“发送数据”大约执行0.5秒,我认为这是低性能,我不能做得更好。
可能索引不正确。
下表中包含主键和索引的t\ U故障表的ddl

CREATE TABLE `t_fault` (
  `id` varchar(36) NOT NULL,
  `pile_id` varchar(19) DEFAULT NULL,
  `report_type` int(2) DEFAULT '0',
  `fault_code` int(2) DEFAULT NULL,
  `err_code` int(2) DEFAULT NULL,
  `err_status` int(2) DEFAULT NULL,
  `solve_status` int(2) DEFAULT NULL,
  `create_time` datetime DEFAULT NULL,
  `record_time` datetime DEFAULT NULL,
  `fault_type` int(8) DEFAULT NULL,
  `update_time` datetime DEFAULT NULL,
  `solve_time` datetime DEFAULT NULL,
  `operator_id` varchar(19) DEFAULT NULL,
  `inter_no` smallint(6) DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `i_fault_common` (`err_status`,`report_type`,`solve_status`,`fault_code`,`record_time`),
  KEY `i_fault_pile_common` (`pile_id`,`err_status`,`report_type`,`solve_status`,`fault_code`,`record_time`),
  KEY `i_fault_operator_common` (`operator_id`,`err_status`,`report_type`,`solve_status`,`fault_code`,`record_time`),
  KEY `i_fault_operator_pile_common` (`operator_id`,`pile_id`,`err_status`,`report_type`,`solve_status`,`fault_code`,`record_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

这个表包含8944637行

explain select count(*) from t_fault tf where err_status = 1 and report_type = 2 and solve_status =2 and fault_code =8 and tf.record_time between '2018-01-12 00:00:00' and '2018-01-18 23:59:59';

mysql打印此

+----+-------------+-------+-------+----------------+----------------+---------+------+---------+--------------------------+
| id | select_type | table | type  | possible_keys  | key            | key_len | ref  | rows    | Extra                    |
+----+-------------+-------+-------+----------------+----------------+---------+------+---------+--------------------------+
|  1 | SIMPLE      | tf    | range | i_fault_common | i_fault_common | 26      | NULL | 1584048 | Using where; Using index |
+----+-------------+-------+-------+----------------+----------------+---------+------+---------+--------------------------+

因此,如何通过一些技巧来加快有问题查询的“发送数据”。

bgibtngc

bgibtngc1#

这个表包含8944637行。
你的table太重了。我建议看一下范围划分。

相关问题