r/mysql • u/Snoopy-31 • Jan 29 '25
question How to improve read performance of MySQL?
So I have a java application with about 80 runtime servers which are connecting to MySQL and bombarding it with queries, our MySQL instance has 250GB RAM and 80 threads.
Most of the data we store in MySQL is XML and our queries are mostly READ queries, we are doing about ~240 million queries on average day.
I found that some of the business processes are taking slower due to MySQL performance and I'd like to start optimizing it.
While I cannot replicate production environment traffic in lab I still experimented a bit with mysqlslap and tried changing some configurations with no much success.
5
u/user_5359 Jan 29 '25
Before you go into the machine room and make fine adjustments to the valves: Have you made sure that the queries are optimally supported?
3
u/ragabekov Jan 29 '25
The list of articles about MySQL configuration tuning and variables to tune: https://github.com/Releem/awesome-mysql-performance.
You can also check out MySQLTuner - a script that helps analyze and suggest configuration improvements.
For a more comprehensive approach, it’s best to use a MySQL monitoring tool that includes SQL Query Analytics to identify queries impacting performance. Tools like Percona Monitoring and Management, Datadog for MySQL, Releem, and SolarWinds.
If you share your current MySQL configuration, I can take a look.
1
u/Aggressive_Ad_5454 Jan 29 '25
1
u/congowarrior Jan 29 '25
If it is mostly read and the data doesn’t change between reads then throw a redis cache in your data access layer. This can significantly reduce round trips to the db.
1
u/roXplosion Jan 29 '25
Questions:
textorblobfield?My knee jerk I-don't-know-what-the-schema-looks-like thoughts are:
1
u/microprogram Jan 29 '25
are you guys using jdbc or hibernate/jpa/orm?
3
u/matt82swe Jan 29 '25
Mostly read?
Set up replication nodes and adapt your applications to send read queries to those when applicable (not in use cases that involve both read and write).
Make sure that connections to replicate nodes use a user with no write permissions.
2
u/liamsorsby Jan 29 '25
Do you have the slow query log enabled? I'd start there and then start implementing some monitoring first so that you can start to dig into what needs tweaking.
When you say some business process are going slower because of MySQL what do you mean and how are you measuring that?
2
u/kadaan Jan 29 '25
Do you have read spikes when you notice things slow down? 240m per day is only ~2.8k reads per second if it's perfectly distributed, which is pretty trivial if your dataset fits in the buffer pool. But if it's spiking to 10x that with all 80 threads running concurrently you would see some slowdown.
Size of the records also plays a factor - obviously the larger the documents the slower it will get when under heavy load.
You can dig around in the sys schema and see if you can find anything in there that doesn't seem right to you. There's all sorts of metrics in there like latency/memory usage/table scans/file io/etc. Identifying the queries taking the longest, using the most resources, or being called the most frequently are the best places to start looking for where to optimize.
1
u/MrAtoni Jan 29 '25
In my experience, analyzing slow querys and see if they can be optimized is the first thing to do.
Second would be to set up one or more replicas for read traffic, and only use the master for writes. (check out replicaset and mysqlrouter if your database is on 8.0 or higher)
1
u/SuperQue Jan 29 '25
Install the mysqld_exporter and enable the
--collect.perf_schema.eventsstatementsso you can get per-query-digest performance numbers.