怎么来,行数与SELECT COUNT(*)FROM table [duplicate]不一样

时间:2022-12-05 12:58:46

This question already has an answer here:

这个问题在这里已有答案:

I don't have a lot of experience with DB, but this thing is little confusing: First I did:

我没有很多DB经验,但这件事有点令人困惑:首先我做了:

mysql> EXPLAIN SELECT COUNT(*) FROM tweets;
+----+-------------+--------+-------+---------------+---------+---------+------+----------+-------------+
| id | select_type | table  | type  | possible_keys | key     | key_len | ref  | rows     | Extra       |
+----+-------------+--------+-------+---------------+---------+---------+------+----------+-------------+
|  1 | SIMPLE      | tweets | index | NULL          | user_id | 4       | NULL | 18683420 | Using index |
+----+-------------+--------+-------+---------------+---------+---------+------+----------+-------------+
1 row in set (0.03 sec)

Than I tried this:

比我试过这个:

mysql> SELECT COUNT(*) FROM tweets;
+----------+
| COUNT(*) |
+----------+
| 15254792 |
+----------+
1 row in set (9.60 sec)

But the number of rows different from number that I got from last query. Can you please explain why is that? Is it a bug or it is an expected result?

但行数不同于我从上次查询得到的数字。你能解释一下为什么吗?这是一个错误还是预期的结果?

1 个解决方案

#1


2  

Expected.

EXPLAIN uses statistics about the index to work out a query plan, not the actual index (or data).

EXPLAIN使用有关索引的统计信息来计算查询计划,而不是实际的索引(或数据)。

#1


2  

Expected.

EXPLAIN uses statistics about the index to work out a query plan, not the actual index (or data).

EXPLAIN使用有关索引的统计信息来计算查询计划,而不是实际的索引(或数据)。