0
0
PostgreSQLquery~20 mins

Hash index for equality in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Hash Index Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Effect of Hash Index on Equality Query Performance

Consider a PostgreSQL table users with columns id (primary key) and email (unique). A hash index is created on the email column.

Which query will most likely benefit from this hash index?

PostgreSQL
CREATE TABLE users (id SERIAL PRIMARY KEY, email TEXT UNIQUE);
CREATE INDEX users_email_hash_idx ON users USING HASH (email);
ASELECT * FROM users WHERE email LIKE '%@example.com';
BSELECT * FROM users WHERE email = 'example@example.com';
CSELECT * FROM users WHERE email > 'a@example.com';
DSELECT * FROM users ORDER BY email;
Attempts:
2 left
💡 Hint

Hash indexes are optimized for equality comparisons, not range or pattern matching.

🧠 Conceptual
intermediate
2:00remaining
Hash Index Limitations in PostgreSQL

Which of the following is NOT a limitation of hash indexes in PostgreSQL?

AThey require more disk space than B-tree indexes for the same data.
BThey cannot be used to enforce uniqueness constraints.
CThey are not WAL-logged by default, so recovery can be problematic.
DThey only support equality comparisons, not range queries.
Attempts:
2 left
💡 Hint

Think about the storage size and uniqueness enforcement capabilities.

📝 Syntax
advanced
2:00remaining
Creating a Hash Index Syntax

Which of the following SQL statements correctly creates a hash index on the username column of the accounts table in PostgreSQL?

ACREATE HASH INDEX accounts_username_hash ON accounts (username);
BCREATE INDEX accounts_username_hash ON accounts (username) USING HASH;
CCREATE INDEX HASH accounts_username_hash ON accounts (username);
DCREATE INDEX accounts_username_hash ON accounts USING HASH (username);
Attempts:
2 left
💡 Hint

Remember the correct order of clauses in the CREATE INDEX statement.

optimization
advanced
2:00remaining
Choosing Index Type for Query Optimization

You have a table products with a column sku that is queried mostly by exact matches. You want to optimize query speed and reduce index size. Which index type is best?

ACreate a Hash index on <code>sku</code>.
BCreate a B-tree index on <code>sku</code>.
CCreate a GIN index on <code>sku</code>.
DCreate a GiST index on <code>sku</code>.
Attempts:
2 left
💡 Hint

Consider the type of queries and index size.

🔧 Debug
expert
2:00remaining
Diagnosing Hash Index Usage in Query Plan

You created a hash index on the customer_id column of the orders table. However, your query SELECT * FROM orders WHERE customer_id = 123; does not use the hash index according to EXPLAIN. What is the most likely reason?

AThe hash index is not WAL-logged and thus disabled for queries.
BPostgreSQL prefers B-tree indexes by default and ignores hash indexes.
CThe <code>customer_id</code> column is of a data type that does not support hash indexing.
DThe query uses an inequality operator instead of equality.
Attempts:
2 left
💡 Hint

Check the data type compatibility with hash indexes.