首页 > 解决方案 > MySQL 为什么将限制从 20 增加到 21 将执行时间从 1,4s 减少到 0,0067s

问题描述

我有一个有 230 000 行的表
查询 1 持续时间 1,389 秒

SELECT sql_no_cache `model_regulation_sections`.* 
FROM `model_regulation_sections` 
WHERE `model_regulation_sections`.`regulation_id` = 254 
ORDER BY `model_regulation_sections`.`lft` ASC 
LIMIT 20

解释
select_type = SIMPLE
table = model_regulation_sections
partitions = null`
type = index
possible_keys = document_id
key = lft
key_len = 4
ref = null
rows = 2229
filtered = 0.90
Extra = 使用 where

查询 2 持续时间 0,0067

SELECT sql_no_cache `model_regulation_sections`.* 
FROM `model_regulation_sections` 
WHERE `model_regulation_sections`.`regulation_id` = 254 
ORDER BY `model_regulation_sections`.`lft` ASC 
LIMIT 21

解释
select_type = SIMPLE
table = model_regulation_sections
partitions = null`
type = ref
possible_keys = document_id
key = lft
key_len = 4
ref = const
rows = 2251
filtered = 100
Extra = '使用索引条件;使用文件排序'

MySQL 为什么将限制从 20 增加到 21 会将执行时间从 1,4s 减少到 0,0067s?

为什么 2 小时前查询 1 的执行时间是 13.5 秒?我没有更改配置。

标签: mysql

解决方案


推荐阅读