Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
Free tutorials in Hindi & English Daily computer, mobile and IT guides Beginner friendly learning
Blog · MySQL / SQL · 04 Jul 2026 · Hindi + English

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)

You want "Photosynthesis" in a 500-page biology book. Without an index page, you flip through all 500 pages one by one. With the index page at the back — "Photosynthesis ... page 214" — you jump straight there. A database index is exactly this: a sorted lookup structure that lets MySQL jump to matching rows instead of reading the entire table. Reading the whole table is called a full table scan — the 500-page flip.

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

If indexes only helped, we would index every column. We don't, because every INSERT, UPDATE and DELETE must also update every index on that table. Like a librarian who must correct the index page every time a book moves. Ten indexes on a busy table = ten extra writes per change = slow inserts. Indexes also consume disk space.
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), JOIN columns (foreign keys like roll_no in fees), columns in ORDER BY of 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 वही चीज़ है)

500 pages की biology किताब में आपको "Photosynthesis" चाहिए. Index page के बिना सारे 500 pages एक-एक करके पलटेंगे. पीछे के index page से — "Photosynthesis ... page 214" — सीधे वहीं पहुंचेंगे. Database index बिल्कुल यही है: एक sorted lookup structure जिससे MySQL पूरी table पढ़ने की बजाय matching rows पर सीधे कूदता है. पूरी table पढ़ना full table scan कहलाता है — वही 500-page वाली पलटाई.

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 मुफ्त नहीं

अगर indexes से सिर्फ फायदा होता, तो हर column index कर देते. नहीं करते, क्योंकि हर INSERT, UPDATE और DELETE को उस table का हर index भी update करना पड़ता है. जैसे librarian को हर किताब हिलने पर index page ठीक करना पड़े. Busy table पर दस indexes = हर change पर दस extra writes = धीमे inserts. Indexes disk space भी खाते हैं.
Reads (SELECT)Writes (INSERT/UPDATE/DELETE)
Index के साथ✅ बहुत तेज़❌ धीमे (index maintenance)
Index के बिना❌ Full scan✅ तेज़

किन columns पर index बनता है?

  • इन पर बनाएं: बार-बार WHERE में आने वाले columns (name, email, mobile), JOIN columns (fees में roll_no जैसी foreign keys), heavy queries के ORDER BY columns.
  • इन पर नहीं: कम 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 हो रहा है.