top of page

Welcome
to NumpyNinja Blogs

NumpyNinja: Blogs. Demystifying Tech,

One Blog at a Time.
Millions of views. 

SQL Optimization

May 23, 2025
2 min read

When you are working with large datasets or complex queries, have you ever run into performance issues?

May be your query takes minutes to run, or may be its timing out entirely?

Well, I had similar experience and thats how I learnt about SQL Optimization .


In this blog, we will look some of the optimization techniques to tune SQL queries (in Postgres) which I had followed.

Why Performance Tuning Matters

When our dealing with large datasets , a poorly written query can:

  • Slow down the entire app.

  • Lock tables and block other queries.

  • Delay business decisions.

Let's look into some of the ways to tune our queries:

  1. Use EXPLAIN and EXPLAIN ANALYZE


    EXPLAIN ANALYZE

    SELECT * FROM partitioned_pregnancy_info WHERE edd_v1 >='2014-01-01' AND edd_v1 <'2015-01-01' ;

    It shows :

    • Whether PostgreSQL uses a Sequential Scan or Index Scan.

      Prefer Index scan over seq scan.

    • Explains the execution plan and cost estimates.

    • how much time each operation takes.

      Identify slow joins or large table scans.



  1. Avoid SELECT*, Instead SELECT only the required fields , wherever possible.

    Or use index-only scans by selecting only indexed columns.

    --Bad

    SELECT * FROM demographics;


    --Good

    SELECT participant_id, ethnicity FROM demographics;

  1. Keep Your Stats Updated

PostgreSQL relies on statistics to optimize queries. If your data changes often, outdated stats can ruin performance.


 Run:

bash

CopyEdit

VACUUM ANALYZE;


Or schedule autovacuum, which does this automatically.

  • VACUUM clears dead tuples

  • ANALYZE updates planner stats

  1. Partition Large Tables

    If you're working with large fact tables, consider table partitioning.


    CREATE TABLE sales (

    id SERIAL,

    region TEXT,

    sale_date DATE

    ) PARTITION BY RANGE (sale_date);

When we run a query, it will only scan the relevant partitions.

Great for time series or region based data.


  1. Avoid Nested Loops on Large Joins

    PostgreSQL defaults to nested loops for joins , but these can become very expensive.


Pro Tips

  • Use LIMIT during dev or Testing.

  • Cache Materialized views if queries are reused often.

  • Use pg_stat_statements to find slow queries.

  • Add indexes on foreign keys and JOIN keys.

  • Optimize ORDER BY with indexes + LIMIT


 
 

+1 (302) 200-8320

NumPy_Ninja_Logo (1).png

Numpy Ninja Inc. 8 The Grn Ste A Dover, DE 19901

© Copyright 2025 by Numpy Ninja Inc.

  • Twitter
  • LinkedIn
bottom of page