TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst · 76.7K subscribers
Post #2813 422
Don't retrieve every transaction and filter it later in Python or Excel if the database can efficiently perform the filtering.

A good principle is:

«Let the database do the data filtering and aggregation whenever practical.»

24️⃣ Execution Plans

One of the most important tools for understanding SQL performance is the execution plan.

An execution plan shows how the database intends to execute your query.

It can reveal things such as:

• Table Scan

• Index Scan

• Index Seek

• Join Strategy

• Sort

• Aggregation

• Estimated Rows

• Actual Rows

• Cost

Different databases use different terminology.

25️⃣ EXPLAIN

Many SQL databases support "EXPLAIN".

For example:

EXPLAIN
SELECT *
FROM orders
WHERE customer_id = 101;


This lets you inspect the planned execution strategy.

Some databases support:

EXPLAIN ANALYZE

which can provide information about actual execution as well.

Exact syntax and output vary by database.

26️⃣ Table Scan vs Index Access

Imagine a table with:

10,000,000 rows

A table scan may mean the database reads a large portion of the table to find matching records.

Conceptually:

10 million rows

↓

Check rows

↓

Find matching rows

An index-based access path may instead look more like:

Index

↓

Locate matching keys

↓

Fetch relevant rows

For highly selective queries, the second approach can be much more efficient.

But if a query needs a large percentage of the table, scanning the table may actually be more efficient.

This is why the optimizer chooses the execution strategy.

27️⃣ Selectivity

Selectivity describes how effectively a condition narrows down the data.

Consider:

WHERE customer_id = 100245

If customer IDs are unique, this may return one row.

Highly selective.

Now consider:

WHERE country = 'India'

If 60% of the table contains Indian customers, the condition is much less selective.

The database may decide that scanning the table is cheaper than using an index.

Therefore:

«An index isn't automatically useful simply because the column appears in WHERE.»

28️⃣ Indexes on Low-Cardinality Columns

Suppose:

status

contains only:

• Active

• Inactive

That's a low-cardinality column.

An index may not always provide a large benefit if most rows match the condition.

For example:

WHERE status = 'Active'

If 95% of rows are Active, reading the index and then retrieving almost the entire table may be less efficient than scanning the table.

Again, the optimizer makes the decision.

29️⃣ Real-World Example

Suppose an orders table contains:

50 million rows

Analysts frequently run:

SELECT
order_id,
order_date,
order_amount
FROM orders
WHERE customer_id = 12345
AND order_date >= DATE '2026-01-01';


A possible candidate is:

CREATE INDEX idx_orders_customer_date
ON orders(customer_id, order_date);
More from @sqlanalyst
  1. Sep 29, 2026SQL Interview Series — Part 2 📌 Question 2: Find Duplicate Records Suppose you have an Em…
  2. Sep 29, 2026𝗙𝗥𝗘𝗘 𝗥𝗲𝘀𝗼𝘂𝗿𝗰𝗲𝘀 𝗧𝗼 𝗟𝗲𝗮𝗿𝗻 𝗔𝗜 𝗶𝗻 𝟮𝟬𝟮𝟲🚀 ​ Explore 6 free resource…
  3. Sep 29, 2026SQL Interview Series — Part 1 Hi guys, let's start a SQL interview series covering frequen…
  4. Sep 28, 2026🧠 Real-World SQL Scenario-Based Questions & Answers 1. Get the 2nd highest salary from th…
  5. Sep 28, 2026🎓 𝗛𝗔𝗥𝗩𝗔𝗥𝗗 𝗨𝗡𝗜𝗩𝗘𝗥𝗦𝗜𝗧𝗬 𝗙𝗥𝗘𝗘 𝗢𝗡𝗟𝗜𝗡𝗘 𝗖𝗢𝗨𝗥𝗦𝗘𝗦 😍 Dreaming of…
  6. Sep 27, 2026Data Analytics Roadmap | |-- Fundamentals | |-- Mathematics | | |-- Descriptive Statistics…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →