Re: Need help with complicated SQL statement

From: Shane Ambler <pgsql(at)Sheeky(dot)Biz>
To: Ted Byers <r(dot)ted(dot)byers(at)rogers(dot)com>
Cc: Sam Mason <sam(at)samason(dot)me(dot)uk>, pgsql-general(at)postgresql(dot)org
Subject: Re: Need help with complicated SQL statement
Date: 2007-11-18 18:53:20
Message-ID: 47408A20.9030201@Sheeky.Biz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Shane Ambler wrote:
> I INSERTed 500 stocks entries and 10,000 stockprices entries for each
> stock (that's 5,000,000 price rows), then from
>
> EXPLAIN ANALYSE SELECT * FROM stock_price_history WHERE stock_id = 20
> I got - Total runtime: 981.618 ms
>
> EXPLAIN ANALYSE SELECT * FROM stock_price_history WHERE stock_id IN
> (10,25,36,45,86,154,368,481)
> I got - Total runtime: 8084.217 ms
>

Actually I found a better way - after you run the example I gave you
before -

DROP INDEX idx_stockprices_date
DROP INDEX idx_stockprices_stock_id

CREATE INDEX idx_stockprices_id_date ON stockprices (stock_id,price_date);

with the same data (5,000,000 price rows) I then get -

EXPLAIN ANALYSE SELECT * FROM stock_price_history WHERE stock_id = 20
I got - Total runtime: 6.397 ms

EXPLAIN ANALYSE SELECT * FROM stock_price_history WHERE stock_id IN
(10,25,36,45,86,154,368,481)
I got - Total runtime: 36.265 ms

Which is probably the speed you want ;-)

--

Shane Ambler
pgSQL(at)Sheeky(dot)Biz

Get Sheeky @ http://Sheeky.Biz

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Sam Mason 2007-11-18 18:59:57 Re: GIN: any ordering guarantees for the hits returned?
Previous Message Tom Lane 2007-11-18 18:36:43 Re: ERROR: invalid restriction selectivity: 224359728.000000