The short answer
Look at the process list, turn on the slow query log, run EXPLAIN on the worst queries, add the right index, and check connection and memory settings.
What is running now?
sudo mysql -e "SHOW FULL PROCESSLIST;"Look for queries that run for many seconds, many identical queries, or lots of connections in the Sleep state.
Record the slow ones
sudo mysql -e "SET GLOBAL slow_query_log=1; SET GLOBAL long_query_time=1;"The log file name is in SHOW VARIABLES LIKE 'slow_query_log_file'. After a day, summarise it with mysqldumpslow -s t -t 10 file. Turn it off again when done, or set it in the config.
Explain a bad query
EXPLAIN SELECT ... ;If type is ALL on a big table, the database reads every row. An index on the column in the WHERE or JOIN usually fixes it. Test index changes on a copy first.
Check the settings
innodb_buffer_pool_size: for a dedicated database server it is often set to a large share of RAM. Too small means constant disk reads, too large means swapping.max_connections: if it is hit, apps fail. More connections need more memory.- Run a tool such as
mysqltunerfor hints, and treat them as hints.
WordPress and similar apps
Check the wp_options table for a huge autoload size and plugins that run heavy queries on every page. A page cache and an object cache take much of the load off the database.
Disk matters
A slow disk makes every query slow. Check disk wait with iostat -x 2 and free space with df -h.
Questions people ask
Should I restart MySQL when it is slow?
It clears the symptom for a while, and loses the evidence. Look at the process list and the slow log first.
Is more RAM the answer?
Sometimes. First find out if a few queries cause the load. One missing index can cost more than any extra RAM.
Can I tune it without downtime?
Many settings can be changed live with SET GLOBAL, then saved in the config. Take a backup first.