暂无图片
请问带时间字段的慢sql怎么优化
我来答
分享
czxin788
2020-05-12
请问带时间字段的慢sql怎么优化

本帖最后由 czxin788 于 2020-5-12 15:03 编辑


explain SELECT
    -> id,
    -> customer_id,
    -> send_customer_id,
    -> send_customer_name,
    -> send_site_id,
    -> message_type,
    -> op_type,
    -> ref_type,
    -> ref_id,
    -> ref_review_id,
    -> message_name,
    -> message_body,
    -> create_time,
    -> read_time,
    -> is_read,
    -> read_site_id,
    -> is_remind,
    -> sort_id,
    -> is_valid,
    -> resource_type,
    -> ios_send_time,
    -> android_send_time,
    -> wx_send_time,
    -> notify_config_id,
    -> message_src_time,
    -> is_permit,
    -> page_storage_path,
    -> web_storage_path,
    -> app_storage_parh
    ->  FROM customer_message
    ->  WHERE 1=1
    -> and op_type in
    ->  (  
    -> '121'
    ->  ,
    -> '123'
    ->  )
    -> and create_time >= '2018-05-17 15:35:46.0'
    ->  limit 0, 9223372036854775807;
+----+-------------+------------------+------------+------+----------------+------+---------+------+---------+----------+-------------+
| id | select_type | table            | partitions | type | possible_keys  | key  | key_len | ref  | rows    | filtered | Extra       |
+----+-------------+------------------+------------+------+----------------+------+---------+------+---------+----------+-------------+
|  1 | SIMPLE      | customer_message | NULL       | ALL  | idx_createtime | NULL | NULL    | NULL | 1536673 |    10.00 | Using where |
+----+-------------+------------------+------------+------+----------------+------+---------+------+---------+----------+-------------+
1 row in set, 1 warning (0.00 sec)



show index from customer_message;
+------------------+------------+---------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table            | Non_unique | Key_name                  | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+------------------+------------+---------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| customer_message |          0 | PRIMARY                   |            1 | id          | A         |     1475167 |     NULL | NULL   |      | BTREE      |         |               |
| customer_message |          1 | idx_createtime            |            1 | create_time | A         |      714707 |     NULL | NULL   | YES  | BTREE      |         |               |
| customer_message |          1 | idx_customer_ref_id_multi |            1 | customer_id | A         |      125866 |     NULL | NULL   | YES  | BTREE      |         |               |
| customer_message |          1 | idx_customer_ref_id_multi |            2 | ref_id      | A         |      752687 |     NULL | NULL   | YES  | BTREE      |         |               |
+------------------+------------+---------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
4 rows in set (0.00 sec)





select count(distinct op_type) from customer_message;
+-------------------------+
| count(distinct op_type) |
+-------------------------+
|                      76 |
+-------------------------+
1 row in set (0.88 sec)


请问,上面的慢sql应该怎么优化好呢

我来答
添加附件
收藏
分享
问题补充
2条回答
默认
最新
文成

看起来这个表数据量比较大,从执行计划来看走的是全表扫描,因为没有合适的索引
可以考虑对这个表进行按照时间进行分区
并对 op_type 增加联合索引
同时需要考虑业务的合理性 是否需要返回这么多字段和这么多行的数据?

暂无图片 评论
暂无图片 有用 0
黄伟波

看一下customer_message表结构

暂无图片 评论
暂无图片 有用 0
回答交流
提交
问题信息
请登录之后查看
邀请回答
暂无人订阅该标签,敬请期待~~
暂无图片墨值悬赏