Class 11-12

Class 11 Computer Science

BSEB 100-Day Study Plan
Notes, MCQs, and Solutions

Explore Class 11

Class 12 Computer Science

Advanced Topics & Practical Guide
Board Exam Preparation

Explore Class 12

Day 62: Indexing and Query Optimization in SQL | Class 11, Chapter 6

Explore indexing and query optimization in SQL on Day 62(11) Computer Science. Learn types of indexing, query performance, and optimization techniques

Day 62 – Indexing and Optimization in SQL

Class 11 Computer Science – Chapter 6

What is Indexing in SQL? (SQL में Indexing क्या है?)

Indexing in SQL is a data structure technique that improves the speed of data retrieval operations on a database table by creating pointers to the data.
SQL में Indexing एक डेटा संरचना तकनीक है जो डेटाबेस टेबल पर डेटा पुनर्प्राप्ति संचालन की गति को बढ़ाती है।

Why is Indexing Important? (Indexing क्यों महत्वपूर्ण है?)

  • Speeds up query performance by reducing search time.
    खोज समय को कम करके क्वेरी प्रदर्शन में तेजी लाता है।
  • Improves the efficiency of SELECT queries.
    SELECT क्वेरी की दक्षता में सुधार करता है।
  • Reduces the workload on database servers during high traffic.
    उच्च ट्रैफिक के दौरान डेटाबेस सर्वर पर कार्यभार को कम करता है।

Types of Indexes in SQL (SQL में Indexes के प्रकार)

  • Clustered Index: Rearranges the data in the table to match the index.
    Clustered Index: इंडेक्स से मेल खाने के लिए टेबल में डेटा को पुनः व्यवस्थित करता है।
  • Non-Clustered Index: Creates a separate structure for the index without altering the table’s data.
    Non-Clustered Index: टेबल डेटा को बदले बिना इंडेक्स के लिए एक अलग संरचना बनाता है।
  • Unique Index: Ensures that no duplicate values exist in the indexed column.
    Unique Index: सुनिश्चित करता है कि इंडेक्स कॉलम में डुप्लिकेट मान न हों।
  • Composite Index: Indexes multiple columns together.
    Composite Index: कई कॉलम को एक साथ इंडेक्स करता है।

What is Query Optimization in SQL? (SQL में Query Optimization क्या है?)

Query optimization is the process of improving query performance by minimizing the time and resources required to execute a query.
Query optimization एक प्रक्रिया है जो क्वेरी प्रदर्शन को बढ़ाने और इसे निष्पादित करने में लगने वाले समय और संसाधनों को कम करने के लिए उपयोग की जाती है।

Techniques for Query Optimization (Query Optimization के लिए तकनीकें)

  • Use proper indexing on frequently searched columns.
    बार-बार खोजे जाने वाले कॉलम पर उचित इंडेक्सिंग का उपयोग करें।
  • Avoid using SELECT *; specify only the required columns.
    SELECT * का उपयोग न करें; केवल आवश्यक कॉलम को निर्दिष्ट करें।
  • Use JOINs instead of subqueries for better performance.
    बेहतर प्रदर्शन के लिए Subqueries के बजाय JOINs का उपयोग करें।
  • Analyze and rewrite queries to reduce complexity.
    जटिलता को कम करने के लिए क्वेरी को विश्लेषित और पुनः लिखें।
  • Use database query execution plans to identify bottlenecks.
    बॉटलनेक्स की पहचान करने के लिए डेटाबेस क्वेरी निष्पादन योजना का उपयोग करें।

Examples of Indexing and Optimization

1. Creating an Index

Query:


CREATE INDEX idx_name ON Employees (Name);

This creates an index on the "Name" column of the "Employees" table.

2. Using Execution Plans

Execution plans help analyze and optimize query performance.

Command:


EXPLAIN SELECT * FROM Employees WHERE Name = 'Alice';

Advantages of Indexing and Optimization (Indexing और Optimization के लाभ)

  • Improves query execution speed.
    क्वेरी निष्पादन की गति में सुधार करता है।
  • Reduces database workload.
    डेटाबेस कार्यभार को कम करता है।
  • Enhances user experience with faster responses.
    तेज़ प्रतिक्रिया के साथ उपयोगकर्ता अनुभव को बढ़ाता है।

Practice Questions

Multiple Choice Questions (MCQs)

  1. Which type of index rearranges the data in the table?
    (a) Non-Clustered | (b) Clustered | (c) Composite | (d) Unique
  2. Which query clause should be avoided for better optimization?
    (a) SELECT * | (b) WHERE | (c) ORDER BY | (d) GROUP BY
  3. What is the main purpose of indexing in SQL?
    (a) Reduce table size | (b) Improve query speed | (c) Remove duplicates | (d) Add constraints
  4. Which index type ensures no duplicate values?
    (a) Clustered | (b) Non-Clustered | (c) Unique | (d) Composite
  5. What is a Composite Index?
    (a) Index on a single column | (b) Index on multiple columns | (c) Duplicate values allowed | (d) None
  6. Which of these techniques improves query performance?
    (a) Using SELECT * | (b) Creating proper indexes | (c) Increasing table size | (d) Avoiding WHERE clause
  7. What does an execution plan do?
    (a) Executes queries | (b) Analyzes query performance | (c) Creates indexes | (d) None
  8. Which type of index creates a new structure for data lookup?
    (a) Clustered | (b) Non-Clustered | (c) Unique | (d) Composite
  9. How does indexing affect database performance?
    (a) Slows down writes | (b) Speeds up reads | (c) No effect | (d) Both (a) and (b)
  10. Which command creates an index in SQL?
    (a) CREATE INDEX | (b) EXPLAIN | (c) SELECT | (d) GRANT

Answers to MCQs:

1: (b), 2: (a), 3: (b), 4: (c), 5: (b), 6: (b), 7: (b), 8: (b), 9: (d), 10: (a)

Short Answer Questions

  1. What is indexing in SQL?
    Answer: Indexing is a technique to speed up data retrieval by creating pointers to data in a database table.
  2. What is the difference between clustered and non-clustered indexes?
    Answer: Clustered indexes rearrange table data to match the index, while non-clustered indexes create a separate structure.
  3. Explain the importance of query optimization.
    Answer: Query optimization improves performance by minimizing the time and resources needed to execute queries.
  4. Write a query to create an index on a table.
    Answer:
    
    CREATE INDEX idx_name ON Employees (Name);
    
            
  5. List two techniques for query optimization.
    Answer:
    • Use indexes on frequently queried columns.
    • Avoid using SELECT * in queries.

Long Answer Questions

  1. Explain the types of indexing in SQL with examples.
  2. Discuss the significance of query optimization and techniques to achieve it.
  3. Write SQL queries demonstrating indexing and query optimization techniques.

Post a Comment