Sub topic 15
Performance Notes and Dialect Differences
Assume you know SELECT, joins, GROUP BY, aggregate functions, subqueries, and common table expressions (CTEs). You also need a database that supports window functions. The examples use PostgreSQL-style SQL, but most also work in MySQL 8+, SQL Server, SQLite 3.25+, and Oracle with small changes.
Why window functions can be expensive
A window function may need to organize rows before calculating a result. For example, this query must group rows by region and order each region by date:
SELECT
region,
sale_date,
amount,
SUM(amount) OVER (
PARTITION BY region
ORDER BY sale_date, id
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS running_total
FROM sales;The database may sort rows by region, sale_date, and id. Large sorts can use memory and may spill to temporary storage. Several window functions with different partitioning or ordering rules may require several sorts.
A useful index can reduce this work:
CREATE INDEX sales_region_date_id_idx
ON sales (region, sale_date, id);An index is not a guarantee that the database will avoid sorting. The optimizer chooses a plan based on table size, statistics, filters, and available indexes. Use your database's plan tool, such as EXPLAIN or EXPLAIN ANALYZE, to check the actual work.
Reduce the rows before the window step
Filtering early often matters more than changing the window expression. If the report covers one year, filter the source rows before calculating windows:
WITH recent_sales AS (
SELECT region, sale_date, id, amount
FROM sales
WHERE sale_date >= DATE '2026-01-01'
)
SELECT
region,
sale_date,
amount,
SUM(amount) OVER (
PARTITION BY region
ORDER BY sale_date, id
ROWS UNBOUNDED PRECEDING
) AS running_total
FROM recent_sales;The optimizer may move predicates automatically, but clear query structure makes intent easier to inspect. Do not assume that a CTE always creates a temporary table. PostgreSQL, SQL Server, MySQL, SQLite, and Oracle can treat CTEs differently depending on the query and version.
Dialect differences
| Feature | PostgreSQL | MySQL 8+ | SQL Server | SQLite |
|---|---|---|---|---|
| Named `WINDOW` clause | Yes | Yes | No | Yes |
| `NULLS FIRST/LAST` syntax | Yes | Limited | No | Yes |
| `ROWS` frames | Yes | Yes | Yes | Yes |
| `GROUPS` frames | Version-dependent | Check version | No | Yes |
Oracle supports analytic functions, but its syntax and version support should be checked separately. Frame syntax is especially different across systems. RANGE with numeric or interval offsets may not be portable, so use ROWS when you mean a physical number of rows and need predictable behavior.
Ordering also differs for null values. PostgreSQL lets you write NULLS LAST; SQL Server requires an expression such as:
ORDER BY CASE WHEN sale_date IS NULL THEN 1 ELSE 0 END, sale_datePortable queries should include a tie-breaker such as id. Without one, rows with equal ordering values may appear in an unpredictable order, especially when using ROWS.
Exercise
A running-total query is slow on a large table. What should you inspect first?
Show answer
Inspect the execution plan, then check whether early filters reduce the input and whether an index supports the filter and window ordering.