Automatically Killing Long-Running Queries in Aurora MySQL (Serverless v2)

Long-running queries can wreak havoc on Aurora MySQL performance. Learn how to automatically detect, log, and kill queries that exceed a time limit using MySQL’s event scheduler. This step-by-step guide walks through building a killer procedure, testing safely, scheduling it, and keeping full audit logs.

Table of Contents

 

Introduction

One of the most frustrating issues for database administrators is when a query runs far longer than expected. Long-running queries can tie up CPU, block locks, and degrade performance for every other workload on the cluster.

On Amazon Aurora MySQL (Serverless v2), you don’t have direct OS-level access to cron or external process managers, so you need to handle this inside the database itself. Fortunately, MySQL’s event scheduler and a bit of stored procedure logic provide everything you need to automatically find, log, and kill queries that exceed a defined threshold.

In this post, we’ll walk step-by-step through building a solution that:

  • Detects queries running longer than a threshold (e.g., 120 seconds).
  • Kills them cleanly.
  • Logs every action in an audit table.
  • Provides a smoke test so you can verify behavior without actually killing queries.
  • Lets you adjust frequency, thresholds, and exclusions to fit your environment.

Step 1. Create the Log Table

We’ll start by creating a table that records every action. This log will capture details like the thread ID, user, query text, and who killed it.

This table will also serve as a target for our test events to verify that the event scheduler is working.


Step 2. Verify and Enable the Event Scheduler

Aurora MySQL uses the event scheduler to run recurring jobs inside the database. By default, this may be turned off.

Check the status:

  • ON → Events will run normally.
  • OFF or DISABLED → You’ll need to enable it.

In Aurora, this requires changing the DB cluster parameter group:

  1. In the AWS Console, go to RDS → Databases → Your Aurora cluster → Configuration.
  2. Note the DB cluster parameter group.
  3. Go to RDS → Parameter groups and edit the group (or create a new one).
  4. Set event_scheduler = ON.
  5. Apply the group to your cluster. Aurora Serverless v2 usually applies changes quickly.

Optional Test

To confirm the scheduler is working, create a test event that inserts a row into the log after one minute:

After a minute, query the log:

If you see a row with event_test, the scheduler is active. Clean up when done:


Step 3. Build a Smoke Test (No Kill)

Before introducing any destructive behavior, it’s good practice to confirm your logic is sound. The smoke test scans the processlist for long-running queries and logs them—but does not kill them.

You can call it manually:

Check the kill_log table to see if anything was captured. Optionally, you can schedule this procedure as a recurring event for a short period of time to validate it runs automatically, then drop the event.


Step 4. Create the Killer Procedure

Now that you’ve confirmed the smoke test works, it’s time to add the actual enforcement. The killer procedure is almost identical, except it issues a KILL QUERY for each offending thread.


Step 5. Schedule the Killer

With the procedure in place, you can now schedule it to run automatically every 30 seconds:

This setup means:

  • Every 30 seconds, the procedure runs.
  • Any query running longer than 120 seconds is killed.
  • A log entry is created in kill_log.

Step 6. Operations and Maintenance

Adjust the Frequency

You can change how often the killer runs by altering the event:

  • Run every 1 minute:
  • Run every 5 minutes:

The minimum interval is 1 second, but in most environments, every 30–120 seconds is sufficient.

Exempt Specific Users

If you have ETL jobs or reporting queries that are expected to run long, you can exclude their usernames in the procedure’s cursor query:

Log Retention

Over time, the kill_log table will grow. Set up a daily purge event:

Maintenance Windows

You can temporarily disable or re-enable the killer:

Cleanup

To remove everything:


Conclusion

Aurora Serverless v2 doesn’t give you shell access, but with MySQL’s event scheduler you can still build lightweight, reliable jobs to manage runaway queries.

By combining a logging table, a smoke test, a killer procedure, and scheduled events, you now have a way to:

  • Protect cluster performance by automatically killing long-running queries.
  • Retain a full audit trail for accountability.
  • Tune the schedule, thresholds, and exclusions to match your workload.

This approach gives you the control and visibility you need to keep Aurora clusters healthy—even when application queries misbehave.

Have a project or a problem?

Talk with a senior engineer for practical recommendations—no obligation.

Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Categories

Get a free consultation from Reliable Penguin

Submit the form—or for immediate service call 866-649-7984.