pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: J@Planeti.Biz
To: pgsql-performance@postgresql.org
Subject: Multiple Order By Criteria
Date: Tue, 17 Jan 2006 17:03:27 -0500
Message-ID: <05c301c61bb1$d88c6a00$681e140a@fatchubby> (raw)

I'm trying to query a table with 250,000+ rows. My query requires I provide 5 colums in my "order by" clause:

select
   column
from table 
where 
 column >= '2004-3-22 0:0:0'
order by 
    ds.receipt desc,
    ds.carrier_id asc,
    ds.batchnum asc,
    encounternum asc,
    ds.encounter_id ASC
limit 100 offset 0

I have an index built for each of these columns in my order by clause. This query takes an unacceptable amount of time to execute. Here are the results of the explain:

Limit  (cost=229610.78..229611.03 rows=100 width=717)
  ->  Sort  (cost=229610.78..230132.37 rows=208636 width=717)
        Sort Key: receipt, carrier_id, batchnum, encounternum, encounter_id
        ->  Seq Scan on detail_summary ds  (cost=0.00..22647.13 rows=208636 width=717)
              Filter: (receipt >= '2004-03-22'::date)


When I have the order by just have 1 criteria, it's fine (just ds.receipt DESC)

Limit  (cost=0.00..177.71 rows=100 width=717)
  ->  Index Scan Backward using detail_summary_receipt_id_idx on detail_summary ds  (cost=0.00..370756.84 rows=208636 width=717)
        Index Cond: (receipt >= '2004-03-22'::date)

I've increased my work_mem to up to 256meg with no speed increase. I think there's something here I just don't understand.

How do I make this go fast ?

view thread (12+ messages)  latest in thread

Message-ID: <05c301c61bb1$d88c6a00$681e140a@fatchubby>
Permalink:  ../05c301c61bb1$d88c6a00$681e140a@fatchubby/
Also on:    postgresql.org/message-id/05c301c61bb1$d88c6a00$681e140a@fatchubby

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: J@Planeti.Biz
  Subject: Re: Multiple Order By Criteria
  In-Reply-To: <05c301c61bb1$d88c6a00$681e140a@fatchubby>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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