patternsqlMinor
why in explain in mysql row count is very high
Viewed 0 times
whyexplainhighmysqlverycountrow
Problem
i am using mysql, here is query-
why in explain row count is too much but in actual it is less.
mysql> select count(*) from payments as Payment WHERE `Payment`.`fordate` BETWEEN '2015-11-28 00:00:00' AND '2015-12-31 23:59:59';
+----------+
| count(*) |
+----------+
| 187216 |
+----------+
1 row in set (0.16 sec)
mysql> explain select count(*) from payments as Payment WHERE `Payment`.`fordate` BETWEEN '2015-11-28 00:00:00' AND '2015-12-31 23:59:59';
+----+-------------+---------+-------+-----------------+-----------------+---------+------+--------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+-------+-----------------+-----------------+---------+------+--------+--------------------------+
| 1 | SIMPLE | Payment | range | pafordate_index | pafordate_index | 5 | NULL | 379894 | Using where; Using index |
+----+-------------+---------+-------+-----------------+-----------------+---------+------+--------+--------------------------+
1 row in set (0.04 sec)why in explain row count is too much but in actual it is less.
Solution
Is the table InnoDB? For that the index statistics are gathered by "index dives" and are by definition imprecise.
The important thing is that if it keeps numbers in the same order as reality (which, being a double in your case, it is), it works well enough for most cases.
Keeping exact numbers is too cumbersome and not even really possible in transactional environment.
EXPLAIN is not actually executing the query, but uses the estimates to plan it so it shows those imprecise numbers.The important thing is that if it keeps numbers in the same order as reality (which, being a double in your case, it is), it works well enough for most cases.
Keeping exact numbers is too cumbersome and not even really possible in transactional environment.
Context
StackExchange Database Administrators Q#123430, answer score: 3
Revisions (0)
No revisions yet.