Junior — Middle
Explain what partial indexes in databases are and in which cases they are used.
sobes.tech AI
Answer from AI
Partial indexes are indexes that are created not on the entire table, but only on a subset of rows that satisfy a certain condition (filter). This allows reducing the size of the index and speeding up search operations if queries often access a subset of data.
For example, if a table of orders frequently queries only active orders, a partial index can be created on rows with status = 'active'. Then, the index will contain only these records, saving space and increasing performance.
Example in SQL:
CREATE INDEX idx_active_orders ON orders(order_date) WHERE status = 'active';
Partial indexes are useful when data has a clear division by status or categories, and queries are focused on specific subsets.