Slow query at one DB, but fast at his copy Slow query at one DB, but fast at his copy database database

Slow query at one DB, but fast at his copy


I have 3 solutions for you:

1) check & optimize & repair the table using this command:

mysqlcheck -u root -p --auto-repair --check --optimize --all-databases

2) if problem not solved check your query execution plan using monitoring apps like Newrelic

3) Sometimes it's not about specific table it's about database corruption, try to drop and create a new one with old data. How to recover/recreate mysql's default 'mysql' database


Have you tried to rebuild indexes on live DB?

In MySQL this could be done with REPAIR <table> QUICK command (quick repair - means only repair indexes - rebuild them).

(Please backup DB before applying... There is almost no chance to broke DB by this command, but...)


Some additional explanation:

Mysql mostly using self-balancing B+ trees for indexes, they are trying to be as most flat as possible to provide good search times.

But in some cases (often it is related to incremental data insertions into DATETIME column e.g. createdAt with index) MySQL B+ tree implementation unable to handle such type of load. Generated tree delivers performance which constantly decrease over a time(days, weeks). Such indexes must be rebuild...

If this approach will help you - rebuild indexes once per x days (Once per week worked for me last time)


There can be a couple of issue, One is rebuilding indexes, which is addressed and do take backup before that,

The other is the server you are running, I mean Apache or NGINX.

See whats running for apache

ps auxf

Monitor apache by turning on server-status

apachectl fullstatus

A good read here on Apache performance