0
0
PostgreSQLquery~5 mins

Expression indexes in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an expression index in PostgreSQL?
An expression index is an index built on the result of an expression or function applied to one or more columns, not just on the raw column values.
Click to reveal answer
beginner
Why use an expression index instead of a regular index?
Expression indexes speed up queries that filter or sort based on computed values, like lowercased text or date parts, which regular indexes on raw columns cannot optimize.
Click to reveal answer
intermediate
How do you create an expression index on the lower case of a column named 'name'?
Use: CREATE INDEX idx_lower_name ON table_name (LOWER(name)); This creates an index on the lowercased values of 'name'.
Click to reveal answer
intermediate
Can expression indexes be used with WHERE clauses?
Yes, you can create partial expression indexes by adding a WHERE clause to index only rows meeting certain conditions, improving performance and saving space.
Click to reveal answer
advanced
What happens if you query without using the exact expression in the WHERE clause that matches the expression index?
PostgreSQL cannot use the expression index if the query's expression does not exactly match the index expression, so the index won't speed up that query.
Click to reveal answer
What does an expression index in PostgreSQL index?
AOnly the raw column values
BThe result of a function or expression applied to columns
COnly primary key columns
DOnly text columns
Which SQL command creates an expression index on the lower case of column 'email'?
ACREATE INDEX idx_email_lower ON users (LOWER);
BCREATE INDEX idx_email_lower ON users (email);
CCREATE INDEX idx_email_lower ON users (LOWER(email));
DCREATE INDEX idx_email_lower ON users (LOWER(email)) WHERE email IS NOT NULL;
Can expression indexes improve performance for queries filtering by a computed value?
AOnly for sorting, not filtering
BNo, they only work on raw columns
COnly if the table is small
DYes, they speed up queries using that computed value
What is a partial expression index?
AAn index on an expression with a WHERE clause limiting rows indexed
BAn index on only part of a column's text
CAn index that covers only primary keys
DAn index that is created automatically by PostgreSQL
If a query uses a different expression than the one in the expression index, what happens?
AThe index is not used for that query
BThe index is used anyway
CPostgreSQL automatically adjusts the index
DThe query fails
Explain what an expression index is and give a simple example of when you would use one.
Think about indexing computed values instead of raw columns.
You got /3 concepts.
    Describe how partial expression indexes work and why they might be useful.
    Consider indexing only some rows based on a condition.
    You got /3 concepts.