Lesson 35 of the SQL Optimization Course: Optimizing MySQL Partitioned Tables for Time Series Queries

For relational databases, the design of tables and SQL is written are particularly crucial. It wouldn’t be an exaggeration to say that they account for 90% of performance. So this time, specifically targeting these two major knowledge areas, we’ll conduct a detailed analysis for you, peeling back the layers.
This Series uses plain and understandable language and selects a large number of examples to elaborate on the subtleties for you.
🧑💻 Target audience:
We will use MySQL as the demonstration database.
This article builds on our previous guide to MySQL time-based partitioning. It focuses on critical SQL best practices for maximizing efficiency in partitioned table queries. While partitioning reduces scanned records, improper queries can negate its benefits.
Consider two tables:
ytt_pt1 (partitioned by log_date)
|
|
The second query uses an explicit IN list due to MySQL’s partition pruning limitations. Partition pruning (which skips irrelevant partitions) supports functions like TO_DAYS()
but not MONTH()
.
Verify pruning behavior with a test table pt_pruning (partitioned by TO_DAYS(log_date))
:
|
|
Queries leveraging pruning (e.g., log_date = ‘2020-01-02’) execute in 0.03s, while non-prunable ranges (e.g., log_date < ‘2020-02-01’) scan all partitions.
If your partition function lacks pruning support:
Replace ranges with exact values or IN lists. Example:
|
|
Direct the optimizer to target specific partitions:
|
|
For optimal time-series partitioned tables:
Aligning SQL logic with partitioning schemes ensures maximum performance for large datasets.
👋 See you in the next lesson.
SQLFlash is your AI-powered SQL Optimization Partner.
Based on AI models, we accurately identify SQL performance bottlenecks and optimize query performance, freeing you from the cumbersome SQL tuning process so you can fully focus on developing and implementing business logic.
Join us and experience the power of SQLFlash today!.