UGC-NET, GATE, NIELIT (O/A/B Level), KVS/NVS Computer Teacher, PGDCA, BCA, MCA, B-Tech व All India IT Exam विशेष नोट्स: इस अध्याय में हम Clustered vs Non-Clustered Indexes and Covering Indexes in SQL (SQL में क्लस्टर्ड बनाम नॉन-क्लस्टर्ड और कवरिंग इंडेक्स) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और राष्ट्रीय स्तर की परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: Database indexes accelerate SQL query retrieval using B+ Tree structures at the cost of extra storage and write overhead. Distinguishing between Clustered Indexes (physical row order), Non-Clustered Indexes (separate lookup structures), and Covering Indexes (index-only scans) is crucial for SQL performance tuning.
अध्याय सार: डेटाबेस इंडेक्स अतिरिक्त स्टोरेज और राइट ओवरहेड की कीमत पर B+ ट्री संरचनाओं का उपयोग करके SQL क्वेरी को तेज करते हैं। SQL प्रदर्शन ट्यूनिंग के लिए क्लस्टर्ड इंडेक्स (भौतिक पंक्ति क्रम), नॉन-क्लस्टर्ड इंडेक्स (अलग लुकअप संरचना) और कवरिंग इंडेक्स (इंडेक्स-ओनली स्कैन) के बीच अंतर समझना अत्यंत महत्वपूर्ण है।
Clustered vs Non-Clustered Indexes and Covering Indexes in SQL: Detailed Notes & Architecture (SQL में क्लस्टर्ड बनाम नॉन-क्लस्टर्ड और कवरिंग इंडेक्स)
1. Clustered Index Architecture (क्लस्टर्ड इंडेक्स आर्किटेक्चर)
[EN] A Clustered Index defines the physical storage order of data rows in a table's B+ Tree leaf pages; therefore, a table can have at most ONE clustered index. In SQL Server and MySQL InnoDB, the Primary Key automatically creates a clustered index unless specified otherwise.
[HI] क्लस्टर्ड इंडेक्स टेबल की B+ ट्री लीफ पेजों में डेटा पंक्तियों के भौतिक स्टोरेज क्रम को परिभाषित करता है; इसलिए किसी टेबल में अधिकतम केवल एक ही क्लस्टर्ड इंडेक्स हो सकता है। SQL Server और MySQL InnoDB में प्राइमरी की डिफ़ॉल्ट रूप से क्लस्टर्ड इंडेक्स बनाती है।
2. Non-Clustered Index and Key Lookup Overhead (नॉन-क्लस्टर्ड इंडेक्स और की-लुकअप ओवरहेड)
[EN] A Non-Clustered Index builds a separate B+ Tree storing the indexed key values alongside a row pointer (RID in a heap, or the Clustered Index key). A table can have many non-clustered indexes, though fetching non-indexed columns requires an extra Key Lookup (Bookmark Lookup) into the base table.
[HI] नॉन-क्लस्टर्ड इंडेक्स एक अलग B+ ट्री बनाता है जिसमें इंडेक्स की के साथ रो पॉइंटर (RID या क्लस्टर्ड की) होता है। एक टेबल में कई नॉन-क्लस्टर्ड इंडेक्स हो सकते हैं, लेकिन गैर-इंडेक्स्ड कॉलम लाने के लिए बेस टेबल में अतिरिक्त Key Lookup करना पड़ता है।
3. Covering Indexes and INCLUDE Columns (कवरिंग इंडेक्स और INCLUDE कॉलम्स)
[EN] A Covering Index contains every column referenced by a query (in SELECT, WHERE, JOIN, ORDER BY) within the index itself, enabling an Index-Only Scan with zero base-table lookups. Using the INCLUDE clause (e.g., CREATE INDEX idx ON Orders(CustID) INCLUDE (Total, Status)) stores extra columns only at leaf pages without bloating intermediate B+ Tree nodes.
[HI] कवरिंग इंडेक्स में क्वेरी द्वारा उपयोग किए जाने वाले सभी कॉलम इंडेक्स के अंदर ही मौजूद होते हैं, जिससे बिना बेस टेबल पढ़े Index-Only Scan हो जाता है। INCLUDE क्लॉज अतिरिक्त कॉलम को केवल लीफ पेजों पर रखता है जिससे ऊपरी B+ ट्री नोड्स भारी नहीं होते।
4. Trade-offs: Write Penalty and SARGability (ट्रेड-ऑफ: राइट पेनल्टी और SARGability)
[EN] While indexes speed up SELECT queries from O(N) table scans to O(log N) seeks, every INSERT, UPDATE, and DELETE must maintain all indexes, slowing writes. Furthermore, wrapping an indexed column in a function (e.g., WHERE YEAR(OrderDate) = 2025) breaks SARGability and forces a full index/table scan.
[HI] इंडेक्स SELECT क्वेरी को O(N) से O(log N) तक तेज करते हैं, लेकिन हर INSERT, UPDATE और DELETE को सभी इंडेक्स अपडेट करने पड़ते हैं। इसके अलावा, WHERE में कॉलम पर फंक्शन लगाने (जैसे YEAR(OrderDate)) से SARGability टूटती है और इंडेक्स का उपयोग नहीं हो पाता।
Syntax, Formula & Technical Example (Clustered vs Non-Clustered Indexes and Covering Indexes in SQL)
-- Clustered Index (only 1 per table)
CREATE CLUSTERED INDEX IX_Emp_ID ON Employees(EmpID);
-- Covering Non-Clustered Index with INCLUDE
CREATE NONCLUSTERED INDEX IX_Orders_Cust_Cover
ON Orders(CustomerID) INCLUDE (OrderDate, TotalAmount);
-- Query satisfied via Index-Only Scan (no Key Lookup):
SELECT OrderDate, TotalAmount FROM Orders WHERE CustomerID = 42;
Clustered vs Non-Clustered Indexes and Covering Indexes in SQL Quick Reference & Comparison Table
| Index Type | Limit per Table | Leaf Node Contents |
|---|---|---|
| Clustered Index | Maximum 1 per table | Actual physical table data rows |
| Non-Clustered Index | Multiple (e.g., 999 in SQL Server) | Index key + Row Locator (RID or Clustered Key) |
| Covering Index | Multiple (subset of Non-Clustered) | All columns needed by query (via key + INCLUDE) |
| Heap Table | 0 clustered indexes | Unordered data pages; uses RIDs |
Clustered vs Non-Clustered Indexes and Covering Indexes in SQL Top 20 Bilingual MCQs with Answers
Q1. What is the maximum number of Clustered Indexes a single SQL table can have?
(एकल SQL टेबल में अधिकतम कितने क्लस्टर्ड इंडेक्स हो सकते हैं?)
(A) 1
(B) 2
(C) Unlimited
(D) 16
Correct Answer: (A)
[EN] Because a clustered index dictates the physical sort order of the table's rows, only one can exist per table.
[HI] चूंकि क्लस्टर्ड इंडेक्स टेबल की पंक्तियों के भौतिक क्रम को तय करता है, इसलिए प्रति टेबल केवल 1 ही क्लस्टर्ड इंडेक्स हो सकता है।
Q2. What is stored in the leaf nodes of a Clustered Index?
(क्लस्टर्ड इंडेक्स के लीफ नोड्स (leaf nodes) में क्या संग्रहीत होता है?)
(A) Foreign key constraints only
(B) Only row pointers (RIDs)
(C) Only NULL placeholders
(D) The actual data rows of the table
Correct Answer: (D)
[EN] In a clustered B+ Tree, the leaf level IS the physical table data ordered by the clustered key.
[HI] क्लस्टर्ड इंडेक्स के लीफ स्तर पर ही टेबल की वास्तविक डेटा पंक्तियाँ संग्रहीत होती हैं।
Q3. What is a 'Covering Index' in SQL?
(SQL में 'कवरिंग इंडेक्स' (Covering Index) क्या होता है?)
(A) An index that covers every table in the database
(B) An index that encrypts all columns
(C) An index automatically deleted after one query
(D) An index that includes all columns required by a query so the base table is never accessed
Correct Answer: (D)
[EN] A covering index satisfies SELECT, WHERE, and JOIN columns directly from the index pages (Index-Only Scan).
[HI] कवरिंग इंडेक्स में क्वेरी के लिए आवश्यक सभी कॉलम मौजूद होते हैं, जिससे बेस टेबल को पढ़ने की आवश्यकता नहीं पड़ती।
Q4. In SQL Server / PostgreSQL, what is the purpose of the INCLUDE clause in CREATE INDEX?
(SQL Server / PostgreSQL में CREATE INDEX में INCLUDE क्लॉज का क्या उद्देश्य है?)
(A) To automatically partition the table
(B) To force a full table scan
(C) To create a second clustered index
(D) To add non-key columns only to the leaf level to create a covering index
Correct Answer: (D)
[EN] INCLUDE stores extra payload columns at leaf pages without adding them to the B+ Tree search key.
[HI] INCLUDE गैर-की कॉलम को केवल लीफ स्तर पर जोड़ता है ताकि बिना सर्च की बढ़ाए कवरिंग इंडेक्स बन सके।
Q5. What performance issue occurs when a Non-Clustered Index finds matching keys, but must fetch extra columns from the base table?
(जब नॉन-क्लस्टर्ड इंडेक्स को की मिल जाती है, लेकिन अतिरिक्त कॉलम के लिए बेस टेबल पर जाना पड़ता है, तो कौन सी प्रक्रिया होती है?)
(A) Key Lookup (or Bookmark Lookup)
(B) Hash Collision
(C) Deadlock
(D) Cartesian Product
Correct Answer: (A)
[EN] Fetching non-covered columns requires random I/O Key Lookups into the clustered index or heap.
[HI] गैर-कवर्ड कॉलम लाने के लिए बेस टेबल में Key Lookup (या Bookmark Lookup) करना पड़ता है, जिससे I/O बढ़ता है।
Q6. Which data structure is most commonly used by relational databases (MySQL InnoDB, PostgreSQL, SQL Server) for standard indexes?
(रिलेशनल डेटाबेस (MySQL, PostgreSQL, SQL Server) द्वारा मानक इंडेक्स के लिए सामान्यतः किस डेटा स्ट्रक्चर का उपयोग किया जाता है?)
(A) B+ Tree
(B) Stack
(C) Binary Search Tree (unbalanced)
(D) Adjacency Matrix
Correct Answer: (A)
[EN] B+ Trees minimize disk I/O with high branching factors and keep all values/pointers at linked leaf pages for fast range scans.
[HI] B+ ट्री कम डिस्क I/O और तेज रेंज स्कैन के कारण मानक डेटाबेस इंडेक्स के लिए उपयोग होता है।
Q7. What is a table without any Clustered Index called in SQL Server?
(SQL Server में बिना किसी क्लस्टर्ड इंडेक्स वाली टेबल को क्या कहा जाता है?)
(A) View
(B) Cluster
(C) Heap
(D) Materialized Table
Correct Answer: (C)
[EN] A table stored without a clustered index has unordered pages and is called a Heap.
[HI] बिना क्लस्टर्ड इंडेक्स वाली टेबल की पंक्तियाँ अव्यवस्थित होती हैं और इसे Heap कहा जाता है।
Q8. In a table with a Clustered Index, what does a Non-Clustered Index store as its row locator?
(क्लस्टर्ड इंडेक्स वाली टेबल में, नॉन-क्लस्टर्ड इंडेक्स रो लोकेटर के रूप में क्या स्टोर करता है?)
(A) The user's IP address
(B) The physical hard drive sector number
(C) A duplicate copy of the entire row
(D) The Clustered Index key
Correct Answer: (D)
[EN] Non-clustered indexes point to the Clustered Index key (or RID if the table is a Heap).
[HI] नॉन-क्लस्टर्ड इंडेक्स रो लोकेटर के रूप में क्लस्टर्ड इंडेक्स की (Clustered Key) स्टोर करता है।
Q9. Why does adding too many indexes degrade database performance?
(बहुत अधिक इंडेक्स जोड़ने से डेटाबेस का प्रदर्शन क्यों खराब होता है?)
(A) Indexes delete primary keys
(B) Indexes disable ACID transactions
(C) SELECT queries stop working after 3 indexes
(D) Every INSERT, UPDATE, and DELETE must update all affected indexes, increasing write overhead
Correct Answer: (D)
[EN] Each write operation incurs extra B+ Tree maintenance and disk space consumption across all indexes.
[HI] प्रत्येक INSERT, UPDATE और DELETE को सभी संबंधित इंडेक्स अपडेट करने पड़ते हैं, जिससे राइट ओवरहेड बढ़ता है।
Q10. Which WHERE clause is SARGable (can efficiently use an Index Seek on OrderDate)?
(निम्नलिखित में से कौन सा WHERE क्लॉज SARGable है (OrderDate पर इंडेक्स सीक का कुशल उपयोग कर सकता है)?)
(A) WHERE OrderDate + 1 = '2025-01-02'
(B) WHERE YEAR(OrderDate) = 2025
(C) WHERE OrderDate >= '2025-01-01' AND OrderDate < '2026-01-01'
(D) WHERE CAST(OrderDate AS VARCHAR) LIKE '2025%'
Correct Answer: (C)
[EN] Applying functions or arithmetic to the indexed column prevents B+ Tree index seek; range predicates keep it SARGable.
[HI] इंडेक्स्ड कॉलम पर YEAR() या CAST() लगाने से इंडेक्स सीक रुक जाता है; सीधी रेंज तुलना SARGable रहती है।
Q11. In MySQL InnoDB, if you do not explicitly define a Primary Key or Unique NOT NULL index, what does InnoDB use as the Clustered Index?
(MySQL InnoDB में यदि आप प्राइमरी की परिभाषित नहीं करते हैं, तो InnoDB क्लस्टर्ड इंडेक्स के रूप में क्या उपयोग करता है?)
(A) It refuses to create the table
(B) It creates a Hash index instead
(C) A hidden internal 6-byte ROW_ID (GEN_CLUST_INDEX)
(D) It uses the last VARCHAR column
Correct Answer: (C)
[EN] InnoDB always stores data in a clustered B+ Tree, falling back to a hidden 6-byte synthetic ROW_ID if no key exists.
[HI] InnoDB हमेशा क्लस्टर्ड B+ ट्री का उपयोग करता है और कोई की न होने पर एक छिपा हुआ 6-बाइट ROW_ID बनाता है।
Q12. Which index type is ideal for equality lookups (=) on in-memory tables but cannot support range queries (<, >, BETWEEN)?
(कौन सा इंडेक्स प्रकार समानता (=) खोज के लिए आदर्श है लेकिन रेंज क्वेरी (<, >, BETWEEN) का समर्थन नहीं करता?)
(A) Clustered Index
(B) Hash Index
(C) B+ Tree Index
(D) Covering B-Tree Index
Correct Answer: (B)
[EN] Hash indexes compute O(1) bucket hashes for exact matches (=) but have no sorted ordering for range scans.
[HI] हैश इंडेक्स सटीक मिलान (=) के लिए तेज है लेकिन क्रमबद्ध न होने के कारण रेंज क्वेरी का समर्थन नहीं करता।
Q13. Given a composite index on (LastName, FirstName), which query can efficiently use the index via the Leftmost Prefix Rule?
((LastName, FirstName) पर बने कंपोजिट इंडेक्स के लिए, लेफ्टमोस्ट प्रीफिक्स नियम से कौन सी क्वेरी इसका कुशल उपयोग कर सकती है?)
(A) WHERE FirstName = 'Amit'
(B) WHERE FirstName LIKE '%mit'
(C) WHERE LastName = 'Sharma'
(D) WHERE Age = 30
Correct Answer: (C)
[EN] A composite B+ Tree is sorted first by LastName; filtering only on FirstName skips the leading key and cannot seek efficiently.
[HI] कंपोजिट इंडेक्स पहले LastName से क्रमबद्ध होता है, इसलिए केवल LastName वाली क्वेरी ही लेफ्टमोस्ट प्रीफिक्स नियम से इंडेक्स का उपयोग कर सकती है।
Q14. What does 'Index Fragmentation' refer to in relational databases?
(रिलेशनल डेटाबेस में 'इंडेक्स फ्रेगमेंटेशन' (Index Fragmentation) का क्या अर्थ है?)
(A) Logical page order no longer matches physical disk order due to page splits from inserts/updates
(B) An index has too many columns
(C) Two tables share the same primary key
(D) The SQL syntax has a typo
Correct Answer: (A)
[EN] Frequent random inserts cause B+ Tree page splits, leading to external fragmentation and wasted space, fixed by REBUILD/REORGANIZE.
[HI] बार-बार इंसर्ट/अपडेट से B+ ट्री पेज स्प्लिट होते हैं जिससे लॉजिकल और फिजिकल क्रम बिगड़ जाता है।
Q15. Which SQL command removes an existing index named IX_Emp_Name from table Employees in SQL Server?
(SQL Server में Employees टेबल से IX_Emp_Name नामक इंडेक्स हटाने के लिए किस कमांड का उपयोग होता है?)
(A) REMOVE INDEX Employees.IX_Emp_Name;
(B) DROP INDEX IX_Emp_Name ON Employees;
(C) DELETE INDEX IX_Emp_Name;
(D) TRUNCATE INDEX IX_Emp_Name;
Correct Answer: (B)
[EN] SQL Server uses DROP INDEX index_name ON table_name; (whereas PostgreSQL uses DROP INDEX index_name;).
[HI] SQL Server में इंडेक्स हटाने के लिए DROP INDEX IX_Emp_Name ON Employees; का उपयोग होता है।
Q16. Which of the following statements about SQL indexes is NOT true?
(SQL इंडेक्स के बारे में निम्नलिखित में से कौन सा कथन सत्य नहीं है?)
(A) B+ Tree indexes support both equality and range queries
(B) A Covering Index can eliminate Key Lookups
(C) Non-Clustered Indexes take additional disk space
(D) A single table can have multiple Clustered Indexes to sort data in different physical orders simultaneously
Correct Answer: (D)
[EN] A table's rows can only be physically stored in one order, so at most one Clustered Index is permitted per table.
[HI] एक टेबल की पंक्तियों को भौतिक रूप से केवल एक ही क्रम में रखा जा सकता है, इसलिए एक से अधिक क्लस्टर्ड इंडेक्स संभव नहीं हैं।
Q17. Within Clustered vs Non-Clustered Indexes and Covering Indexes in SQL, which index type directly maps to: "Maximum 1 per table"?
(Clustered vs Non-Clustered Indexes and Covering Indexes in SQL में कौन सा Index Type सीधे "Maximum 1 per table" से संबंधित है?)
(A) Heap Table
(B) Clustered Index
(C) Covering Index
(D) Non-Clustered Index
Correct Answer: (B)
[EN] In Clustered vs Non-Clustered Indexes and Covering Indexes in SQL, 'Clustered Index' handles 'Maximum 1 per table' (Actual physical table data rows).
[HI] Clustered vs Non-Clustered Indexes and Covering Indexes in SQL में 'Clustered Index' का कार्य/विवरण 'Maximum 1 per table' (Actual physical table data rows) है।
Q18. Which option accurately describes 'Non-Clustered Index' in Clustered vs Non-Clustered Indexes and Covering Indexes in SQL?
(Clustered vs Non-Clustered Indexes and Covering Indexes in SQL के संदर्भ में 'Non-Clustered Index' की सही विशेषता कौन सी है?)
(A) 0 clustered indexes
(B) Maximum 1 per table
(C) Multiple (e.g., 999 in SQL Server)
(D) Multiple (subset of Non-Clustered)
Correct Answer: (C)
[EN] 'Non-Clustered Index' provides 'Multiple (e.g., 999 in SQL Server)' — key point: Index key + Row Locator (RID or Clustered Key).
[HI] 'Non-Clustered Index' का मुख्य विवरण 'Multiple (e.g., 999 in SQL Server)' है (Index key + Row Locator (RID or Clustered Key))।
Q19. In Clustered vs Non-Clustered Indexes and Covering Indexes in SQL, identify the correct parameter for the specification: "Multiple (subset of Non-Clustered)".
(Clustered vs Non-Clustered Indexes and Covering Indexes in SQL में "Multiple (subset of Non-Clustered)" विवरण किस विकल्प के लिए सही है?)
(A) Non-Clustered Index
(B) Clustered Index
(C) Heap Table
(D) Covering Index
Correct Answer: (D)
[EN] 'Covering Index' corresponds to 'Multiple (subset of Non-Clustered)' (All columns needed by query (via key + INCLUDE)).
[HI] 'Covering Index' का संबंध 'Multiple (subset of Non-Clustered)' (All columns needed by query (via key + INCLUDE)) से है।
Q20. Considering Clustered vs Non-Clustered Indexes and Covering Indexes in SQL, what key behavior defines 'Heap Table'?
(Clustered vs Non-Clustered Indexes and Covering Indexes in SQL में 'Heap Table' की मुख्य भूमिका या मान क्या है?)
(A) Multiple (subset of Non-Clustered)
(B) 0 clustered indexes
(C) Multiple (e.g., 999 in SQL Server)
(D) Maximum 1 per table
Correct Answer: (B)
[EN] 'Heap Table' represents '0 clustered indexes' (Unordered data pages; uses RIDs).
[HI] 'Heap Table' का अर्थ/मान '0 clustered indexes' है (Unordered data pages; uses RIDs)।
Clustered vs Non-Clustered Indexes and Covering Indexes in SQL — Exam FAQs
Q1: Why should a Clustered Index key be narrow, unique, and ever-increasing (like INT IDENTITY)?
क्लस्टर्ड इंडेक्स की को छोटा, यूनिक और लगातार बढ़ने वाला (जैसे INT IDENTITY) क्यों होना चाहिए?
[EN] Because every Non-Clustered Index embeds the Clustered Key as its pointer, and ever-increasing keys append to the end of the B+ Tree without causing costly page splits.
[HI] क्योंकि हर नॉन-क्लस्टर्ड इंडेक्स अपने पॉइंटर के रूप में क्लस्टर्ड की को स्टोर करता है, और लगातार बढ़ने वाली की पेज स्प्लिट से बचाती है।
Q2: Does PostgreSQL support Clustered Indexes like SQL Server?
क्या PostgreSQL SQL Server की तरह क्लस्टर्ड इंडेक्स का समर्थन करता है?
[EN] Not automatically; PostgreSQL's CLUSTER command performs a one-time physical reorder of a heap table based on an index, but future inserts are not kept in that order.
[HI] स्वचालित रूप से नहीं; PostgreSQL में CLUSTER कमांड केवल एक बार टेबल को भौतिक रूप से पुनर्व्यवस्थित करता है, लेकिन नए इंसर्ट के बाद क्रम नहीं बनाए रखता।
Q3: When does the query optimizer ignore an index and choose a Full Table Scan instead?
क्वेरी ऑप्टिमाइज़र इंडेक्स को अनदेखा करके फुल टेबल स्कैन कब चुनता है?
[EN] When the query returns a large percentage of rows (low selectivity) or the table is very small, making sequential I/O cheaper than random index lookups.
[HI] जब क्वेरी टेबल का बड़ा हिस्सा लौटाती है (कम सेलेक्टिविटी) या टेबल बहुत छोटी होती है, तब सीक्वेंशियल स्कैन तेज होता है।
Post a Comment
Post a Comment