If your MySQL or MariaDB install has been a bit sluggish, chances are your databases or tables are defragmented. This happens over time and is completely normal but causes MySQL or MariaDB to become slow as the software needs to located the information on the disk that is no longer close together. Just like a computer drive that has data fragmented over time, the same thing happens to databases. To fix this, we optimize and defragment the tables. This will move all the data so that it is closer together on the hard drives resulting in faster read and write times.
As root, run the following command to automatically optimize, repair, and defragment your MySQL or MariaDB tables and databases:
mysqlcheck -u <username> -p –auto-repair –optimize –all-databases
Be sure to replace <username> with the root username you installed MySQL as. You will be prompted for the password to enter securely so it is not available when running the history program. This command will work with any Linux system running either MySQL or MariaDB.