Expand description
Cost-based query optimization for TLExpr.
This module implements a cost-based optimizer that:
- Generates equivalent expression rewrites
- Estimates execution cost for each alternative
- Selects the most efficient representation
- Provides backend optimization hints
§Overview
The cost-based optimizer explores the space of equivalent expressions by applying rewriting rules, then uses cost estimation to select the best alternative. This is particularly useful for complex queries where there are multiple valid execution strategies.
§Example
use tensorlogic_compiler::optimize::optimize_by_cost;
use tensorlogic_compiler::CompilerContext;
use tensorlogic_ir::{TLExpr, Term};
let mut ctx = CompilerContext::new();
ctx.add_domain("Person", 1000);
// Complex expression with multiple equivalent forms
let expr = TLExpr::and(
TLExpr::pred("p", vec![Term::var("x")]),
TLExpr::or(
TLExpr::pred("q", vec![Term::var("x")]),
TLExpr::pred("r", vec![Term::var("x")]),
),
);
let (optimized, stats) = optimize_by_cost(&expr, &ctx);
println!("Cost reduction: {:.1}%", stats.cost_reduction_percent());Structs§
- Cost
Based Optimizer - Cost-based optimizer that explores rewrite space.
- Cost
Based Stats - Statistics from cost-based optimization.
Enums§
- Rewrite
Rule - Rewriting rule for generating equivalent expressions.
Functions§
- optimize_
by_ cost - Optimize an expression using cost-based optimization.
- optimize_
by_ cost_ with_ config - Optimize with custom parameters.