Objective. Students will create B-tree indexes, interpret EXPLAIN ANALYZE output, and identify at least two query patterns that benefit from indexing on a provided dataset.
This lesson, its worked examples and its practice questions has not yet been verified by an industry-certified professional or by a certification or approval body. It is a study draft and may contain errors. Do not use it as instructions for work on a job site or on live equipment. Always follow your instructor, your supervising professional, and current official references and regulations.
VoTechWorks program approval and accreditation are still in progress. Credentials are issued only by the certifying bodies named in each program, after their own exams.
As databases grow, poorly written queries can turn a fraction-of-a-second response into a multi-second delay. A 50,000-row table may seem small, but production systems routinely store millions of rows. Developing good habits now—reading query plans, creating targeted indexes, and knowing when not to index—will carry through your entire career as a developer or database administrator.
When you submit a SELECT statement, PostgreSQL does not run it immediately. First, the query planner examines available statistics about the table (row counts, column value distributions) and generates a query plan: a tree of steps describing exactly how data will be retrieved and joined. The planner chooses the plan it estimates will cost the least work. Understanding that plan is the first step toward improving it.
A sequential scan (Seq Scan) reads every row in a table from beginning to end—like reading a book cover to cover to find one sentence. For small tables or queries that return most rows, this is fine. For large tables where only a few rows match, it is wasteful.
An index scan uses a pre-built data structure to jump directly to matching rows—like using a book's index to find the exact page. PostgreSQL supports several index types; the default and most broadly useful is the B-tree index, which handles equality checks (`=`), range queries (`<`, `>`, `BETWEEN`), and `ORDER BY` efficiently.
The syntax is straightforward:
```sql
CREATE INDEX idx_orders_customer_id
ON orders (customer_id);
```
PostgreSQL will build the index immediately and maintain it automatically as rows are inserted, updated, or deleted. Name indexes descriptively so you can identify them later (`idx_tablename_columnname` is a common convention).
`EXPLAIN` shows the planner's estimated plan without running the query. `EXPLAIN ANALYZE` actually executes the query and adds real timing and row-count data. Use `EXPLAIN ANALYZE` during development; avoid it on destructive statements in production unless you wrap them in a transaction you roll back.
A typical output node looks like this:
```
Seq Scan on orders (cost=0.00..1540.00 rows=50000 width=72)
(actual time=0.032..18.451 rows=50000 loops=1)
```
Key fields to annotate:
After adding an index, a node may change from `Seq Scan` to `Index Scan` or `Bitmap Index Scan`, and the total cost and actual time should drop noticeably.
Look for these patterns in a query plan:
1. Seq Scan on a large table with a low `rows` estimate — the planner wants few rows but has no index to use.
2. High actual time on a single node — that node is doing most of the work.
3. Filter lines inside a Seq Scan — rows are being discarded after a full read, meaning the WHERE clause column has no index.
Columns that frequently appear in `WHERE`, `JOIN ON`, and `ORDER BY` clauses are prime index candidates.
Indexes are not free. Each index must be updated every time a row is inserted, updated, or deleted, adding overhead to write operations. Guidelines from PostgreSQL documentation and industry practice:
A structured log makes your work reproducible and defensible. For each index you create, record: the index name, the query tested, the before cost (from EXPLAIN ANALYZE total), the after cost, and a written interpretation explaining why cost changed. This habit mirrors professional database tuning documentation.
PostgreSQL itself does not involve physical safety hazards, but responsible database work has its own critical rules:
Answer each question, then check it. Nothing is recorded; this is your own practice.
A free tech program — complimentary, as our commitment to get America working. Help us build VoTechWorks. Tell us what works, what doesn’t, and what you want to learn.
Does the school offer financial aid?
No. We don’t offer financial aid because we’re free. No, really: completely free.
Do you offer industry certification?
No. We are not certified yet, so we can’t offer that to students. What we do offer is knowledge and understanding of a trade.
Enroll first in the trade you’re interested in. You get your key, your program opens, and you begin studying.
Enroll free →