MCQs on Indexes and Performance Basics | PostgreSQL Database

Indexes are crucial for optimizing query performance in PostgreSQL. This guide explores index creation, usage, and analysis with tools like EXPLAIN, enabling efficient data retrieval and management.


What are Indexes?

  1. What is the primary purpose of indexes in PostgreSQL?
    a) Storing data efficiently
    b) Optimizing query performance
    c) Managing database schemas
    d) Enabling transaction logs
  2. Which of the following best describes an index?
    a) A data storage table
    b) A database connection method
    c) A data structure to improve query efficiency
    d) A type of database backup
  3. Which type of data operation benefits most from indexes?
    a) Sequential scans
    b) Random updates
    c) Search queries
    d) Bulk data loads
  4. What is a disadvantage of using indexes in a database?
    a) Increased query complexity
    b) Reduced query performance
    c) Increased storage requirements
    d) Difficulty in data visualization
  5. What type of index is automatically created for a primary key in PostgreSQL?
    a) Hash index
    b) B-tree index
    c) GIN index
    d) BRIN index

Creating and Dropping Indexes

  1. Which command is used to create an index in PostgreSQL?
    a) CREATE INDEX
    b) ADD INDEX
    c) MAKE INDEX
    d) INSERT INDEX
  2. What is required to create an index on a table?
    a) A foreign key constraint
    b) An existing column
    c) A unique constraint
    d) A schema privilege
  3. Which command removes an existing index in PostgreSQL?
    a) REMOVE INDEX
    b) DROP INDEX
    c) DELETE INDEX
    d) TRUNCATE INDEX
  4. Can multiple indexes be created on the same table in PostgreSQL?
    a) No, only one index is allowed per table
    b) Yes, but only for unique constraints
    c) Yes, with different columns or conditions
    d) No, it is restricted by default
  5. What is a potential impact of dropping an index?
    a) Reduced query performance for related queries
    b) Loss of table data
    c) Automatic recreation during the next query
    d) Conversion to a primary key

Understanding Index Scans

  1. What does an index scan in PostgreSQL indicate?
    a) The database is reading all rows sequentially
    b) The database is using the index to find specific rows
    c) The database is creating a new index
    d) The database is ignoring existing indexes
  2. What type of query is most likely to use an index scan?
    a) SELECT * FROM table
    b) SELECT column FROM table WHERE column = value
    c) INSERT INTO table VALUES (…)
    d) DELETE FROM table
  3. What is a sequential scan in PostgreSQL?
    a) Scanning the index for all matching rows
    b) Scanning the entire table row by row
    c) Creating an index dynamically during query execution
    d) Optimizing index scans for faster performance
  4. Which factor determines whether PostgreSQL uses an index scan or a sequential scan?
    a) The size of the table
    b) The storage engine
    c) The network latency
    d) The database version
  5. What happens if an index does not exist for a query’s condition?
    a) PostgreSQL raises an error
    b) The query fails to execute
    c) A sequential scan is performed instead
    d) PostgreSQL creates an index automatically

Simple Use of EXPLAIN

  1. What is the purpose of the EXPLAIN command in PostgreSQL?
    a) To visualize database schemas
    b) To analyze and display query execution plans
    c) To update database indexes
    d) To monitor database performance over time
  2. Which of the following options can be used with EXPLAIN to show actual execution times?
    a) EXPLAIN PERFORMANCE
    b) EXPLAIN TIMINGS
    c) EXPLAIN ANALYZE
    d) EXPLAIN METRICS
  3. What does the cost value in an EXPLAIN output represent?
    a) The time required to execute the query
    b) The monetary cost of the query
    c) The estimated effort to run the query
    d) The total size of the result set
  4. How does EXPLAIN ANALYZE differ from EXPLAIN?
    a) It predicts execution plans without running the query
    b) It shows actual execution statistics after running the query
    c) It only works for indexed tables
    d) It can modify the query plan for optimization
  5. Which part of the EXPLAIN output describes the use of an index?
    a) Sequential Scan
    b) Bitmap Heap Scan
    c) Index Scan
    d) Table Scan
  6. Why is EXPLAIN useful for database optimization?
    a) It provides insights into query performance bottlenecks
    b) It updates outdated indexes automatically
    c) It enforces primary key constraints
    d) It generates optimized execution plans
  7. Which parameter in the EXPLAIN output indicates rows processed?
    a) Rows
    b) Cost
    c) Time
    d) Buffers
  8. What does a high cost value in an EXPLAIN plan suggest?
    a) The query is optimized
    b) The query is expensive to execute
    c) The index is not being used
    d) The table has insufficient rows
  9. How can you force PostgreSQL to use a specific index in a query?
    a) Use FORCE INDEX
    b) Add an INDEX hint to the query
    c) Rewrite the query to match the index structure
    d) Use SET INDEX command
  10. Which EXPLAIN output would suggest an index is underutilized?
    a) Index Scan with low cost
    b) Sequential Scan on a large table
    c) Bitmap Heap Scan with high rows
    d) Index Only Scan

Advanced EXPLAIN Features

  1. What does “Bitmap Heap Scan” in EXPLAIN output signify?
    a) Rows are scanned sequentially
    b) Index results are combined for efficient access
    c) A new index is created dynamically
    d) No index is used in the query
  2. How can EXPLAIN help identify redundant indexes?
    a) By listing all indexes in the output
    b) By showing high-cost sequential scans
    c) By revealing unused indexes in execution plans
    d) By analyzing row counts in indexes
  3. Which EXPLAIN keyword allows capturing detailed buffer usage?
    a) EXPLAIN MEMORY
    b) EXPLAIN ANALYZE BUFFERS
    c) EXPLAIN COST
    d) EXPLAIN DETAILS
  4. When would “Index Only Scan” appear in an EXPLAIN output?
    a) When only indexed columns are accessed
    b) When sequential scanning is faster
    c) When no index matches the query
    d) When multiple indexes are combined
  5. What is the advantage of EXPLAIN ANALYZE over EXPLAIN?
    a) It can modify query execution plans
    b) It measures real-time query execution metrics
    c) It provides additional index creation options
    d) It suppresses sequential scan details

Answer Key

QnoAnswer
1b) Optimizing query performance
2c) A data structure to improve query efficiency
3c) Search queries
4c) Increased storage requirements
5b) B-tree index
6a) CREATE INDEX
7b) An existing column
8b) DROP INDEX
9c) Yes, with different columns or conditions
10a) Reduced query performance for related queries
11b) The database is using the index to find specific rows
12b) SELECT column FROM table WHERE column = value
13b) Scanning the entire table row by row
14a) The size of the table
15c) A sequential scan is performed instead
16b) To analyze and display query execution plans
17c) EXPLAIN ANALYZE
18c) The estimated effort to run the query
19b) It shows actual execution statistics after running the query
20c) Index Scan
21a) It provides insights into query performance bottlenecks
22a) Rows
23b) The query is expensive to execute
24c) Rewrite the query to match the index structure
25b) Sequential Scan on a large table
26b) Index results are combined for efficient access
27c) By revealing unused indexes in execution plans

Here are the answers for the last three questions:

QnoAnswer
28b) EXPLAIN ANALYZE BUFFERS
29a) When only indexed columns are accessed
30b) It measures real-time query execution metrics

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top