![]() |
VOOZH | about |
A database index is a data structure, typically organized as a B-tree, that is used to quickly locate and access data in a database table. Indexes are used to speed up the query process by allowing the database to quickly locate records without having to search through every row of the table.
An index is a data structure in a database that is used to improve the speed of data retrieval operations on a database table. It is a special type of database object that stores the values of one or more columns of a table, and the row ids that point to the physical storage location of the corresponding rows. Indexes are used to optimize the performance of queries, as they allow the database to quickly determine the location of the data that is being requested.
Multiple indexes refer to the creation of individual indexes on different columns in the same table. Each index provides a separate access path to locate data quickly based on the column it’s built on.
In Multiple Indexes
CREATE INDEX idx_name ON People(name);
CREATE INDEX idx_age ON People(age);
CREATE INDEX idx_dob ON People(date_of_birth);
If a query filters by
age, the database usesidx_age.
Useful when you have varied queries like:
SELECT * FROM People WHERE name = 'Alice';
SELECT * FROM People WHERE age = 25;
A multi-column index is a single index that includes two or more columns in a specific order. It's used when queries frequently involve multiple columns together. Unlike multiple indexes, multi-column indexes are organized based on the combined data stored in the indexed columns and can only be used to locate data in the indexed columns.
In Multiple Indexes
CREATE INDEX idx_name_dob_age ON People(name, date_of_birth, age);
This index helps with queries that filter or sort by:
namenameanddate_of_birthname,date_of_birth, andage
Ideal when your queries consistently use combinations:
SELECT * FROM People WHERE name = 'Alice' AND date_of_birth = '1990-01-01';
| Feature / Use Case | Multiple Indexes | Multi-Column Indexes |
|---|---|---|
| Definition | Multiple separate indexes on different columns in the same table. | A single index that includes two or more columns. |
| Structure | Each index is stored separately and can grow independently. | Stored as a single unit, combining multiple columns. |
| Performance Benefit | Improves performance for queries using different individual columns. | Optimizes queries that filter or sort using multiple columns together. |
| Query Optimization | Useful for individual column searches; may use index intersection for multi-columns. | Best for queries using the leading column(s) in the same order. |
| Range Queries | Can optimize range queries individually on each column. | Can optimize range queries if they start with the first indexed column. |
| SELECT Query Efficiency | Helps when multiple columns are queried separately. | Helps when multiple columns are queried together in a known order. |
| Enforcing Uniqueness | Can enforce uniqueness per column. | Can enforce uniqueness across a combination of columns. |
| Storage Requirements | Higher storage as each index is separate. | More efficient as it combines multiple columns into a single index. |
| Subset Column Queries | Not ideal unless specific indexes exist. | Can optimize queries that use a subset starting from the first column. |
| Use with Multiple Tables | Not limited to single-table queries. | Cannot be used to query across multiple tables directly. |
| Common Use Cases | Best for queries using a variety of columns without fixed patterns. | Best when queries consistently use the same column combinations. |