Junior — Middle
What types of indexes would you create for a database table to improve query performance?
sobes.tech AI
Answer from AI
To improve query performance on a database table, the following types of indexes are usually created:
- Primary Key — a unique index that provides quick access to rows by a unique identifier.
- Unique Indexes — ensure the uniqueness of values in a column and speed up searches.
- Non-unique Indexes — accelerate searches on frequently used columns, such as foreign keys or filter fields.
- Composite Indexes — index multiple columns together, useful for queries filtering by several fields.
- Full-text Indexes — for fast searching of text data.
The choice of indexes depends on the types of queries most frequently executed. For example, if searches are often performed by date and status, a composite index on these columns should be created.
Example of creating an index in SQL:
CREATE INDEX idx_user_email ON users(email);
Or a composite index:
CREATE INDEX idx_orders_date_status ON orders(order_date, status);