"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Why are my \"SELECT COUNT(*)\" queries on the change_event table so slow?

Why are my \"SELECT COUNT(*)\" queries on the change_event table so slow?

Published on 2024-11-18
Browse:216

Why are my \

Tackling Slow "SELECT COUNT(*)" Queries on MySQL

Your query on the change_event table, counting rows that exceed a specific change_event_id, is experiencing significant delays. But why? Let's delve into possible causes.

Unveiling InnoDB's Behavior

MySQL's InnoDB engine utilizes clustered primary keys, meaning that the primary key is stored alongside row data in data pages, rather than separate index pages. As a result, range scans, such as yours, require scanning through all potentially wide rows in data pages. This factor is exacerbated by the table's xml_diff column, a TEXT data type that further slows down processing.

Optimization Strategies

To accelerate the query, two approaches are worth considering:

  • Optimize Table: This command reorganizes data pages into sorted order, potentially improving the efficiency of range scans.
  • Create Additional Index: Establishing a non-primary index solely on the change_event_id column creates a copy of that column in index pages. This index can be scanned considerably faster than data pages. Verify the explain plan post-creation to confirm its utilization.

Additional Tip:

To enhance performance further, consider altering the change_event_id column to bigint unsigned. This step prevents negative values and can also streamline processing.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3