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?
CREATE TABLE users (id SERIAL PRIMARY KEY, email TEXT UNIQUE); CREATE INDEX users_email_hash_idx ON users USING HASH (email);
Hash indexes are optimized for equality comparisons, not range or pattern matching.
Hash indexes in PostgreSQL are designed to speed up queries that use equality operators (=). Queries using LIKE with wildcards, range comparisons, or ordering do not benefit from hash indexes.
Which of the following is NOT a limitation of hash indexes in PostgreSQL?
Think about the storage size and uniqueness enforcement capabilities.
Hash indexes support equality queries only and cannot enforce uniqueness. They were historically not WAL-logged, but since PostgreSQL 10, they are WAL-logged. They do not necessarily require more disk space than B-tree indexes.
Which of the following SQL statements correctly creates a hash index on the username column of the accounts table in PostgreSQL?
Remember the correct order of clauses in the CREATE INDEX statement.
The correct syntax is CREATE INDEX index_name ON table_name USING HASH (column_name);. Other options have syntax errors or misplaced keywords.
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?
Consider the type of queries and index size.
Hash indexes are optimized for equality lookups and can be smaller and faster than B-tree indexes for this use case. GIN and GiST indexes are for full-text search and complex data types, not simple equality.
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?
Check the data type compatibility with hash indexes.
Hash indexes only support certain data types. If the column's data type is unsupported, PostgreSQL will not use the hash index. PostgreSQL does not ignore hash indexes by default, and since version 10, hash indexes are WAL-logged. The query uses equality, so that is not the issue.