How AI made my Amazon RDS for MySQL queries 23x faster
Learn how I detected MySQL slow queries and optimised their performance with EverSQL’s AI-driven optimisation engine to get a 23x performance gain.
Create an Amazon RDS for MySQL service
Customize the parameters in the Parameter group
Associate the Parameter Group to Amazon RDS for MySQL
Install the EverSQL sensor from the Amazon Serverless Application repository
Connect to the Amazon RDS for MySQL instance
Verify the presence of the slow query log
This blog showcases how, using the AI-driven insights provided by EverSQL by Aiven, I was able to identify a slow performing query and automatically receive optimisation suggestions which made it 23x faster, all for free!
A fully integrated experience is now available for PostgreSQL® in Aiven AI Database Optimizer check it out for FREE
What you will learn
- How to enable Amazon RDS for MySQL slow queries log, a feature which tracks non-performant SQL statements in your database
- How to connect the EverSQL sensor to an Amazon RDS for MySQL instance and start receiving performance insights and optimization suggestions
- How to optimize a query by adding indexes and rewriting the SQL statement
- In the Engine options section select MySQL and the 8.0.33 as version
- Select the Dev/Test Template that is sufficient for our example
- In the Settings section leave database-1 as DB instance identifier and set a Master password
- In the Instance configuration section select
db.m5.large
, the minimal instance size is sufficient - In the Connectivity section, enable Public access
- In the VPC security group (firewall) section select Create new to create a new VPC security group and enter
aws-rds-mysql-security-group
as name - In the Monitoring section disable Performance Insights.
- Click Create database
The above command starts the creation of the Amazon RDS for MySQL database.
- Create a new parameter group
- Customize the parameters to enable slow query log
- Associate the parameter group with the Amazon RDS for MySQL database
- Navigate to the Amazon RDS console
- Choose Parameter group
- Select Create parameter group
- In the Parameter group family section select mysql8.0
- In the Type section select DB Parameter Group
- Write
slow-query-parameter
as Group Name - Write
Slow query parameter
as Description - Click on Create
We should now see the newly created Parameter group called
slow-query-parameter
. Click on it to edit.- Click on Edit
- Set the following parameters:
general_log = 1
to enable loggingslow_query_log = 1
to enable slow query logginglong_query_time = 1
to log every query taking more than 1 second (you can change the parameter as needed)log_output =FILE
to write the slow query log in a dedicated table queryable by SQL- Click on Save Changes
- Navigate to the Amazon RDS console
- Click on Databases
- Click on the
database-1
we created previously - Click on Modify
- In the Additional configuration section, select
slow-query-parameter
as the DB parameter group - Click on Continue
- The Summary of modifications showcases the change in the DB parameter group, select the Apply immediately to restart the database and apply the changes.
- Click on Modify DB instance
Alert: After clicking on Modify DB instance, the database reboots immediately to make the necessary modifications to the parameters. After a few minutes the database will be up and running again and we’ll be able to connect to it.
- Navigate to EverSQL
- Click on Start Optimizing for Free
- Select Analyze why my database is slow
- Select MySQL
- Select Amazon RDS
- Select Native SQL. We are assuming we can directly change the SQL queries. If you are using an ORM to manage the database, please select the dedicated option
- After creating the account, select Analyze why my database is slow
- Click on install now, it takes 60 seconds
- The screen in the below picture showcases the steps needed to install the EverSQL performance sensor and provide the personal API key
- Click on Deploy
- In the Application settings section:
- Leave
EverSQL-Performance-Sensor
as Application name - Write
database-1
as AwsDBServersList, this name should match your database name - Write the AWS region where the Amazon RDS for MySQL instance is deployed (e.g.
eu-west-3
for Paris) - Copy the EverSQL API Key in the EverSQLApiKey field
- Check the I acknowledge that this app creates custom IAM roles checkbox
- Click on Deploy
Alert: if you don’t see the sensor as available in the EverSQL console, don’t worry! We’ll need to generate some slow traffic for it to show up.
After signing in, click on the Download button.
Once the zip file is downloaded, we can extract it and navigate with the terminal within the folder which contains a list of CSV files called
yellow_trip_data
together with the year and month.- Navigate to the Amazon RDS Console
- Click on Databases
- Click on the
database-1
we created previously - In the Connectivity and security section we can find the Endpoint and port
- With
mysql
command line already installed, we can execute the following command to connect:
[USERNAME]
is the MySQL user, by default admin[HOST]
is the database hostname, that you can find in the Connectivity and security section[PORT]
is the database port, that you can find in the Connectivity and security section[PASSWORD]
is the MySQL password that we set during the database creation
- Creates a database called
taxi
- Creates a table called
taxi_trips
- Creates a table called
taxi_zones
Now let’s load the data.We can use the LOAD DATA MySQL command for this.
7667793
records in the taxi_trips
table. By switching the file name in the 2nd row of the above SQL (now mentioning yellow_tripdata_2019-01.csv
) we can also load the other 17 files loading data for 2019 and 2020.We can also load the
taxi_zones
table with 133
records with the following statement.16.47
seconds on average.To check the presence of the slow query log:
- Navigate to the Amazon RDS console
- Click on Databases
- Click on the
database-1
we created previously - Click on Logs & events
- Scroll down to the Logs section
- Verify the presence of the
slowquery/mysql-slowquery.log
log
If themysql-slowquery.log
is not appearing, reboot the database and issue the query again.
database-1
.So far so good! The next step is to head to the EverSQL dashboard console page. Scrolling down, we can see the Your SQL Queries Container section listing the query we executed previously. We can find an Optimize button that provides us insight on how to speed the query.
Note: The EverSQL sensor runs every 3 minutes by default. You might need to wait a few minutes for your query to show up in the EverSQL console.
- Adding three indexes, one for the
taxi_trips
based onpayment_type
andtpep_pickup_datetime
and two fortaxi_zones
onLocationID
andZone
, more one dedicated to theBorough
- Rewriting the query to the following, avoiding using the DATE function in the filter condition and limiting the data from taxi_trips (filtering for the
tpep_pickup_datetime
andpayment_type
) before joining. All the explanations are available in the same window.
EverSQL allows us to monitor the database, review insights about non performing queries or workload anomalies and automatically receive suggestions on indexes and sql rewrites, exactly what we need to optimize performance and spend on our MySQL instance.