### Unique ![[assets/ex_unique.png|256]] ### The Explain Trace ```sql -- Performing a DISTINCT on a sorted index SET enable_hashagg = off; EXPLAIN (ANALYZE, COSTS, BUFFERS, VERBOSE) SELECT DISTINCT species_id FROM animals ORDER BY species_id; ``` ```text Unique (cost=1.29..438.29 rows=5 width=4) (actual time=1.009..1.879 rows=5 loops=1) Output: species_id Buffers: shared hit=22 -> Index Only Scan using idx_animals_species_id on public.animals (...) ``` --- - **Description**: Removes duplicate rows from the result set. - **Performance**: High performance if the input is already sorted; removes duplicates in a single pass. - **Factors**: Dataset size and the cost of the child sort/index scan node. - **Cost**: `cpu_tuple_cost * number of tuples` - **Operates on**: [[Structures/Result Set]] - **Workloads**: - [[Workloads/LWLock/Buffers/BufferContent|LWLock: BufferContent]]