All articles
sql2026-11-16

10 SQL Anti-Patterns That Are Slowing Down Your Queries

Contents

You write a query, run it, and wait. 40 seconds. A minute. Two minutes. Sound familiar? Most SQL performance issues don't come from hardware limitations — they come from specific coding mistakes that can be fixed in 5 minutes. Here are 10 anti-patterns I see most often in day-to-day e-commerce data work.

1. SELECT * Everywhere

A classic. Instead of fetching only the columns you need, you pull everything. In [Snowflake](https://www.snowflake.com/?utm_source=newsletter&utm_medium=affiliate), this is especially costly because the columnar architecture means every additional column translates to additional I/O.

-- Bad
SELECT * FROM orders WHERE order_date >= '2024-01-01';

-- Good
SELECT order_id, customer_id, total_amount FROM orders WHERE order_date >= '2024-01-01';

2. Functions on Columns in the WHERE Clause

Wrapping a column in a function prevents Snowflake from using partition pruning and micro-partition elimination. You end up scanning the entire table instead of just the relevant partitions.

-- Bad
SELECT * FROM orders WHERE YEAR(order_date) = 2024;

-- Good
SELECT order_id, total_amount FROM orders 
WHERE order_date >= '2024-01-01' AND order_date < '2025-01-01';

3. Implicit Type Conversions

These are silent performance killers. Comparing VARCHAR to INTEGER or DATE to TIMESTAMP forces Snowflake to convert every single row on the fly.

-- Bad (customer_id is INTEGER, but you're passing a string)
SELECT * FROM customers WHERE customer_id = '12345';

-- Good
SELECT customer_id, email FROM customers WHERE customer_id = 12345;

4. Correlated Subqueries Instead of JOINs

A correlated subquery executes once for every row in the outer table. With millions of records, that's a disaster.

-- Bad
SELECT 
  o.order_id,
  (SELECT SUM(oi.amount) FROM order_items oi WHERE oi.order_id = o.order_id) AS total
FROM orders o;

-- Good
SELECT o.order_id, SUM(oi.amount) AS total
FROM orders o
JOIN order_items oi ON o.order_id = oi.order_id
GROUP BY o.order_id;

5. DISTINCT Instead of Fixing Your JOIN

DISTINCT is often a band-aid — a symptom of a bad join that's duplicating rows. Instead of fixing the join, you slap on DISTINCT and hope for the best.

-- Bad (duplicates caused by a 1:N join)
SELECT DISTINCT c.customer_id, c.email
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id;

-- Good
SELECT c.customer_id, c.email
FROM customers c
WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id);

6. OR in the WHERE Clause Instead of UNION ALL

OR often prevents efficient use of micro-partitions. UNION ALL lets Snowflake optimize each branch independently.

-- Bad
SELECT * FROM orders WHERE status = 'pending' OR status = 'processing';

-- Better (especially on large tables)
SELECT order_id, status FROM orders WHERE status = 'pending'
UNION ALL
SELECT order_id, status FROM orders WHERE status = 'processing';

-- Even better
SELECT order_id, status FROM orders WHERE status IN ('pending', 'processing');

7. Not Filtering Data Early in the CTE Chain

You write a chain of CTEs but leave filtering for the end. Every CTE processes the full dataset instead of working on already-filtered data.

-- Bad
WITH all_orders AS (
  SELECT * FROM orders
),
filtered AS (
  SELECT * FROM all_orders WHERE order_date >= '2024-01-01'
)
SELECT customer_id, COUNT(*) FROM filtered GROUP BY customer_id;

-- Good
WITH recent_orders AS (
  SELECT customer_id, order_id 
  FROM orders 
  WHERE order_date >= '2024-01-01'
)
SELECT customer_id, COUNT(*) FROM recent_orders GROUP BY customer_id;

8. COUNT(DISTINCT ...) on Huge Datasets Without Approximations

COUNT(DISTINCT) is expensive — it requires full materialization and deduplication. When you only need an approximation (e.g., number of unique users), use HyperLogLog, which is available natively in Snowflake.

-- Expensive on billions of rows
SELECT COUNT(DISTINCT user_id) FROM events WHERE event_date = '2024-11-01';

-- Faster approximation (~1-2% error)
SELECT HLL(user_id) FROM events WHERE event_date = '2024-11-01';

9. Using HAVING Instead of WHERE to Filter Before Aggregation

HAVING runs after aggregation. If you're filtering values you already know before grouping, move them to WHERE and save processing time.

-- Bad
SELECT customer_id, SUM(total_amount) AS revenue
FROM orders
GROUP BY customer_id
HAVING customer_id IS NOT NULL AND SUM(total_amount) > 1000;

-- Good
SELECT customer_id, SUM(total_amount) AS revenue
FROM orders
WHERE customer_id IS NOT NULL
GROUP BY customer_id
HAVING SUM(total_amount) > 1000;

10. Unnecessary Sorting in Subqueries

ORDER BY inside a subquery or CTE that isn't the final result is pure resource waste. Snowflake can reorder processing anyway — that sort means nothing.

-- Bad — ORDER BY in a CTE is meaningless
WITH sorted_orders AS (
  SELECT order_id, customer_id, total_amount
  FROM orders
  ORDER BY order_date DESC
)
SELECT customer_id, SUM(total_amount) FROM sorted_orders GROUP BY customer_id;

-- Good — sort only where it actually matters
WITH orders_data AS (
  SELECT order_id, customer_id, total_amount FROM orders
)
SELECT customer_id, SUM(total_amount) FROM orders_data GROUP BY customer_id;

How to Diagnose Problems in Practice

Snowflake gives you several diagnostic tools. Query Profile in Snowsight shows the query execution tree — look for nodes with high "Bytes Scanned" and "Spillage to Disk." Those are your first optimization targets. INFORMATION_SCHEMA.QUERY_HISTORY lets you surface the most expensive queries from the last 7 days:

Is your data scattered across several places?

I help build a warehouse and pipelines that refresh on their own. The first call is free.

Book a consultation

More articles about data analytics:

Back to blog