How to Optimize Long-Running Queries in SQL Medium
How to Optimize Long-Running Queries in SQL Medium
Open in app
Search
Member-only story
https://medium.com/endtoenddata/how-to-optimize-long-running-queries-in-sql-bd797ae9dfd6 1/24
3/5/25, 10:06 AM How to Optimize Long-Running Queries in SQL? | by Prem Vishnoi(cloudvala) | EndToEndData | Feb, 2025 | Medium
https://medium.com/endtoenddata/how-to-optimize-long-running-queries-in-sql-bd797ae9dfd6 2/24
3/5/25, 10:06 AM How to Optimize Long-Running Queries in SQL? | by Prem Vishnoi(cloudvala) | EndToEndData | Feb, 2025 | Medium
https://medium.com/endtoenddata/how-to-optimize-long-running-queries-in-sql-bd797ae9dfd6 3/24
3/5/25, 10:06 AM How to Optimize Long-Running Queries in SQL? | by Prem Vishnoi(cloudvala) | EndToEndData | Feb, 2025 | Medium
https://medium.com/endtoenddata/how-to-optimize-long-running-queries-in-sql-bd797ae9dfd6 4/24
3/5/25, 10:06 AM How to Optimize Long-Running Queries in SQL? | by Prem Vishnoi(cloudvala) | EndToEndData | Feb, 2025 | Medium
Here are some practical strategies to help we identify bottlenecks and speed up our
queries:
This reveals how the database processes our query, including which indexes are
used, the order of operations, and estimated costs.
https://medium.com/endtoenddata/how-to-optimize-long-running-queries-in-sql-bd797ae9dfd6 5/24
3/5/25, 10:06 AM How to Optimize Long-Running Queries in SQL? | by Prem Vishnoi(cloudvala) | EndToEndData | Feb, 2025 | Medium
Identify Slow Parts: Look for full table scans, expensive joins, or sorting
operations that could be optimized.
2. Indexing
https://medium.com/endtoenddata/how-to-optimize-long-running-queries-in-sql-bd797ae9dfd6 6/24
3/5/25, 10:06 AM How to Optimize Long-Running Queries in SQL? | by Prem Vishnoi(cloudvala) | EndToEndData | Feb, 2025 | Medium
Add Indexes: Create indexes on columns used in WHERE, JOIN, GROUP BY, or
ORDER BY clauses. For example:
https://medium.com/endtoenddata/how-to-optimize-long-running-queries-in-sql-bd797ae9dfd6 7/24
3/5/25, 10:06 AM How to Optimize Long-Running Queries in SQL? | by Prem Vishnoi(cloudvala) | EndToEndData | Feb, 2025 | Medium
Avoid Over-Indexing: Too many indexes can slow down INSERT, UPDATE, and
DELETE operations, so balance read vs. write performance.
3. Rewrite the Query
https://medium.com/endtoenddata/how-to-optimize-long-running-queries-in-sql-bd797ae9dfd6 8/24
3/5/25, 10:06 AM How to Optimize Long-Running Queries in SQL? | by Prem Vishnoi(cloudvala) | EndToEndData | Feb, 2025 | Medium
https://medium.com/endtoenddata/how-to-optimize-long-running-queries-in-sql-bd797ae9dfd6 9/24
3/5/25, 10:06 AM How to Optimize Long-Running Queries in SQL? | by Prem Vishnoi(cloudvala) | EndToEndData | Feb, 2025 | Medium
https://medium.com/endtoenddata/how-to-optimize-long-running-queries-in-sql-bd797ae9dfd6 10/24
3/5/25, 10:06 AM How to Optimize Long-Running Queries in SQL? | by Prem Vishnoi(cloudvala) | EndToEndData | Feb, 2025 | Medium
Simplify Complex Queries: Break down large queries into smaller, more
manageable parts (e.g., using temporary tables or CTEs).
Optimize Joins: Use INNER JOIN instead of LEFT JOIN where possible, and
ensure join conditions use indexed columns.
Filter Early: Apply WHERE conditions to reduce the dataset before joining or
grouping
Partition Large Tables: For very large tables, consider partitioning by date,
range, or another logical division to reduce the data scanned.
5. Optimize Subqueries
Replace Subqueries with Joins: Subqueries can sometimes be less efficient than
joins:
-- Instead of:
SELECT * FROM table_a WHERE id IN (SELECT id FROM table_b);
-- Use:
SELECT a.* FROM table_a a JOIN table_b b ON a.id = b.id;
Use EXISTS: For checking existence, EXISTS can be faster than IN.
6. Caching
https://medium.com/endtoenddata/how-to-optimize-long-running-queries-in-sql-bd797ae9dfd6 12/24
3/5/25, 10:06 AM How to Optimize Long-Running Queries in SQL? | by Prem Vishnoi(cloudvala) | EndToEndData | Feb, 2025 | Medium
Materialized Views: If the data doesn’t change often, store the query results in a
materialized view and refresh it periodically.
7. Database Configuration
https://medium.com/endtoenddata/how-to-optimize-long-running-queries-in-sql-bd797ae9dfd6 13/24
3/5/25, 10:06 AM How to Optimize Long-Running Queries in SQL? | by Prem Vishnoi(cloudvala) | EndToEndData | Feb, 2025 | Medium
Update Statistics: Ensure the database’s statistics are current (e.g., ANALYZE in
PostgreSQL) so the query planner makes informed decisions.
https://medium.com/endtoenddata/how-to-optimize-long-running-queries-in-sql-bd797ae9dfd6 14/24
3/5/25, 10:06 AM How to Optimize Long-Running Queries in SQL? | by Prem Vishnoi(cloudvala) | EndToEndData | Feb, 2025 | Medium
Profile the Query: Use tools like SQL Server Profiler, PostgreSQL’s
pg_stat_statements, or MySQL’s Performance Schema to measure execution time
and resource usage.
Test Changes: Benchmark our query before and after optimizations to confirm
improvements.
Example
Suppose we have this slow query:
https://medium.com/endtoenddata/how-to-optimize-long-running-queries-in-sql-bd797ae9dfd6 15/24
3/5/25, 10:06 AM How to Optimize Long-Running Queries in SQL? | by Prem Vishnoi(cloudvala) | EndToEndData | Feb, 2025 | Medium
SELECT *
FROM orders o
JOIN customers c ON o.customer_id = c.id
WHERE o.order_date >= '2023-01-01'
ORDER BY o.order_date;
Optimization Steps:
3. Ensure customer_id and id are indexed (usually primary keys are by default).
Final Thoughts
The best approach depends on our specific query, data size, and database system.
Start by analyzing the execution plan, then apply targeted optimizations like
indexing or rewriting.
Follow
Published in EndToEndData
12 Followers · Last published 1 day ago
EndToEndData is the ultimate destination for data engineers and architects looking to ace interviews,
uncover innovative ideas, and master data architecture theory. We cover the full spectrum — from core
principles to complete solutions — offering practical advice, fresh insights
Following
Head of Data and ML experienced in designing, implementing, and managing large-scale data
infrastructure. Skilled in ETL, data modeling, and cloud computing
Responses (1)
Dime
TechieTreeHugger
Feb 23
Some really nice suggestions on optimization techniques, I will definitely try some of them.
9 1 reply Reply
https://medium.com/endtoenddata/how-to-optimize-long-running-queries-in-sql-bd797ae9dfd6 17/24
3/5/25, 10:06 AM How to Optimize Long-Running Queries in SQL? | by Prem Vishnoi(cloudvala) | EndToEndData | Feb, 2025 | Medium
Feb 6 76
https://medium.com/endtoenddata/how-to-optimize-long-running-queries-in-sql-bd797ae9dfd6 18/24
3/5/25, 10:06 AM How to Optimize Long-Running Queries in SQL? | by Prem Vishnoi(cloudvala) | EndToEndData | Feb, 2025 | Medium
Feb 23 57
Feb 13 51
Overview :
Jan 25 20
Vijay Gadhave
Feb 21 25 1
https://medium.com/endtoenddata/how-to-optimize-long-running-queries-in-sql-bd797ae9dfd6 20/24
3/5/25, 10:06 AM How to Optimize Long-Running Queries in SQL? | by Prem Vishnoi(cloudvala) | EndToEndData | Feb, 2025 | Medium
Feb 26 7 1
Lists
https://medium.com/endtoenddata/how-to-optimize-long-running-queries-in-sql-bd797ae9dfd6 21/24
3/5/25, 10:06 AM How to Optimize Long-Running Queries in SQL? | by Prem Vishnoi(cloudvala) | EndToEndData | Feb, 2025 | Medium
Think Data
Feb 22 137 5
https://medium.com/endtoenddata/how-to-optimize-long-running-queries-in-sql-bd797ae9dfd6 22/24
3/5/25, 10:06 AM How to Optimize Long-Running Queries in SQL? | by Prem Vishnoi(cloudvala) | EndToEndData | Feb, 2025 | Medium
Feb 24 22
Yousef Yousefi
Feb 19 2
Data Layout Approach: A Modern Approach to Scalable Data Lakehouse Design and
Understanding with Databricks notebook
Feb 15 108
https://medium.com/endtoenddata/how-to-optimize-long-running-queries-in-sql-bd797ae9dfd6 24/24