2

I have a records table with ~3 million records. The table has a column called record_type with an index.

                                         Table "public.records"
      Column       |            Type             |                      Modifiers
-------------------+-----------------------------+------------------------------------------------------
 id                | integer                     | not null default nextval('records_id_seq'::regclass)
 record_type       | character varying(255)      |

Indexes:
    "records_pkey" PRIMARY KEY, btree (id)
    "index_records_on_record_type" btree (record_type)

The following query takes more than 1.8 seconds to complete:

SELECT DISTINCT record_type FROM records;

If I check the query planner, it looks like PostgreSQL performs a sequential scan on the table. I would expect the database to take advantage of the index.

EXPLAIN SELECT DISTINCT record_type FROM records;
                              QUERY PLAN
-----------------------------------------------------------------------
 HashAggregate  (cost=101961.23..101961.40 rows=17 width=3)
   Group Key: record_type
   ->  Seq Scan on records  (cost=0.00..94465.78 rows=2998178 width=3)
(3 rows)

Is there a way to speed up this query?