ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Database Systems
    COMP2114
    Progress0 / 34 topics
    Topics
    1. Basic Database Concepts2. Database Approach vs File Based System3. Database Architecture4. Three Level Schema Architecture5. Data Independence6. Relational Data Model7. Attributes8. Schemas9. Tuples10. Domains11. Relation Instances12. Keys of Relations13. Integrity Constraints14. Relational Algebra15. Selection in Relational Algebra16. Projection in Relational Algebra17. Cartesian Product in Relational Algebra18. Types of Joins19. Normalization20. Functional Dependencies21. Normal Forms22. Entity-Relationship Model23. Entity Sets24. Attributes in Entity-Relationship Model25. Relationship in Entity-Relationship Model26. Entity-Relationship Diagrams27. Structured Query Language (SQL)28. Joins in SQL29. Sub-Queries in SQL30. Grouping and Aggregation in SQL31. Concurrency Control32. Database Backup and Recovery33. Indexes34. NoSQL Systems
    COMP2114›Indexes
    Database SystemsTopic 33 of 34

    Indexes

    8 minread
    1,278words
    Intermediatelevel

    Indexes in Database Systems

    An index in a database is a data structure that improves the speed of data retrieval operations on a table at the cost of additional space and slower write operations (such as insertions, deletions, and updates). Indexes are primarily used to optimize query performance by providing a mechanism for quick lookup of data, much like an index in a book helps you quickly locate specific content.

    Indexes play a key role in improving the efficiency of query processing, especially in large databases where scanning every row would be inefficient.


    Types of Indexes

    1. Single-Level Index:

      • A simple index where the index itself is a structure that directly points to data records in the table.
      • In a single-level index, each key corresponds to a single record in the data.
    2. Multilevel Index:

      • A multilevel index is an index where the index itself is stored in multiple levels. It’s essentially an index of an index.
      • This approach is used to reduce the search time when dealing with large data sets.
    3. Clustered Index:

      • In a clustered index, the data records themselves are stored in the order of the index key.
      • There can be only one clustered index per table, as the data rows themselves are physically ordered on the disk.
      • Example: If you have a table sorted by "EmployeeID", the data rows will be stored in order by "EmployeeID".
    4. Non-Clustered Index:

      • A non-clustered index is a separate data structure from the table. It contains pointers to the data rows rather than the actual data.
      • A table can have multiple non-clustered indexes, each pointing to a different column or set of columns.
      • Example: An index on "LastName" in a "Employees" table may point to multiple rows, but the data itself is stored independently.
    5. Unique Index:

      • A unique index ensures that the values in the indexed columns are unique across the table.
      • It's commonly used to enforce the UNIQUE constraint in SQL, ensuring no duplicate values exist for that column.
    6. Composite (or Multicolumn) Index:

      • A composite index is an index that involves multiple columns.
      • It is particularly useful for queries that filter on more than one column simultaneously.
      • Example: An index on both "FirstName" and "LastName" in a table could optimize queries filtering on both these columns.
    7. Full-Text Index:

      • A full-text index is used to optimize queries that involve searching for words or phrases within large text fields.
      • This is commonly used for searching documents or large blocks of text in a database (e.g., finding documents that contain the word "database").
    8. Spatial Index:

      • A spatial index is used for storing and querying spatial data, like geographic locations or 2D shapes.
      • Example: A spatial index could help speed up queries that check for the proximity of two locations (e.g., finding stores near a certain location).
    9. Bitmap Index:

      • A bitmap index is used when a column has a low cardinality (i.e., few unique values). It creates a bitmap for each distinct value, and each bit in the bitmap represents a row in the table.
      • Example: In a table with a "Gender" column (with values "Male" and "Female"), a bitmap index would store a bitmap for each gender, indicating which rows are male or female.
    10. B-Tree Index:

      • The most common type of index used by relational databases. A B-Tree (Balanced Tree) index is a self-balancing tree data structure that maintains sorted data.
      • It allows for efficient searching, insertion, deletion, and sequential access of records.
      • Example: A B-tree index on the "EmployeeID" field would enable fast search and retrieval of employee records based on ID.
    11. Hash Index:

      • A hash index uses a hash function to map keys to a location in a hash table. It is very efficient for equality searches (e.g., retrieving a record with a specific key).
      • However, it is not effective for range queries (e.g., finding all records where a column is greater than a certain value).
      • Example: Searching for a specific user ID using a hash index.

    How Indexes Improve Query Performance

    Indexes optimize query performance primarily in the following ways:

    1. Faster Search: Instead of scanning the entire table, the database uses the index to quickly locate the row(s) that match the query conditions.

      • For example, finding a specific employee by employee ID is much faster with an index than by scanning every row in a table of employees.
    2. Efficient Sorting: Indexes help in sorting data efficiently. Queries that require sorting (e.g., ORDER BY) can benefit from existing indexes that are already sorted in a specific order.

      • Example: A query that orders records by a customer’s last name will benefit from a sorted index on "LastName".
    3. Optimized Join Operations: Indexes can speed up join operations by enabling faster lookups for matching records.

      • Example: When joining two tables on a common column, an index on that column can make the join operation much faster.
    4. Quicker Range Queries: For queries that involve ranges of values (e.g., BETWEEN, >, <), indexes allow faster searches for records within the specified range.

      • Example: A query that retrieves records where an order date is between two specific dates can use a date index to quickly find matching rows.

    Trade-offs of Using Indexes

    While indexes greatly improve query performance, they come with trade-offs:

    1. Storage Overhead:

      • Indexes consume additional storage space. The larger the table, the more storage an index will require. In some cases, this can be significant.
    2. Impact on Write Performance:

      • Insertions, deletions, and updates are slower when indexes are present, because the index must be updated along with the table data. For example, if you insert a new row, the corresponding index must also be updated.
      • Example: Inserting a record into a table with a large number of indexes will require more time than inserting into a table without indexes.
    3. Maintenance:

      • Indexes need to be maintained and can become fragmented over time. Periodically rebuilding or reorganizing indexes might be necessary to ensure they continue to perform optimally.

    When to Use Indexes

    Indexes should be used judiciously to balance performance improvements with overhead:

    1. Primary Key and Unique Constraints: Automatically creates an index to enforce uniqueness, ensuring that the table has no duplicate values.

    2. Frequently Queried Columns: If a column is often used in search conditions (e.g., WHERE clause), creating an index on that column can significantly improve performance.

    3. Columns Used for Sorting or Joining: Columns that are used for sorting (ORDER BY) or joining (JOIN) benefit from indexes.

    4. Large Tables: Indexes are especially useful in large tables where scanning all rows to answer queries would be slow.

    5. Full-Text Searches: For columns containing large amounts of text, full-text indexes can speed up searches for specific words or phrases.


    How to Create Indexes in SQL

    In SQL, you can create indexes using the CREATE INDEX statement. Here’s a simple example:

    CREATE INDEX idx_employee_id ON employees (employee_id);
    
    • This creates an index named idx_employee_id on the employee_id column in the employees table.

    To create a unique index, you would use:

    CREATE UNIQUE INDEX idx_unique_email ON employees (email);
    

    To create a composite index (on multiple columns):

    CREATE INDEX idx_fullname ON employees (first_name, last_name);
    

    Conclusion

    Indexes are a crucial tool for optimizing database query performance, especially for large databases. They improve search, sorting, and join operations, and can be used to speed up queries significantly. However, they come with trade-offs in terms of storage space and write performance. Understanding when and where to apply indexes is vital for efficient database management, ensuring that you achieve the best performance without unnecessary overhead.

    Previous topic 32
    Database Backup and Recovery
    Next topic 34
    NoSQL Systems

    Past Papers

    Open this section to load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time8 min
      Word count1,278
      Code examples0
      DifficultyIntermediate