数据库
首页 > 数据库> > MySQL解释行限制

MySQL解释行限制

作者:互联网

下面是我的查询,获得20行genre_id 1.

EXPLAIN SELECT * FROM (`content`) 
WHERE `genre_id` = '1' 
AND `category` = 1
LIMIT 20

我在genre_id 1的内容表中总共有654行,我在genre_id上​​有索引,在上面的查询中我限制结果只显示20条工作正常的记录,但解释是在行下显示654条记录,我试图在类别上添加索引但仍然是相同的结果,然后我也删除了AND category = 1但相同的行数:

id  select_type table   type    possible_keys   key key_len ref rows    Extra
1   SIMPLE  content ref genre_id    genre_id    4   const   654 Using where

HERE我找到了答案

LIMIT is not taken into account while estimating number of rows Even
if you have LIMIT which restricts how many rows will be examined MySQL
will still print full number

但也在评论中发布了另一个回复:

LIMIT is now taken into account when estimating number of rows. I’m
not sure which version addressed this, but in 5.1.30, EXPLAIN
accurately takes LIMIT into account.

我正在使用带有InnoDB的MySQL 5.5.16.因此,根据上述评论,它仍然没有考虑到.所以我的问题是mysql是否通过所有654行返回20行,即使我已设置限制?谢谢

解决方法:

Rick JamesMySQL回复

Does mysql LIMIT is taken into account when estimating number of rows in Explain?

No. (5.7 with JSON may be a different matter.)

标签:mysql,database-performance
来源: https://codeday.me/bug/20191008/1875391.html