Writing Fast SQL Queries on Large Data

A query that runs fine on small data can crawl on large data. Here are ten habits for writing fast SQL, with examples.

A query that finishes instantly on a table with thousands of rows can run for hours once the data grows to hundreds of millions of rows. This article follows up SQL for beginners with ten habits that keep queries fast on large data. Examples use standard SQL syntax; details may vary slightly between databases.

1. Select only the columns you need

Avoid SELECT *. In columnar databases like ClickHouse or BigQuery, every extra column means extra data read from disk.

-- Slow
SELECT * FROM fact_sales;
-- Better
SELECT sale_date, branch_id, total FROM fact_sales;

2. Filter as early as possible

Put filters in WHERE so unneeded rows are dropped before joins or aggregation.

3. Use partitions

Large tables are usually partitioned by date. Always include a filter on the partition column so the database reads only the relevant partitions (partition pruning).

SELECT branch_id, SUM(total)
FROM fact_sales
WHERE sale_date >= '2026-11-01' AND sale_date < '2026-12-01'
GROUP BY branch_id;

4. Do not wrap filter columns in functions

Functions on a filtered column can prevent indexes or partitions from being used.

-- Slow: function on the column
WHERE YEAR(sale_date) = 2026
-- Better: a value range
WHERE sale_date >= '2026-01-01' AND sale_date < '2027-01-01'

5. Use indexes wisely (row-store databases)

In transactional databases like PostgreSQL or MySQL, index the columns used most for filters and joins. But do not overdo it: every index slows down inserts and updates.

6. Join on the right keys

Join on columns of the same type, ideally numeric surrogate keys, as in a star schema. Make sure join conditions are complete to avoid accidental row multiplication.

7. Aggregate first, join later

If you only need totals per customer, summarize the fact table first, then join it to the dimension table.

WITH summary AS (
  SELECT customer_id, SUM(total) AS total
  FROM fact_sales
  WHERE sale_date >= '2026-11-01'
  GROUP BY customer_id
)
SELECT c.name, s.total
FROM summary s JOIN dim_customer c ON c.customer_id = s.customer_id;

8. Choose EXISTS or JOIN deliberately

To check “does it exist”, EXISTS is often more efficient than IN with a large subquery, or a JOIN that then needs DISTINCT.

9. Read the execution plan

Use EXPLAIN (or EXPLAIN ANALYZE) to see how the database runs a query: whether it uses an index, how many rows it reads, and which step is heaviest. It is the most reliable way to find the cause of slowness.

10. Prepare summary tables for dashboards

A dashboard opened hundreds of times a day should read an aggregate table or materialized view refreshed on a schedule, not recalculate from billions of rows every time.

Bonus: more good habits

  • Use LIMIT while exploring data.
  • For estimates on very large data, some databases offer approximate functions like APPROX_COUNT_DISTINCT that are much faster.
  • Avoid ORDER BY on large results unless truly needed.
  • Keep table statistics up to date so the optimizer can choose the best plan.

Conclusion

Fast queries start with simple habits: read less data, filter early, use partitions and indexes, then check the execution plan. Combined with a good data model in the data warehouse, your dashboards will open in seconds.

Level up your team’s SQL skills

DNASTUDIO’s SQL and data warehouse training covers query optimization with hands-on practice on large datasets.

Leave a comment

Your email address will not be published. Required fields are marked *