Imagine you have a large table with millions of rows. You want to find rows quickly based on a column value. What does adding an index on that column do?
Think about how a book's index helps you find a topic quickly.
An index creates a quick lookup structure that helps the database find rows without scanning the entire table.
You run a query filtering by a column that has no index. What is the expected behavior?
Without a map or index, how do you find a word in a dictionary?
Without an index, the database must check each row one by one, which takes more time.
Choose the correct SQL statement to create a unique index on the 'email' column.
Remember the correct order of keywords in PostgreSQL for unique indexes.
The correct syntax is CREATE UNIQUE INDEX index_name ON table(column);
Why should you avoid creating indexes on sensitive data columns such as passwords?
Think about where index data is stored and who can access it.
Indexes can store copies of column data in plain text, so indexing sensitive data risks exposure if the index is accessed.
Given a table with many inserts and updates, but also many read queries filtering by 'status' and 'created_at', which indexing approach is best?
Think about how composite indexes help with multi-column queries and write costs.
A composite index on the queried columns improves read speed for those filters and reduces the number of indexes to update on writes, balancing performance.