MySQL में Index: क्या है, कैसे काम करता है, कब नुकसान करता है
Index किताब के index page जैसा काम करता है — पूरी table पढ़ने की बजाय सीधे row पर पहुंचो. CREATE INDEX syntax, EXPLAIN से proof, और write-cost tradeoff.
The book-index analogy (it is literally the same thing)
Creating an index
-- Our situation: 1,00,000 students, we constantly search by name
SELECT * FROM students WHERE name = 'Aman Kumar';
-- Without index: MySQL checks all 1,00,000 rows. Every. Single. Time.
CREATE INDEX idx_name ON students(name);
-- Now MySQL keeps names sorted in a side structure (a B-tree)
-- and finds 'Aman Kumar' in a handful of jumps.
Note: some indexes you already have without knowing — PRIMARY KEY and UNIQUE columns are automatically indexed. That is why searching by roll_no was always fast.
Proof with EXPLAIN — see MySQL's plan
EXPLAIN SELECT * FROM students WHERE name = 'Aman Kumar';
-- BEFORE index:
-- type: ALL rows: 100000 <- full table scan, all rows!
-- AFTER CREATE INDEX idx_name:
-- type: ref rows: 1 <- jumped straight to the row
-- key : idx_name
EXPLAIN is your X-ray machine: put it before any SELECT and MySQL shows whether it will scan everything (type: ALL — bad on big tables) or use an index (ref/range — good).
The price you pay — indexes are not free
| Reads (SELECT) | Writes (INSERT/UPDATE/DELETE) | |
|---|---|---|
| With index | ✅ Much faster | ❌ Slower (index maintenance) |
| Without index | ❌ Full scan | ✅ Faster |
Which columns deserve an index?
- Index these: columns in frequent
WHERE(name, email, mobile),JOINcolumns (foreign keys like roll_no in fees), columns inORDER BYof heavy queries. - Don't index: columns rarely searched; tiny tables (scan is already instant); columns with very few distinct values (a gender column with 2 values — the index barely narrows anything).
- Composite index tip:
CREATE INDEX idx_cs ON students(class, section)serves queries filtering by class alone or class+section — but NOT section alone (leftmost-prefix rule, a favourite advanced question). - Interview line: "An index is a sorted B-tree lookup that trades slower writes and extra storage for dramatically faster reads on searched columns."
किताब के index वाली analogy (यह literally वही चीज़ है)
Index बनाना
-- हमारी situation: 1,00,000 students, बार-बार name से search
SELECT * FROM students WHERE name = 'Aman Kumar';
-- बिना index: MySQL सारी 1,00,000 rows check करता है. हर. एक. बार.
CREATE INDEX idx_name ON students(name);
-- अब MySQL names को एक side structure (B-tree) में sorted रखता है
-- और 'Aman Kumar' को गिनती के jumps में ढूंढ लेता है.
Note: कुछ indexes आपके पास बिना जाने पहले से हैं — PRIMARY KEY और UNIQUE columns automatically indexed होते हैं. इसीलिए roll_no से search हमेशा तेज़ थी.
EXPLAIN से proof — MySQL का plan देखिए
EXPLAIN SELECT * FROM students WHERE name = 'Aman Kumar';
-- Index se PEHLE:
-- type: ALL rows: 100000 <- full table scan, सारी rows!
-- CREATE INDEX idx_name ke BAAD:
-- type: ref rows: 1 <- सीधे row पर पहुंचा
-- key : idx_name
EXPLAIN आपकी X-ray machine है: किसी भी SELECT के आगे लगाइए और MySQL बताता है कि वह सब scan करेगा (type: ALL — बड़ी tables पर बुरा) या index use करेगा (ref/range — अच्छा).
कीमत भी है — indexes मुफ्त नहीं
| Reads (SELECT) | Writes (INSERT/UPDATE/DELETE) | |
|---|---|---|
| Index के साथ | ✅ बहुत तेज़ | ❌ धीमे (index maintenance) |
| Index के बिना | ❌ Full scan | ✅ तेज़ |
किन columns पर index बनता है?
- इन पर बनाएं: बार-बार
WHEREमें आने वाले columns (name, email, mobile),JOINcolumns (fees में roll_no जैसी foreign keys), heavy queries केORDER BYcolumns. - इन पर नहीं: कम search होने वाले columns; छोटी tables (scan वैसे ही instant है); बहुत कम distinct values वाले columns (2 values वाला gender column — index से कुछ खास घटता नहीं).
- Composite index tip:
CREATE INDEX idx_cs ON students(class, section)सिर्फ class या class+section वाली queries को serve करता है — अकेले section को NAHI (leftmost-prefix rule, favourite advanced question). - Interview line: "Index एक sorted B-tree lookup है जो थोड़े धीमे writes और extra storage के बदले searched columns पर reads को dramatically तेज़ करता है."
Frequently Asked Questions
MySQL में index आसान शब्दों में क्या है?
Index चुने हुए columns पर बना sorted lookup structure (B-tree) है, जिससे MySQL पूरी table scan करने की बजाय सीधे matching rows पर पहुंचता है — बिल्कुल किताब के index page जैसा.
Indexes का नुकसान क्या है?
हर INSERT, UPDATE और DELETE को table का हर index भी maintain करना पड़ता है, जिससे writes धीमे होते हैं; indexes extra disk space भी लेते हैं.
कैसे check करें कि MySQL मेरा index use कर रहा है?
Query के आगे EXPLAIN लगाएं — type ALL का मतलब full table scan, जबकि key column में आपके index के नाम के साथ ref या range का मतलब index use हो रहा है.