4.4.15 · HinglishDatabases

EXPLAIN — reading query plans, cost estimation

2,260 words10 min readRead in English

4.4.15 · Coding › Databases


Query plan KYA hota hai?

Figure — EXPLAIN — reading query plans, cost estimation

Postgres plan KAISE padhein

Ek typical line kuch aisi dikhti hai:

Seq Scan on orders  (cost=0.00..18334.00 rows=1000000 width=33)
                                                  (actual time=0.01..95.2 rows=1000000 loops=1)
  Filter: (amount > 500)
  Rows Removed by Filter: 200000

Ise decode karo:

Field Meaning
Seq Scan on orders operator + uska table
cost=0.00..18334.00 startup cost .. total cost (abstract units)
rows=1000000 is node se estimated rows emit honge
width=33 estimated bytes per row
actual time=0.01..95.2 real ms: startup..total (sirf ANALYZE ke saath)
loops=1 node kitni baar run hua

DO costs kyun: startup vs total

LIMIT ke liye yeh important hai! LIMIT 1 ke saath, low startup cost wala plan jeet jaata hai — chahe uska total cost zyada ho — kyunki tum ek row ke baad ruk jaate ho.


Cost formula ko pehle principles se derive karna

Postgres yeh planner constants expose karta hai (defaults dikhaye gaye hain):

Constant Default Meaning
seq_page_cost ek page sequentially padhne ka cost
random_page_cost ek page randomly padhne ka cost
cpu_tuple_cost ek row process karne ka cost
cpu_operator_cost ek operator/filter evaluate karne ka cost

Seq Scan cost derive karo. Ek sequential scan har page ek baar padhta hai aur har tuple process karta hai, har ek par filter apply karta hai:

jahan = table mein pages ki sankhya, = tuples (rows) ki sankhya.

Ab Index Scan. Ek index scan ek B-tree follow karta hai (height ) phir matching rows ko heap se random page hits karke fetch karta hai:

jahan = selected rows ki sankhya.


Row estimates KAHAN se aate hain (selectivity)

Planner do key formulas use karta hai:


Worked examples


Common mistakes


Flashcards

Plain EXPLAIN kya dikhata hai vs EXPLAIN ANALYZE?
EXPLAIN run kiye bina estimated cost/rows dikhata hai; EXPLAIN ANALYZE actually execute karta hai aur estimated vs actual rows aur real time dikhata hai.
cost=0.00..18334.00 mein do numbers kya hain?
Startup cost (0.00 = pehli row se pehle kaam) aur total cost (18334 = aakhri row tak kaam).
Sort node ka startup cost zyada kyun hota hai?
Pehli sorted row emit karne se pehle usse saari input rows consume karni padti hain.
Seq Scan cost formula likhو.
— pages×seq_page_cost + rows×(cpu_tuple_cost+cpu_operator_cost).
Default seq_page_cost aur random_page_cost?
1.0 aur 4.0 — random page reads sequential se 4× mahange hote hain.
Index scan seq scan ko kab beat karta hai?
Jab selectivity low ho (kam matching rows ), toh kam random page fetches saare pages sequentially padhne se better hote hain.
Equality selectivity kaise estimate hoti hai?
, uniform distribution assume karke.
Ek node ka estimated output rows = ?
selectivity × input rows ().
Estimates ke liye use hone wali statistics refresh karne ka command kaunsa hai?
ANALYZE (pg_statistic ko n_distinct, MCVs, histogram se populate karta hai).
Postgres plan tree kis direction mein padhte hain?
Bottom-up / inside-out: sabse zyada indented leaf nodes pehle execute hote hain aur apne parents ko feed karte hain.
Ek node rows=10 ... actual rows=900000 dikhata hai. Diagnosis kya hai?
Severe misestimation (stale/insufficient stats); bure plan ka likely cause — re-ANALYZE karo.
Kya cost= alag queries/machines mein compare kiya ja sakta hai?
Nahi — sirf same query ke alternative plans ke beech same config par; yeh ek abstract unit hai, time nahi.

Recall Feynman: ek 12-saal ke bachche ko samjhao

Tum ek giant library se "dragons ke baare mein saari kitaabein" maangte ho. Librarian ya toh har shelf ke paas se chalna choose kar sakta hai (slow par steady), ya card catalog use karke seedha dragon books par jump karna (fast agar sirf kuch hi hain). EXPLAIN tumhara woh plan dekhna hai jo librarian shuru karne se pehle banata hai: "Kya tum har shelf par chaloge? Tumhare khayal mein kitni kitaabein milegi?" Agar librarian guess karta hai "2 kitaabein" lekin actually 900,000 hain, unhone ek bakwaas plan banaya — aur tum unhe bolte ho ki shelves par kya hai phir se count karo (yahi hai ANALYZE). "Cost" bas librarian ka andaza hai ki trip kitni thaka dene wali hogi, minutes nahi.


Connections

  • B-Tree Indexes — index scans ka cost kya hota hai (tree descent + heap fetch)
  • Database Statistics & ANALYZE — selectivity aur row estimates kahan se aate hain
  • Join Algorithms — Nested Loop, Hash, Merge — inner nodes jinke costs EXPLAIN reveal karta hai
  • Selectivity & Cardinality Estimationrows= ke peeche ka math
  • Query Optimizer — woh component jo woh plan produce karta hai jo EXPLAIN print karta hai
  • Sequential vs Random I/O — physical reason kyun random_page_cost > seq_page_cost
  • LIMIT and Pagination — kyun startup cost matter karta hai jab tum jaldi rok dete ho

Concept Map

hands off to

chooses

read data

transform rows

shows estimated

runs and compares

reveals estimate vs actual

each node has

split into

split into

low value wins for

weighted by

SELECT declares WHAT

Query planner optimizer

Query plan tree

EXPLAIN

EXPLAIN ANALYZE

Leaf nodes scans

Inner nodes joins sorts

Startup cost

Total cost

Abstract cost units

LIMIT queries