https://tidb.net/book/tidb-monthly/2022/2022-04/usercase/sql-1000
某一天早上来到公司,接到业务同学反馈,线上某个SQL之前查询速度很快,从某个时间点开始查询速度突然变慢了,希望DBA帮忙查看下。业务同学反馈的原话如下:
看到这个问题,我第一时间询问了业务对这个表的基本操作,得到的反馈如下:
首先,我们来看下这条SQL(脱敏之后):
SELECT
xxx, xxx, xxx, xxx, ....
FROM log_xxxx_2022_4
WHERE 1=1
AND
l_mid
= 'xxxxxxx-E527B8CD-84B-960'AND
l_opertime
< '2022-04-20 10:56:37'AND
l_opertime
>= '2022-03-20 10:56:37'ORDER BY
l_opertime
DESC LIMIT 0,20;
SQL的语义本身比较简单,是一个单表查询,不涉及复杂查询:
从某一张表里面,利用l_mid和l_opertime这两个字段作为过滤条件,输出表里面的其他字段,并按照l_opertime排序。
这样一条简单的SQL,如果有索引的话,应该不会出现问题才对,我们看下表结构:
show index from log_xxxx_2022_4;
+-----------------+------------+---------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+-----------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression | Clustered |
+-----------------+------------+---------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+-----------+
| log_xxxx_2022_4 | 0 | PRIMARY | 1 | l_id | A | 0 | NULL | NULL | | BTREE | | | YES | NULL | YES |
| log_xxxx_2022_4 | 1 | l_oper | 1 | l_oper | A | 0 | NULL | NULL | | BTREE | | | YES | NULL | NO |
| log_xxxx_2022_4 | 1 | l_channel | 1 | l_channel | A | 0 | NULL | NULL | | BTREE | | | YES | NULL | NO |
| log_xxxx_2022_4 | 1 | l_xxxxid | 1 | l_xxxxid | A | 0 | NULL |