mysql enable slow query logging/Query Cache Solution - find/track cause of slow mysqld/high IO/CPU usage

If you have a webserver and find you have high IO/lagginess MySQL is one of the first things to check.  It turns out MySQL was my problem and it was creating a high load on my server, especially for IO.

How to Enable MySQL Slow Query Logging To Find Slow Performance/Queries

vi /etc/my.cnf

Add this anywhere under [mysqld]

#slow queries
log-slow-queries = /var/log/mysql/mysqlslowqueries.log
long_query_time = 1

You can change the log path to whatever you like.  You can also chanege the threshold for slow queries, I have it sent on 1 second.  Once you do this and restart mysqld you'll start seeing the user, database and exact query that is slowing things down.

I was finding queries that were taking as long as 42 seconds long!  That was 42 seconds of slow IO performance that was wreaking havoc on my webserver.

The Quick Fix for slow MySQL performance Queries?

I thought about creating a MySQL cluster but it requires several servers and takes some time to setup and test.  I also thought about a hardware upgrade, getting faster RPM HDD's and maybe going SSD, but this takes time and money even if I wanted to do it now.

What can be done immediately?  The answer for me and a lot of people will be MySQL Query Caching.

Enable MySQL Query Caching To Improve MySQL performance

vi /etc/my.cnf

Under [mysqld] add:

#increase performance with caching
#128MB of memory cache in KB
query_cache_size=131072
query_cache_type=1
#maximum size of individual query that can be cached in KB (about 1 gig although note the cache total size is restricted to 128MB now)
query_cache_limit=1048576

After doing this my performance issues were solved.  A quick note about Query Caching is that it only works for the EXACT same SELECT statements when data has not changed, if data changes the cache becomes irrelevant until the next request. Also note that if the SELECT statement expliclity specifies NOCACHE (can't remember the syntax) then the cache will not be used.

For many database driven sites this is still good and at least it can only improve performance.  I've found it to make a huge difference.

 


Tags:

mysql, enable, query, logging, cache, mysqld, io, cpu, usageif, webserver, lagginess, creating, server, queries, vi, etc, cnf, var, mysqlslowqueries, long_query_time, chanege, threshold, restart, ll, user, database, slowing, wreaking, havoc, cluster, requires, servers, hardware, upgrade, rpm, hdd, ssd, caching, improve, mb, kb, query_cache_size, query_cache_type, maximum, individual, cached, restricted, query_cache_limit, select, statements, irrelevant, expliclity, specifies, nocache, syntax, sites, ve,

Latest Articles

  • How to install Windows or other OS and then bring to another computer by using a physical drive and Virtual Machine with QEMU
  • PXE-E23 Error BOOTx64.EFI GRUB booting is 0 bytes tftp pxe dhcp solution NBP filesize is 0 Bytes
  • vagrant install on Debian Mint Ubuntu Linux RHEL Quick Setup Guide Tutorial
  • RHEL 8 CentOS 8, Alma Linux 8, Rocky Linux 8 System Not Booting with RAID or on other servers/computers Solution for dracut and initramfs missing kernel modules
  • How to Upgrade to Debian 11 from Version 8,9,10
  • Ubuntu Linux Mint Debian Redhat Cannot View Files on Android iPhone USB File Transfer Not Working Solution
  • Virtualbox Best Networking Mode In Lab/Work Environment without using NAT Network or Bridged
  • debootstrap how to install Ubuntu, Mint, Debian install
  • Linux grub not using UUID for the root device instead it uses /dev/sda1 or other device name solution
  • How To Restore Partition Table on Running Linux Mint Ubuntu Debian Machine
  • Debian Ubuntu apt install stop daemon questions/accept the default action without prompting
  • iptables NAT how to enable PPTP in newer Debian/Ubuntu/Mint Kernels Linux
  • Grandstream Phone Vulnerability Security Issue Remote Backdoor Connection to 207.246.119.209:3478
  • Linux How to Check Which NIC is Onboard eth0 or eth1 Ubuntu Centos Debian Mint
  • VboxManage VirtualBox NAT Network Issues Managment Troubleshooting
  • Dell PowerEdge Server iDRAC Remote KVM/IP Default Username, Password Reset and Login Information Solution
  • Nvidia Tesla GPUs K40/K80/M40/P40/P100/V100 at home/desktop hacking, cooling, powering, cable solutions Tutorial AIO Solutions
  • Stop ls in Linux Debian Mint CentOS Ubuntu from applying quotes around filenames and directory names
  • Thunderbird Attachment Download Error Corrupt Wrong filesize of 29 or 27 bytes Solution
  • Generic IP Camera LAN Default IP Settings DVR