Not sure if this is possible in PostgreSQL 9.3+, but I\'d like to create a unique index on a non-unique column. For a table like:
CREATE TABLE data (
id SE
An index can only index actual rows, not aggregated rows. So, yes, as far as the desired index goes, creating a table with unique values like you mentioned is your only option. Enforce referential integrity with a foreign key constraint from data.day
to days.day
. This might also be best for performance, depending on the complete situation.
However, since this is about performance, there is an alternative solution: you can use a recursive CTE to emulate a loose index scan:
WITH RECURSIVE cte AS (
( -- parentheses required
SELECT day FROM data ORDER BY 1 LIMIT 1
)
UNION ALL
SELECT (SELECT day FROM data WHERE day > c.day ORDER BY 1 LIMIT 1)
FROM cte c
WHERE c.day IS NOT NULL -- exit condition
)
SELECT day FROM cte;
Parentheses around the first SELECT
are required because of the attached ORDER BY
and LIMIT
clauses. See:
This only needs a plain index on day
.
There are various variants, depending on your actual queries:
More in my answer to your follow-up querstion: