Uploaded November 2026 | Updated November 2026, 3 hours ago
In this episode of Coding with Dr. Chuck with AI, I start working through a backlog of slow SQL queries collected from real Tsugi production systems.
My servers log database queries that exceed a time threshold, which gives me a list of problems that may never show up in a small development database. Python for Everybody provides a particularly useful test environment because some production courses contain hundreds of thousands of users.
For the first optimization, I choose a discussion-related query that runs when a user first visits discussions in a course.
Instead of simply staring at the SQL and guessing at an index, Cursor takes a much more thorough approach. It reproduces the query with synthetic data, runs EXPLAIN and benchmark tests, studies the existing indexes, and works backwards through the application code to understand exactly what the query is supposed to accomplish.
The problem turns out not to be a missing index. The execution plan was scanning far too many discussion threads before narrowing the results to the current course.
We make a relatively small change that preserves the behavior while allowing the database to take a much more efficient path. We also notice that the current context ID is already available to the application, eliminating an unnecessary lookup.
The resulting query is estimated to run roughly 10–15 times faster while preserving the existing first-visit unread-discussion behavior.
Topics include:
Slow SQL queries
Production performance tuning
MySQL query optimization
EXPLAIN and query plans
Database indexes
SQL joins
Discussion unread tracking
Synthetic benchmarks
Regression testing
Tsugi
Python for Everybody
Cursor AI
AI-assisted software development
This is the first of what will probably be several episodes working through real slow queries found in production.
In this episode of Coding with Dr. Chuck with AI, I start working through a backlog of slow SQL queries collected from real Tsugi production systems.
My servers log database queries that exceed a time threshold, which gives me a list of problems that may never show up in a small development database. Python for Everybody provides a particularly useful test environment because some production courses contain hundreds of thousands of users.
For the first optimization, I choose a discussion-related query that runs when a user first visits discussions in a course.
Instead of simply staring at the SQL and guessing at an index, Cursor takes a much more thorough approach. It reproduces the query with synthetic data, runs EXPLAIN and benchmark tests, studies the existing indexes, and works backwards through the application code to understand exactly what the query is supposed to accomplish.
The problem turns out not to be a missing index. The execution plan was scanning far too many discussion threads before narrowing the results to the current course.
We make a relatively small change that preserves the behavior while allowing the database to take a much more efficient path. We also notice that the current context ID is already available to the application, eliminating an unnecessary lookup.
The resulting query is estimated to run roughly 10–15 times faster while preserving the existing first-visit unread-discussion behavior.
Topics include:
Slow SQL queries
Production performance tuning
MySQL query optimization
EXPLAIN and query plans
Database indexes
SQL joins
Discussion unread tracking
Synthetic benchmarks
Regression testing
Tsugi
Python for Everybody
Cursor AI
AI-assisted software development
This is the first of what will probably be several episodes working through real slow queries found in production.










