Bug #104501 | select count not using secondary index | ||
---|---|---|---|
Submitted: | 2 Aug 2021 6:53 | Modified: | 2 Aug 2021 13:09 |
Reporter: | Yue Mao | Email Updates: | |
Status: | Not a Bug | Impact on me: | |
Category: | MySQL Server: InnoDB storage engine | Severity: | S5 (Performance) |
Version: | 8.0.23 | OS: | CentOS (8) |
Assigned to: | CPU Architecture: | x86 (Intel(R) Xeon(R) CPU E5-26xx v4) | |
Tags: | count, INDEX |
[2 Aug 2021 6:53]
Yue Mao
[2 Aug 2021 11:18]
Yue Mao
OPTIMIZER_TRACE shows this query is not using index. select count(1) from history_price | { "steps": [ { "join_preparation": { "select#": 1, "steps": [ { "expanded_query": "/* select#1 */ select count(1) AS `count(1)` from `history_price`" } ] } }, { "join_optimization": { "select#": 1, "steps": [ { "table_dependencies": [ { "table": "`history_price`", "row_may_be_null": false, "map_bit": 0, "depends_on_map_bits": [ ] } ] }, { "rows_estimation": [ { "table": "`history_price`", "table_scan": { "rows": 22245300, "cost": 109774 } } ] }, { "considered_execution_plans": [ { "plan_prefix": [ ], "table": "`history_price`", "best_access_path": { "considered_access_paths": [ { "rows_to_scan": 22245300, "access_type": "scan", "resulting_rows": 2.22e7, "cost": 2.33e6, "chosen": true } ] }, "condition_filtering_pct": 100, "rows_for_plan": 2.22e7, "cost_for_plan": 2.33e6, "chosen": true } ] }, { "attaching_conditions_to_tables": { "original_condition": null, "attached_conditions_computation": [ ], "attached_conditions_summary": [ { "table": "`history_price`", "attached": null } ] } }, { "optimizing_distinct_group_by_order_by": { } }, { "finalizing_table_conditions": [ ] }, { "refine_plan": [ { "table": "`history_price`" } ] }, { "considering_tmp_tables": [ ] } ] } }, { "join_execution": { "select#": 1, "steps": [ ] } } ] }
[2 Aug 2021 13:09]
MySQL Verification Team
Hi Mr. Mao, Thank you very much for your bug report. However , this is not a bug. Please, let us explain why it is not a bug. InnoDB is a MVCC storage engine, which means that each transaction sees only changes within its scope. Hence a row count, that is visible to a transaction, can and does differ between sessions. This occurs in auto_commit mode as well, since it has in it inherent BEGIN / COMMIT statements. Therefore, the only way for a transaction to know EXACTLY how many rows are there is to count all rows. When you count all rows, you can't use indices, since sequential counting is much faster. If you need an approximate number of rows, you can query I_S tables, as it is explained in our Reference Manual. Not a bug.