【问题标题】:MySQL select query taking too much timeMySQL选择查询花费太多时间
【发布时间】:2022-06-22 21:41:48
【问题描述】:

我有一个简单的 Spring JPA 选择查询,执行起来需要太多时间。

表包含 ~3400 万 条记录。

Query : select * from temp where cust_id='211313131'
taking time >1sec
SLow Query logs : Query_time: 0.990815 Lock_time: 0.000034 Rows_sent: 0 Rows_examined: 3273885 Rows_affected: 0

Table structure : 

mysql> desc temp;
+-------------+--------------+------+-----+-------------------+-----------------------------------------------+
| Field       | Type         | Null | Key | Default           | Extra                                         |
+-------------+--------------+------+-----+-------------------+-----------------------------------------------+
| id          | bigint       | NO   | PRI | NULL              | auto_increment                                |
| cust_id     | varchar(100) | YES  | MUL | NULL              |                                               |
| amount      | double(11,3) | YES  |     | NULL              |                                               |
| is_enabled  | int          | NO   | MUL | 1                 |                                               |
| created_at  | timestamp    | NO   |     | CURRENT_TIMESTAMP | DEFAULT_GENERATED                             |
| updated_at  | timestamp    | NO   | MUL | CURRENT_TIMESTAMP | DEFAULT_GENERATED on update CURRENT_TIMESTAMP |
+-------------+--------------+------+-----+-------------------+-----------------------------------------------+
6 rows in set (0.05 sec)

表索引:

mysql> show index from temp;
+-------------+------------+----------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table       | Non_unique | Key_name                   | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+-------------+------------+----------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| temp        |          0 | PRIMARY                    |            1 | id          | A         |         285 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
| temp        |          1 | idx_subscribers_cust_id    |            1 | cust_id     | A         |         281 |     NULL |   NULL | YES  | BTREE      |         |               | YES     | NULL       |
| temp        |          1 | idx_subscribers_is_enabled |            1 | is_enabled  | A         |           1 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
| temp        |          1 | idx_subscribers_updated_at |            1 | updated_at  | A         |         264 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
+-------------+------------+----------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
4 rows in set (0.21 sec)

我已经检查了解释,解释结果显示它应该立即执行。

mysql> explain select * from temp where cust_id="31231234343";
+----+-------------+-------------+------------+------+-------------------------+-------------------------+---------+-------+------+----------+-------+
| id | select_type | table       | partitions | type | possible_keys           | key                     | key_len | ref   | rows | filtered | Extra |
+----+-------------+-------------+------------+------+-------------------------+-------------------------+---------+-------+------+----------+-------+
|  1 | SIMPLE      |    temp     | NULL       | ref  | idx_subscribers_cust_id | idx_subscribers_cust_id | 403     | const |    1 |   100.00 | NULL  |
+----+-------------+-------------+------------+------+-------------------------+-------------------------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)

任何人都可以帮我找出为什么即使该列上存在索引也需要> 1秒才能获取记录。

【问题讨论】:

  • 查询是如何构建的?

标签: mysql spring


【解决方案1】:

一个与隐式类型转换有关的怀疑。

如果查询如下所示: select * from temp where cust_id = 1, 然后mysql尝试将列转换为适合参数的类型。内部查询是这样执行的:

select * from temp where cast(cust_id as UNSIGNED) =1

我不确定 MYSQL 转换为的确切数字类型,但可以肯定的是,对于这种情况,不会使用任何键

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-08
    • 2012-09-03
    • 1970-01-01
    • 2015-11-02
    • 2018-01-10
    • 2012-06-04
    • 2019-11-14
    相关资源
    最近更新 更多