How POSTGRES indexing is more efficient than MYSQL
dev.toΒ·1dΒ·
Discuss: DEV
πŸ—ΊοΈPostgreSQL Planner
Preview
Report Post

Problem setup (How Index Scan Internally works for both DBs)

Table in MySQL and PostgreSQL:

user(
id   BIGINT PRIMARY KEY,
name VARCHAR(100),
age  INT
)

Index:

CREATE INDEX idx_user_name ON user(name);


Query:

SELECT age FROM user WHERE name = 'Rahim';


Assume:

  • Table has 10 million rows
  • Index is a B-Tree
  • name is not unique
  • age is not in the index

1️⃣ MySQL (InnoDB) β€” Step by step

πŸ”‘ Important InnoDB rule

Secondary index stores the PRIMARY KEY, not the row location

So idx_user_name looks like:

(name, primary_key_id)


Step-by-step execution

🧭 Step 1: Traverse secondary index (name)

MySQL searches the B-Tree for β€˜Rahim’.

Cost: O(log N) Example:

B-Tree levels: Root β†’ Internal β†’ Leaf

At the leaf, it finds: …

Similar Posts

Loading similar posts...

Keyboard Shortcuts

Navigation
Next / previous item
j/k
Open post
oorEnter
Preview post
v
Post Actions
Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Recommendations
Add interest / feed
Enter
Not interested
x
Go to
Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Browse
gb
Search
/
General
Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help