Index Lab
An index is a separate, sorted lookup structure MySQL maintains alongside a table. It can make matching rows much faster to find — at a real cost in storage and write speed.
انڈیکس (Index) ایک علیحدہ، ترتیب شدہ لُک اپ ڈھانچہ (Lookup Structure) ہے جو MySQL ٹیبل کے ساتھ ساتھ برقرار رکھتا ہے۔ یہ مطلوبہ ریکارڈز کو تلاش کرنے کی رفتار کو کئی گنا تیز کر سکتا ہے — تاہم اس کے بدلے اسٹوریج (Storage) اور ڈیٹا لکھنے (Write) کی رفتار پر اضافی بوجھ پڑتا ہے۔
Same query, before and after adding an index
Query: SELECT * FROM fee_invoices WHERE status = 'Overdue'
Without idx_invoices_status
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|---|---|---|---|---|---|---|---|---|---|
| 1 | SIMPLE | fee_invoices | ALL | NULL | NULL | NULL | NULL | 40 | Using where |
With idx_invoices_status
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|---|---|---|---|---|---|---|---|---|---|
| 1 | SIMPLE | fee_invoices | ref | idx_invoices_status | idx_invoices_status | 1 | const | 1 | Using index condition |
Look at the type and key columns: without the
index, MySQL's plan uses type = ALL (a full table scan — it must check every
row). With the index, key names idx_invoices_status and MySQL can
jump straight to matching rows instead of scanning the whole table.
Explain this page
- What is EXPLAIN?
- A command that asks MySQL "how would you run this query?" without actually running it for real — it returns the query plan: which index (if any) it would use, and roughly how many rows it expects to examine.
- EXPLAIN کیا ہے؟
- ایک کمانڈ جو MySQL سے پوچھتی ہے "تم یہ کیوری کیسے چلاؤ گے؟" — اسے اصل میں چلائے بغیر۔ یہ کیوری پلان (Query Plan) واپس کرتی ہے: کون سا انڈیکس (Index) استعمال ہوگا (اگر کوئی ہو)، اور تقریباً کتنی قطاریں جانچی جائیں گی۔