0
0
PostgreSQLquery~5 mins

Covering indexes with INCLUDE in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a covering index in PostgreSQL?
A covering index is an index that contains all the columns needed to satisfy a query, so the database can get the data directly from the index without looking up the main table.
Click to reveal answer
beginner
What does the INCLUDE clause do in a PostgreSQL index?
The INCLUDE clause adds extra columns to the index as non-key columns. These columns are stored in the index but not used for sorting or searching, allowing queries to be covered by the index.
Click to reveal answer
intermediate
Why use INCLUDE columns instead of adding them as key columns in an index?
INCLUDE columns do not affect the index's sort order or size as much as key columns do. They help cover queries without slowing down index scans or increasing index maintenance cost significantly.
Click to reveal answer
beginner
Write a PostgreSQL command to create an index on column 'user_id' and include 'email' and 'created_at' columns.
CREATE INDEX idx_user_id_include ON users (user_id) INCLUDE (email, created_at);
Click to reveal answer
beginner
How does a covering index improve query performance?
It allows the database to answer queries using only the index without accessing the main table, reducing disk reads and speeding up query execution.
Click to reveal answer
What is the main benefit of using INCLUDE columns in a PostgreSQL index?
AThey make the index smaller by removing key columns.
BThey allow the index to cover more queries without increasing sort complexity.
CThey speed up data insertion by skipping index updates.
DThey automatically create foreign key constraints.
Which of these is true about columns added with INCLUDE in PostgreSQL indexes?
AThey replace the key columns in the index.
BThey are used for sorting and searching in the index.
CThey cannot be used to cover queries.
DThey are stored in the index but not used for sorting.
Which SQL command creates a covering index on 'order_id' including 'order_date' and 'customer_id'?
ACREATE INDEX idx_order ON orders (order_id, order_date, customer_id);
BCREATE INDEX idx_order ON orders (order_date, customer_id) INCLUDE (order_id);
CCREATE INDEX idx_order ON orders (order_id) INCLUDE (order_date, customer_id);
DCREATE INDEX idx_order ON orders INCLUDE (order_id, order_date, customer_id);
What happens if a query can be answered entirely from a covering index?
AThe database uses only the index, avoiding main table access.
BThe query runs slower because indexes are ignored.
CThe database reads the main table to get missing data.
DThe database rebuilds the index before running the query.
Which PostgreSQL version introduced the INCLUDE clause for indexes?
APostgreSQL 11
BPostgreSQL 9.2
CPostgreSQL 8.4
DPostgreSQL 13
Explain what a covering index is and how the INCLUDE clause helps create one in PostgreSQL.
Think about how indexes can store extra columns to answer queries fully.
You got /3 concepts.
    Describe the difference between key columns and INCLUDE columns in a PostgreSQL index.
    Consider how sorting and searching work in indexes.
    You got /3 concepts.