Junior — Middle
How is index creation by condition performed in a database column?
sobes.tech AI
Answer from AI
Creating conditional indexing in a database column is usually implemented using partial indexes or filtered indexes. This allows indexing only those rows that meet a certain condition, saving space and speeding up queries.
Main methods:
- In PostgreSQL, you can create a partial index using the following syntax:
CREATE INDEX idx_name ON table_name(column_name) WHERE condition;
For example:
CREATE INDEX idx_active_users ON users(last_login) WHERE active = true;
- In SQL Server, a filtered index is used:
CREATE INDEX idx_name ON table_name(column_name) WHERE condition;
- In MySQL, partial indexes with conditions are not directly supported, but you can use prefix indexing or create an index on a computed column.
Thus, creating conditional indexes depends on the specific DBMS, but most modern systems support partial or filtered indexes for query optimization.