A small script for finding slow queries and checking whether a fix actually helped. It reads a query's execution plan with EXPLAIN, flags full table scans, suggests indexes for the columns in the WHERE clause, and times the original query against your rewritten one.
- Execution plan check. Runs
EXPLAINon a query and looks for tables read withtype = ALLand no usable key. - Index suggestions. Pulls the column names out of the
WHEREclause and suggests an index on them for each table that is fully scanned. - Before and after timing. Runs the original and the optimized query and prints both times and the percentage improvement.
- Scheduled checks. Every minute, runs a small health check in a transaction (the row count of a
userstable and the InnoDB buffer pool data size) using theschedulelibrary, until you stop it with Ctrl+C.
pip install mysql-connector-python schedule- Fill in the connection details in
main()insql_opt_v2.py. - Replace the placeholder queries in
sql_analysis_demo()([column_name],[TABLE],[WHERE_OPTIONS]) with the query you want to check and your improved version. - Run:
python sql_opt_v2.py
The WHERE parsing is a simple regular expression, so it handles plain conditions like column = value or column > value joined by AND and OR. Joins, subqueries and functions on columns need a manual look at the plan.