top of page

Welcome
to NumpyNinja Blogs

NumpyNinja: Blogs. Demystifying Tech,

One Blog at a Time.
Millions of views. 

Analyzing And Interpreting Query Execution Plans

Jan 31, 2025
2 min read

Databases created some plans or a specific path to optimize queries and minimize resource uses. The query optimizer attempts to determine the most efficient way to execute a given query by considering the possible query plan. Understanding that plan can help us to optimize query performance. The execution plans provide information like estimated cost, sequence of operations, data flow, operators, statistics and join algorithms.


Let's understand this with following query example.


Image By Author
Image By Author

Query execution always starts with FROM and JOIN clauses not from SELECT. This is where we choose tables to work with and specify how to join them.

In above query we are using customers table and join it with orders table using common id, in this case customer_id.


Now using INDEX on joining column can significantly improve query performance. INDEX type such as B-Tree and Bitmap Indexes can impact performance based on data distribution and query type.


B-Tree Index
B-Tree Index

Bitmap Index
Bitmap Index

Now we move to WHERE clause, this combines data by applying specific conditions. In above example we consider data on or after 1st January 2020.


Note: It's important to write a SARGable queries to leverage indexes effectively.

SARGable stands for Search ARGument able. It refers to the queries that can use indexes for faster execution.


Examples of Non-SARGable(Bad) and SARGable(Fixed)
Examples of Non-SARGable(Bad) and SARGable(Fixed)

Let's understand with our example,

In SARGable queries we directly compare order_date column to a specific date. This allows the database engine to use an index on order_date column to quickly filter out required condition.


In Non-SARGable queries which used just a YEAR function on order_date column. This prevent database engine using index on order_date because the function must be apply to every row in the table even if the index exist. So Non-SARGable queries will be slower because they have lot more records to scan.


Next GROUP BY and HAVING clause, In our query we are grouping the records by customer_id and filtering group spend by condition total_spend >= 2000. This query find those who have spend more or equal to 2000 on orders.


Then comes SELECT clause it defines which columns we want in our set. In this case customer_id, order_id as total_orders, order_amount as total_spend. Even though SELECT  comes first in SQL query it is pretty far down in query processing order.


NOTE: When optimizes SELECT clauses consider using covering indexes they include all the columns needed for the query specially in SELECT, WHERE and JOIN clause.

This unable database engine to retrieve data directly from index. This speedup query execution.


Finally we have ORDER BY and LIMIT, to optimize those clauses consider using a smaller dataset with filtering and pagination. For large datasets avoid sorting the entire dataset, It can lead to high memory uses and slow response times also use appropriate indexes to speed up of sorting and reduce the amount of data sorted in memory.


Image By Author
Image By Author

In conclusion: "With all above knowledge we can tackle more complex SQL queries and optimize them for better performance."


Thank you for reading my blog , I hope you find it helpful. Keep Reading...!!!


 
 

+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