如何使用long"发送数据来改进查询"时间

时间:2018-01-19 04:26:49

标签: mysql

有问题的查询是

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_fault表的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

此表包含8,944,637行。当我执行以下sql

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 |
+----+-------------+-------+-------+----------------+----------------+---------+------+---------+--------------------------+

那么我怎样才能加快"发送数据"一些技巧的问题查询。

1 个答案:

答案 0 :(得分:0)

  

,此表包含8,944,637行。

你的桌子很重。我建议你看一下RANGE Partitioning