SQL Window Functions: Performance, EXPLAIN, and Dialect Notes/Pre-filter rows before applying windows
Reduce the working set first.
Section: Performance patterns
Pre-filter rows before applying windows
sql
sql
WITH recent_orders AS (
SELECT *
FROM orders
WHERE order_date >= CURRENT_DATE - INTERVAL '90 days'
)
SELECT customer_id,
order_date,
amount,
ROW_NUMBER() OVER (
PARTITION BY customer_id
ORDER BY order_date DESC
) AS rn
FROM recent_orders;Explanation
Filtering early is one of the highest-leverage ways to speed up analytic queries.
Learn the surrounding workflow
Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.
Related commands
Same sheet · prioritizing Performance patterns
Create a supporting index for partition/order keys
Help the database read rows in the right order.
Materialize an expensive intermediate result
Break a huge query into smaller stages.
QUALIFY shortcut in some warehouses
Filter window results without wrapping a subquery.