Sub topic 16
Comparing Optimised and Unoptimised Queries
This chapter assumes you can write Polars expressions and have read an explain() plan, as covered in earlier chapters. You will compare an optimised query with the same query run without optimisations. This confirms that the optimiser keeps results the same, shows where it saves work, and helps you find the step that stops it.
Why compare
The optimiser rewrites your plan but must not change the answer. Comparing the two versions is useful in three situations:
- Benchmarking: you want to know how much the optimiser helps.
- Suspected bug: you think an optimisation changed a result.
- Missing speed-up: you expected a filter to reach the scan, but the plan shows otherwise.
Run both versions
Disabling optimisations uses pl.QueryOptFlags. This class exists in recent Polars releases. Older releases use keyword arguments such as predicate_pushdown=False on collect().
import time
import polars as pl
pl.DataFrame({
"user_id": list(range(200_000)),
"ts": list(range(200_000)),
"country": ["NO", "SE", "PE", "EG"] * 50_000,
"payload": ["x" * 20] * 200_000,
}).write_parquet("events.parquet")
lf = (
pl.scan_parquet("events.parquet")
.select("user_id", "ts", "country")
.filter(pl.col("country") == "NO")
)
def timed(opts):
start = time.perf_counter()
out = lf.collect(optimizations=opts)
return out, time.perf_counter() - start
fast, t_fast = timed(pl.QueryOptFlags())
slow, t_slow = timed(pl.QueryOptFlags.none())
print("same result:", fast.equals(slow))
print(f"optimised: {t_fast:.4f}s, unoptimised: {t_slow:.4f}s")same result: True is the check that matters. The timings vary by machine, and on small files they can be close. Run each version several times before you trust a difference.
Compare the plans
The plan shows why the timings differ.
print(lf.explain(optimized=False))
print(lf.explain())In the unoptimised plan, the SELECT and FILTER nodes sit above the Parquet scan. In the optimised plan, the scan node lists only the three columns used and carries the country predicate. The payload column is never read.
Turn off one optimisation at a time
To find which optimisation matters, or which one you suspect, disable just that one:
no_pushdown = pl.QueryOptFlags(predicate_pushdown=False)
print(lf.explain(optimizations=no_pushdown))Other flags include projection_pushdown, slice_pushdown and comm_subexpr_elim. If the result changes when you toggle a single flag, you have found the rewrite to investigate. Keep a minimal example for a bug report.
Summary of the workflow
- Check that optimised and unoptimised results are equal.
- Read both plans and look for the filter and column list in the scan node.
- Toggle one flag at a time to isolate its effect.
- If a pushdown is missing, look for a barrier such as a Python UDF.
Exercise
Your optimised and unoptimised runs give equal results, but the optimised plan still shows the filter above the scan. What should you check next?
Show answer
Look for a step before the filter that blocks pushdown, such as a map_elements call with a Python function. Rewrite it with native expressions, then compare explain() output again.