Overview
Many SQL performance problems are caused not by missing indexes, but by SQL structures that restrict the optimizer’s available execution strategies. Common examples include:- Repeated execution of correlated subqueries
- Predicates that cannot be pushed down
- OR expressions that reduce index usability
- Unnecessary DISTINCT or GROUP BY operations
- Deep pagination that scans and sorts excessive rows
- Cross-shard data movement in distributed databases
Why Query Rewrite Matters
A database optimizer searches for the best execution plan within the structure of the SQL statement it receives. It does not always transform the business expression of that SQL into an entirely different but equivalent form. Semantically equivalent SQL expressions can expose very different optimization opportunities. PawSQL Query Rewrite expands the search space available to the optimizer.Key Capabilities
Subquery rewrite
Analyze IN, EXISTS, scalar subqueries, and related forms to identify equivalent structures that may execute more efficiently on the target database.Predicate optimization
Optimize WHERE and JOIN predicates, including:- Predicate pushdown
- Redundant predicate elimination
- OR-condition rewrite
- Expression simplification
- Recovery of index-friendly predicates
Join optimization
Identify and optimize:- Redundant joins
- Join elimination
- Structural join issues
- Subqueries that can be decorrelated
- Cross-shard join risks in distributed databases
Aggregation optimization
Rewrite or simplify DISTINCT, GROUP BY, COUNT, MIN/MAX, and related aggregation patterns.Pagination optimization
Generate alternative forms for deep OFFSET / LIMIT pagination scenarios.Distributed SQL optimization
Consider distributed-database characteristics such as:- Distribution keys
- Data movement
- Cross-node joins
- Replicated or broadcast tables
- Global and local indexes
Big data SQL optimization
Analyze big-data SQL patterns such as:- Partition pruning
- Bucket joins
- Data skew
- COUNT DISTINCT
- GROUP BY skew
- Window-function skew
- Global sorting
Example
Original SQL:Semantic Safety
SQL rewriting must preserve semantics before it can improve performance. PawSQL therefore needs to account for:- NULL semantics
- Aggregation semantics
- DISTINCT behavior
- Outer join semantics
- Data types and implicit conversions
- Dialect differences
- Function and expression behavior