> [!NOTE] Index Only Scan > <table> > <tr> > <td width="25%"><img src="assets/ex_indexonlyscan.png"></td> > <td>An advanced optimization that retrieves all required columns directly from the index. By avoiding the 'Heap' (table files) entirely, it significantly reduces I/O. This requires the index to contain all requested columns and the Visibility Map to confirm the pages are 'all-visible'.</td> > </tr> > </table> > > ```sql > -- Selecting only the indexed column > EXPLAIN (ANALYZE, COSTS, BUFFERS, VERBOSE) > SELECT id FROM animals WHERE id = 100; > ``` > > ```text > Index Only Scan using animals_pkey on public.animals (cost=1.29..5.30 rows=1 width=4) (actual time=1.067..1.068 rows=1 loops=1) > Output: id > Index Cond: (animals.id = 100) > Heap Fetches: 0 > Buffers: shared hit=2 read=1 > Planning Time: 1.113 ms > Execution Time: 1.084 ms > ``` > > <table> > <tr> > <td rowspan="4" width="25%"><img src="assets/ex_index_only_scan.svg"></td> > <td><b>Performance</b></td><td>Extremely efficient as it avoids random heap I/O.</td> > </tr> > <tr><td><b>Factors</b></td><td>Visibility map status, index size, and selectivity.</td></tr> > <tr><td><b>Cost</b></td><td><code>index cost + cpu_index_tuple_cost * number of index entries</code></td></tr> > <tr><td><b>Workloads</b></td><td><a href="Workloads/IO/DataFile/DataFileRead">IO: DataFileRead</a>, <a href="Workloads/LWLock/Buffers/BufferContent">LWLock: BufferContent</a>, <a href="Workloads/LWLock/Buffers/BufferMapping">LWLock: BufferMapping</a></td></tr> > <tr><td colspan="3"><b>Description</b>: Scans an index without needing to access the table heap (unless rows are not all-visible).</td></tr> > </table>