From: | Adrian Holovaty <postgresql(at)holovaty(dot)com> |
---|---|
To: | pgsql-performance(at)postgresql(dot)org |
Subject: | Index on a function and SELECT DISTINCT |
Date: | 2005-01-14 18:32:12 |
Message-ID: | 200501141232.13139.postgresql@holovaty.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-performance |
If I have this table, function and index in Postgres 7.3.6 ...
"""
CREATE TABLE news_stories (
id serial primary key NOT NULL,
pub_date timestamp with time zone NOT NULL,
...
)
CREATE OR REPLACE FUNCTION get_year_trunc(timestamp with time zone) returns
timestamp with time zone AS 'SELECT date_trunc(\'year\',$1);' LANGUAGE 'SQL'
IMMUTABLE;
CREATE INDEX news_stories_pub_date_year_trunc ON
news_stories( get_year_trunc(pub_date) );
"""
...why does this query not use the index?
db=# EXPLAIN SELECT DISTINCT get_year_trunc(pub_date) FROM news_stories;
QUERY PLAN
---------------------------------------------------------------------------------
Unique (cost=59597.31..61311.13 rows=3768 width=8)
-> Sort (cost=59597.31..60454.22 rows=342764 width=8)
Sort Key: date_trunc('year'::text, pub_date)
-> Seq Scan on news_stories (cost=0.00..23390.55 rows=342764
width=8)
(4 rows)
The query is noticably slow (2 seconds) on a database with 150,000+ records.
How can I speed it up?
Thanks,
Adrian
From | Date | Subject | |
---|---|---|---|
Next Message | Richard Huxton | 2005-01-14 18:34:14 | Re: sum of all values |
Previous Message | Josh Berkus | 2005-01-14 17:36:08 | Re: which dual-CPU hardware/OS is fastest for PostgreSQL? |