select count(*) from table where relation_title='xxxxxxxxx'; +----------+ | count(*) | +------6ren">
gpt4 book ai didi

mysql - 为什么 "explain"返回的行不等于 count()?

转载 作者:IT王子 更新时间:2023-10-29 00:31:06 25 4
gpt4 key购买 nike

    mysql> select count(*) from table where relation_title='xxxxxxxxx';
+----------+
| count(*) |
+----------+
| 1291958 |
+----------+

mysql> explain select * from table where relation_title='xxxxxxxxx';
+----+-------------+---------+-
| id | select_type | rows |
+----+-------------+---------+-
| 1 | SIMPLE | 1274785 |
+----+-------------+---------+-

我认为“解释 select * from table where relation_title='xxxxxxxxx';”按索引返回 relation_title='xxxxxxxxx' 的行。但它比真正的数字小。

最佳答案

它显示了为获得结果而运行了多少行。

错误数据的原因是 EXPLAIN 不准确,它根据存储的有关您的表的信息来猜测您的数据。

这是非常有用的信息,例如,在对许多表执行 JOINS 时,您希望确保您没有为您拥有的每一行的一行信息运行整个连接表。

这是对 608 行表的测试。

SELECT COUNT(id) FROM table WHERE user_id = 1

结果:

COUNT(id)
512

下面是解释

EXPLAIN SELECT COUNT(id) FROM table WHERE user_id = 1

结果:

id  rows
1 608

关于mysql - 为什么 "explain"返回的行不等于 count()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1037471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com