index usage

From: Timur Irmatov <thor(at)sarkor(dot)com>
To: pgsql-performance(at)postgresql(dot)org
Subject: index usage
Date: 2003-01-17 11:48:00
Message-ID: 10597430257.20030117164800@sarkor.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Hi, everyone!

I have a simple query which takes almost 3 seconds to complete, but
disabling sequence scans leads to a new plan using index. This second
plan takes less than 1 millisecond to run.

So, I'd like to hear any comments and suggestions.

Details.

CREATE TABLE MediumStats (
year SMALLINT NOT NULL,
month SMALLINT NOT NULL,
day SMALLINT NOT NULL,
hour SMALLINT NOT NULL,
--- and then goes few data fields
figureId INTEGER NOT NULL,
typeId INTEGER NOT NULL
PRIMARY KEY (figureId, typeId, year, month, day, hour)
);

CREATE FUNCTION indexHelper (INT2, INT2, INT2, INT2)
RETURNS CHARACTER(10) AS '
return sprintf("%d%02d%02d%02d", @_);
' LANGUAGE 'plperl' WITH (isCachable);

CREATE INDEX timeIndex ON MediumStats (indexHelper(year,month,day,hour));

and that is the query:
SELECT * FROM MediumStats
WHERE indexHelper(year,month,day,hour) < '2002121500'
LIMIT 1;

First, original plan:
Limit (cost=0.00..0.09 rows=1 width=22) (actual time=2969.30..2969.30 rows=0 loops=1)
-> Seq Scan on mediumstats (cost=0.00..1332.33 rows=15185 width=22) (actual time=2969.29..2969.29 rows=0 loops=1)
Total runtime: 2969.39 msec

Second plan, seq scans disabled:

Limit (cost=0.00..0.19 rows=1 width=6) (actual time=0.43..0.43 rows=0 loops=1)
-> Index Scan using timeindex on mediumstats (cost=0.00..2898.96 rows=15185 width=6) (actual time=0.42..0.42 rows=0 loops=1)
Total runtime: 0.54 msec

Table MediumStats currently has 45000 rows, all rows belong to this
month.

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Charles H. Woloszynski 2003-01-17 13:29:25 Re: 7.3.1 New install, large queries are slow
Previous Message Stephan Szabo 2003-01-17 07:00:48 Re: 7.3.1 New install, large queries are slow