Re: Add a ROWCOUNT to the output of a select. - Mailing list pgsql-sql

From Gurjeet Singh
Subject Re: Add a ROWCOUNT to the output of a select.
Date
Msg-id 65937bea0805131733t6a434ea1mab67f82b286c0376@mail.gmail.com
Whole thread Raw
In response to Add a ROWCOUNT to the output of a select.  (Gavin 'Beau' Baumanis <gavinb@eclinic.com.au>)
List pgsql-sql
On Wed, May 14, 2008 at 5:24 AM, Gavin 'Beau' Baumanis <gavinb@eclinic.com.au> wrote:
Hi Everyone,

After spending some time searching through our good friend Mr. Google and the mailing list I found a post that provided me with a query that does just what I need.

However, the query takes FOREVER and although this is stated in the original mail I thought I would ask if there was any advice that you might be able to provide to speed things up a little.

And while the query does return over 27,000 rows in my case, I didn't expect it to take 9 minutes and 11 seconds!

Please find the query below - and of course - thanks in advance for any assistance you might be able to provide me with!

select
   (select
        count(*)
    from
        myTable as myCount
    where
        myCount.contactdate <= myTable.contactdate
   ) as rownum,
   contactdate
from
   myTable
where
   contactdate > '2007-06-30 23:59:59'
order by
   contactdate;


Posting EXPLAIN command's output would have helped here.

This sub-query in the SELECT list is a correlated sub-query, so it will be executed for each row that passes the oouter query's WHERE clause. If you don't have it already, I'd suggest creating an index on the 'contactdate' column; that should help speed up the query. In absence of such an index, the planner will choose Sequential Scan, which is very expensive/time-consuming.

Best regards,
--
gurjeet[.singh]@EnterpriseDB.com
singh.gurjeet@{ gmail | hotmail | indiatimes | yahoo }.com

EnterpriseDB http://www.enterprisedb.com

Mail sent from my BlackLaptop device

pgsql-sql by date:

Previous
From: Gavin 'Beau' Baumanis
Date:
Subject: Add a ROWCOUNT to the output of a select.
Next
From: "Robins Tharakan"
Date:
Subject: Re: Add a ROWCOUNT to the output of a select.