"Indexing Mastery: Unlocking Efficient Data Retrieval"
8.2 Bitmap Index, Full Text Index & Index Design (Covering Index, Selectivity)
In this masterclass, we will delve into the world of indexing in databases, focusing on bitmap indexes, full-text indexes, and index design. We will explore the concepts of covering indexes and selectivity, and provide examples to illustrate their use.
Bitmap Index
A bitmap index is a type of index that uses a series of bits to represent the values in a column. Each bit corresponds to a specific value, and a 1 indicates that the value is present in the row, while a 0 indicates that it is not.
How Bitmap Indexes Work
Here's an example of how a bitmap index works:
Suppose we have a table employees with a column department that can take on the values 'Sales', 'Marketing', 'IT', and 'HR'. We create a bitmap index on the department column.
| Employee ID | Department | | --- | --- | | 1 | Sales | | 2 | Marketing | | 3 | IT | | 4 | HR | | 5 | Sales |
The bitmap index would be represented as follows:
| Value | Bit Representation | | --- | --- | | Sales | 10100 | | Marketing | 01000 | | IT | 00100 | | HR | 00010 |
In this example, the bit representation for 'Sales' is 10100, indicating that employees 1 and 5 are in the Sales department.
Advantages of Bitmap Indexes
Bitmap indexes have several advantages:
- They are very compact, making them ideal for columns with a small number of distinct values.
- They are very fast for queries that filter on the indexed column.
- They can be used for efficient aggregation queries.
Disadvantages of Bitmap Indexes
However, bitmap indexes also have some disadvantages:
- They can be slow for queries that require a range scan.
- They can be slow for queries that require a sort.
Creating a Bitmap Index
To create a bitmap index in SQL, you can use the following syntax:
CREATE BITMAP INDEX idx_department ON employees (department);
Full-Text Index
A full-text index is a type of index that is optimized for searching text data. It is used to support full-text search queries, such as searching for a specific phrase or word in a text column.
How Full-Text Indexes Work
Full-text indexes work by creating an inverted index of the text data. An inverted index is a data structure that maps each word or phrase to a list of documents that contain it.
Here's an example of how a full-text index works:
Suppose we have a table articles with a column text that contains the text of each article. We create a full-text index on the text column.
| Article ID | Text | | --- | --- | | 1 | The quick brown fox jumps over the lazy dog. | | 2 | The sun was shining brightly in the clear blue sky. | | 3 | The big red car drove down the street. |
The full-text index would be represented as follows:
| Word | Article IDs | | --- | --- | | quick | 1 | | brown | 1 | | fox | 1 | | sun | 2 | | shining | 2 | | big | 3 | | red | 3 |
In this example, the word 'quick' is mapped to article 1, which contains the phrase 'The quick brown fox jumps over the lazy dog.'.
Advantages of Full-Text Indexes
Full-text indexes have several advantages:
- They support efficient full-text search queries.
- They can be used for efficient filtering and aggregation queries.
Disadvantages of Full-Text Indexes
However, full-text indexes also have some disadvantages:
- They can be slow for queries that require a range scan.
- They can be slow for queries that require a sort.
Creating a Full-Text Index
To create a full-text index in SQL, you can use the following syntax:
CREATE FULLTEXT INDEX idx_text ON articles (text);
Index Design
Index design is the process of selecting the most effective indexes for a database. The goal of index design is to minimize the number of disk I/O operations required to execute queries, while also minimizing the storage space required for the indexes.
Covering Index
A covering index is an index that includes all the columns required to satisfy a query. A covering index can be used to avoid the need to access the underlying table, which can improve performance.
Here's an example of a covering index:
Suppose we have a table employees with columns employee_id, name, and department. We create a covering index on the employee_id and name columns.
| Employee ID | Name | Department | | --- | --- | --- | | 1 | John Smith | Sales | | 2 | Jane Doe | Marketing | | 3 | Bob Johnson | IT |
The covering index would be represented as follows:
| Employee ID | Name | | --- | --- | | 1 | John Smith | | 2 | Jane Doe | | 3 | Bob Johnson |
In this example, the covering index includes all the columns required to satisfy a query that filters on employee_id and name.
Selectivity
Selectivity is a measure of how well an index can filter out rows that do not match a query. An index with high selectivity is more effective at filtering out rows, which can improve performance.
Here's an example of selectivity:
Suppose we have a table employees with a column department that can take on the values 'Sales', 'Marketing', 'IT', and 'HR'. We create an index on the department column.
| Employee ID | Department | | --- | --- | | 1 | Sales | | 2 | Marketing | | 3 | IT | | 4 | HR | | 5 | Sales |
The selectivity of the index would be calculated as follows:
Selectivity = (Number of unique values) / (Total number of rows) = (4) / (5) = 0.8
In this example, the selectivity of the index is 0.8, which means that the index can filter out 80% of the rows that do not match a query.
Best Practices for Index Design
Here are some best practices for index design:
- Create indexes on columns that are frequently used in WHERE, JOIN, and ORDER BY clauses.
- Create covering indexes to avoid the need to access the underlying table.
- Use selectivity to determine the effectiveness of an index.
- Avoid creating indexes on columns with low selectivity.
- Avoid creating indexes on columns that are frequently updated.
By following these best practices, you can create effective indexes that improve the performance of your database.
⚔️ YOUR MISSION
Create a covering index on the employees table that includes the employee_id, name, and department columns. Then, write a query that uses the covering index to filter on employee_id and name, and retrieve the department column.
Read the exercise prompt
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
name VARCHAR(255),
department VARCHAR(255)
);
INSERT INTO employees (employee_id, name, department)
VALUES
(1, 'John Smith', 'Sales'),
(2, 'Jane Doe', 'Marketing'),
(3, 'Bob Johnson', 'IT');
CREATE INDEX idx_employees ON employees (employee_id, name, department);
SELECT department
FROM employees
WHERE employee_id = 1 AND name = 'John Smith';
