Re: Indexing Strategy

From: Josh Berkus <josh(at)agliodbs(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Cc: Matthew Engelbert <mje1975(at)yahoo(dot)com>, pgsql-novice(at)postgresql(dot)org
Subject: Re: Indexing Strategy
Date: 2004-12-15 02:29:57
Message-ID: 200412141829.57589.josh@agliodbs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice pgsql-sql

Matthew,

> select * from quote where sybmol = 'MSFT' and extract(doy from time) = 340
> order by time

Aha, that should be easy.

CREATE INDEX quote_doy_symbol ON quote ( (extract(doy from "time")), symbol );

The reason that I'm putting the DOY first is because it's liable to be
slightly more selective; 365 values as opposed to 150. This is just to help
the planner realize that the index is useful.

I doubt it's worth indexing the order by within that, since the query should
produce a fairly small amount of rows

Of course, using extract doesn't gain you anything, and in fact adds
significant CPU overhead to both the query and the index. So you'd be
slightly better off doing:

SELECT * FROM quote
WHERE symbol = 'MSFT'
AND "time" BETWEEN '2004-10-11' AND '2004-10-12';

This will also allow you to create a single index on:

CREATE INDEX quote_time_symbol ON quote("time", symbol);

... which will be useful for any time-based query, not just one on days. And
it would be potentially useful for time-based queries which don't include a
symbol.

Further, if your queries are *always* structured like the above (time +
symbol) I'd suggest CLUSTERing on the index.

Also, I hope that you didn't really name a column "time".

--
Josh Berkus
Aglio Database Solutions
San Francisco

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Michael Fuhr 2004-12-15 03:00:24 Re: status variables
Previous Message Keith Worthington 2004-12-15 02:11:24 status variables

Browse pgsql-sql by date

  From Date Subject
Next Message Mark Collette 2004-12-15 10:07:03 Updating column to link one table to another
Previous Message Bruno Wolff III 2004-12-14 05:22:50 Re: Similar tables, different indexes performance