Given a table users with columns id (integer) and email (text), an expression index is created on LOWER(email). Which query will use this index efficiently?
CREATE INDEX idx_lower_email ON users (LOWER(email));
Think about which query matches the expression used in the index.
The index is on LOWER(email), so only queries that use LOWER(email) in the WHERE clause can use this index efficiently. Option A matches exactly.
Which of the following is the correct syntax to create an expression index on the price * quantity expression in a sales table?
Expression indexes require specifying the expression inside parentheses, optionally with an index method.
Option C correctly creates an expression index on price * quantity using the default btree method. Option C is missing the index method specification which is optional but recommended. Option C indexes columns separately, not the expression. Option C uses a different expression.
What is the main advantage of using an expression index in a database?
Think about what expression indexes store and how they help queries.
Expression indexes store the result of a function or expression applied to columns, allowing queries that filter on that expression to run faster. They do not update columns or store computed columns physically.
A developer created this index:
CREATE INDEX idx_trim_name ON employees (TRIM(name));
But the query below does not use the index:
SELECT * FROM employees WHERE name = 'Alice';
Why?
Check if the query expression matches the index expression.
The index is on TRIM(name), but the query filters on name directly without trimming. The index is not used because the expressions do not match.
Consider a products table with a JSONB column attributes. You want to speed up queries filtering products where attributes->>'color' = 'red'. Which index creation is best?
Think about indexing the exact expression used in the WHERE clause.
Option D creates an expression index on the exact expression attributes->>'color', which matches the query filter and speeds it up. Option D indexes the whole JSONB column, which is less efficient for this filter. Option D creates a GIN index for general JSONB queries but is less efficient for exact text matches. Option D uses attributes->'color' which returns JSON, not text, so it won't match the query.