Sitemap
The Pipeline: AI ML and Cloud

Practical writing on AI, ML and cloud computing for practitioners and curious minds. Examples, Esssays, and Clear thinking from people who work in the field.

Member-only story

Your SQL Query Works. Here’s Why It’s Still Slow.

--

A query that returns the right answer slowly is still a problem. Here are the most common reasons SQL queries underperform — and the fixes that actually help.

Press enter or click to view image in full size

1. SELECT * Is Pulling Data You Don’t Need

Pulls every column across every row:

SELECT * FROM orders JOIN customers ON orders.customer_id = customers.id;

If you only need three columns, pulling thirty costs you memory, network transfer, and in columnar storage systems (like Databricks or BigQuery), unnecessary column scans.

Fix: Name your columns explicitly. Yes, it’s more typing, but it’s faster and more readable.

SELECT 
orders.id AS order_id,
orders.order_date,
orders.total_amount,
customers.id AS customer_id,
customers.first_name,
customers.last_name,
customers.email
FROM orders
JOIN customers ON orders.customer_id = customers.id;

────────────

2. You’re Filtering After Aggregating Instead of Before

— Slow: aggregates everything, then filters

SELECT customer_id, SUM(amount) 
FROM orders
GROUP BY customer_id
HAVING customer_id > 1000;

— Fast: filters first, then aggregates over smaller dataset

--

--

Suhana Saini
Suhana Saini

Written by Suhana Saini

Data Engineer | Contributing Writer at Towards Data Engineering. Breaking down SQL, PySpark, & data pipelines one query at a time. With bit of life experiences.

The Pipeline: AI ML and Cloud
The Pipeline: AI ML and Cloud

Published in The Pipeline: AI ML and Cloud

Practical writing on AI, ML and cloud computing for practitioners and curious minds. Examples, Esssays, and Clear thinking from people who work in the field.