Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an index over distinct values of a column in PostgreSQL

Tags:

postgresql

I have a PostgreSQL table that looks like this:

CREATE TABLE items (
    name TEXT NOT NULL,
    value TEXT NOT NULL,
    PRIMARY KEY (name, value)
);

I frequently do a query to see what values are available:

SELECT DISTINCT value FROM items;

How do I create an index in PostgreSQL that the above query to not have to iterate over all of the items table?

like image 284
njaard Avatar asked Jan 04 '23 22:01

njaard


1 Answers

Using a completely different query you can force PostgreSQL to use an index and get the equivalent of DISTINCT column..

https://wiki.postgresql.org/wiki/Loose_indexscan

like image 174
Urkle Avatar answered Jan 13 '23 13:01

Urkle