Lesson 34 of the SQL Optimization Course: Partition Pruning Techniques for Time-Based Tables

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.
Partition pruning is a critical optimization technique in MySQL that enhances query performance by reducing the number of scanned partitions. This article explores practical strategies for leveraging partition pruning in time-based partitioned tables.
When working with time-based partitioned tables like ytt_pt1_month1
, developers often encounter inefficiencies due to MySQL’s limited support for certain partition functions. For example:
|
|
This query’s inefficiency stems from MySQL’s inability to prune partitions effectively when using non-supported functions like MONTH()
.
Use direct equality checks on partition keys to leverage pruning:
|
|
This approach reduces scan time to 0.03 seconds by isolating the relevant partition (p_01
).
Convert range conditions into explicit lists for better pruning:
|
|
This modification executes in 0.04 seconds, compared to 1.93 seconds for the original range query.
Direct the optimizer to specific partitions using hints:
|
|
This results in 0.04 seconds execution time by bypassing unnecessary partitions.
TO_DAYS()
, YEAR()
, or UNIX_TIMESTAMP()
for optimal pruning.MONTH()
limit pruning capabilities.By aligning SQL queries with MySQL’s partition pruning mechanics—through equality checks, list-based filtering, and strategic hints—developers can achieve significant performance gains. For legacy systems using non-prunable functions, manual query refactoring or hints offer viable workarounds.
👋 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!.