Home > Database > Mysql Tutorial > Why is My MySQL Query So Slow, Even with an Index?

Why is My MySQL Query So Slow, Even with an Index?

Mary-Kate Olsen
Release: 2024-11-19 14:59:02
Original
350 people have browsed it

Why is My MySQL Query So Slow, Even with an Index?

MySQL Statement Execution Time Excessive

While attempting to execute queries on a large database containing approximately 1 million records, prolonged execution times were encountered. Specifically, a query fetching records from the "ratings" table was particularly slow:

select * from `ratings` order by id limit 499500, 500
Copy after login

Despite employing an index on the "id" column, the problem persisted. However, reducing the table size to 10,000 records resulted in significantly faster query times.

Explanation:

Upon examining the query plan using the "EXPLAIN" statement, it was revealed that the original query was using a full table scan, leading to inefficient execution. In contrast, when a "where" clause was added, a more efficient range index scan was utilized, dramatically improving performance.

select * from `ratings` where id>=499501 limit 500
Copy after login

Considerations:

Using a "where" clause to filter the results can significantly improve query performance by utilizing appropriate indexes.

It is essential to ascertain that no deadlocks are occurring within the database, which can also lead to sluggish query execution.

The above is the detailed content of Why is My MySQL Query So Slow, Even with an Index?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template