pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Indices for both where and order by.
3+ messages / 3 participants
[nested] [flat]

* Indices for both where and order by.
@ 2002-05-17 10:54  Niclas Gustafsson <niclas.gustafsson@codesense.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Niclas Gustafsson @ 2002-05-17 10:54 UTC (permalink / raw)
  To: pgsql-admin

Hi all, 

A quick question about indices...

I have a table with a couple of column, two of which I use in 
a query, one for the where and one for the order by.

Table look something like:
                 Table "hs"
  Attribute|         Type          |                          Modifier

-----------+-----------------------+------------------------------------
------------------------
 id        | integer               | not null default
nextval('"hs_id_seq"'::text)
 timeid    | integer               | 
 code      | character varying(12) | 
 tcode     | character varying(10) | 
 inst      | character varying(10) | 
 timestamp | integer               | 
.
.
.

The query I use is like this:

Select * from hs where tcode = 'XXZZ' order by timestamp
The table above contains something like 500 000 rows

If I create an index on the tcode I can speed the query up using an
index scan, however the 
Sorting is taking some time.

An idea was to create an Index on both tcode and timestamp, like this
Create index hs_tcode_timestamp_idx on hs (tcode, timestamp)

Am I completely wrong here, or could the planner not use it for both the
where and the sorting?


Regards,

Niclas Gustafsson

 





^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Indices for both where and order by.
@ 2002-05-23 19:39  Niclas Gustafsson <Niclas.Gustafsson@codesense.com>
  parent: Niclas Gustafsson <niclas.gustafsson@codesense.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Niclas Gustafsson @ 2002-05-23 19:39 UTC (permalink / raw)
  To: pgsql-general@postgresql.org

Hi all,

A quick question about indices...

I have a table with a couple of column, two of which I use in 
a query, one for the where and one for the order by.

Table look something like:
                 Table "hs"
  Attribute|         Type          |                          Modifier

-----------+-----------------------+------------------------------------
------------------------
 id        | integer               | not null default
nextval('"hs_id_seq"'::text)
 timeid    | integer               | 
 code      | character varying(12) | 
 tcode     | character varying(10) | 
 inst      | character varying(10) | 
 timestamp | integer               | 
.
.
.

The query I use is like this:

Select * from hs where tcode = 'XXZZ' order by timestamp
The table above contains something like 500 000 rows

If I create an index on the tcode I can speed the query up using an
index scan, however the 
Sorting is taking some time.

An idea was to create an Index on both tcode and timestamp, like this
Create index hs_tcode_timestamp_idx on hs (tcode, timestamp)

Am I completely wrong here, or could the planner not use it for both the
where and the sorting?


Regards,

Niclas Gustafsson





^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Indices for both where and order by.
@ 2002-05-23 20:21  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Niclas Gustafsson <Niclas.Gustafsson@codesense.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Tom Lane @ 2002-05-23 20:21 UTC (permalink / raw)
  To: Niclas Gustafsson <Niclas.Gustafsson@codesense.com>; +Cc: pgsql-general@postgresql.org

Niclas Gustafsson <Niclas.Gustafsson@codesense.com> writes:
> Select * from hs where tcode = 'XXZZ' order by timestamp

> An idea was to create an Index on both tcode and timestamp, like this
> Create index hs_tcode_timestamp_idx on hs (tcode, timestamp)

> Am I completely wrong here, or could the planner not use it for both the
> where and the sorting?

You'd have to write
  Select * from hs where tcode = 'XXZZ' order by tcode, timestamp
to get the planner to realize that the index ordering is what's wanted.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2002-05-23 20:21 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-05-17 10:54 Indices for both where and order by. Niclas Gustafsson <niclas.gustafsson@codesense.com>
2002-05-23 19:39 ` Niclas Gustafsson <Niclas.Gustafsson@codesense.com>
2002-05-23 20:21   ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox