pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Imtiaz. S. M <imtiaz_sm@yahoo.com>
Cc: pgsql-sql@postgreSQL.org
Subject: Re: [SQL] pqReadData() error
Date: Mon, 06 Dec 1999 11:01:26 -0500
Message-ID: <18704.944496086@sss.pgh.pa.us> (raw)
In-Reply-To: <384B6971.2A893599@yahoo.com>

"Imtiaz. S. M" <imtiaz_sm@yahoo.com> writes:
> ... to delete the oldest data I use the following SQL queries.

> create table tempdates as select distinct dates from daydata;
> delete from daydata where dates in (select b.dates from tempdates a,
> tempdates b where a.dates-b.dates > '22 hours');

This is certainly the hard, hard way to do it.  Why not

create table tempdates as
    select max(dates) - '22 hours'::interval as cutoff from daydata;
delete from daydata where dates < (select cutoff from tempdates);

As you have it, the subselect must generate O(N^2) rows for an N-row
daydata table (assuming the distribution of dates is fairly even).
If I read you correctly, there are about 12,000 rows in daydata at all
times?  If so the subselect will examine 144,000,000 pairs of a.dates
and b.dates, and probably output about 25,000 rows (500 a rows matching
each b row, and vice versa).

That's a lot of data, and then it has to be scanned over for each row of
the daydata table in order to implement the IN; the rows that you are
keeping will require a comparison against *every single one* of the
subselect output rows to verify that the IN fails.  So about 11,500 *
25,000 row comparisons done by the IN just for the non-deleted rows,
and probably a few million more for the deletable rows.

Lots faster to calculate the cutoff time once and then do one compare
for each row.

You could probably simplify this even more, to

delete from daydata
where dates < (select max(dates) - '22 hours'::interval from daydata);

unless you have some other use for the temp table's contents.

> The query takes a long time and then gives me the
> following error

> "pqReadData() -- backend closed the channel unexpectedly.

Probably all that date arithmetic is overflowing memory with temporary
results ... which won't get reclaimed till end of query, at present.
But even if it didn't run out of memory, doing it this way is very slow.

			regards, tom lane



view thread (4+ messages)

Message-ID: <18704.944496086@sss.pgh.pa.us>
Permalink:  ../18704.944496086@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/18704.944496086@sss.pgh.pa.us

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, imtiaz_sm@yahoo.com
  Subject: Re: [SQL] pqReadData() error
  In-Reply-To: <18704.944496086@sss.pgh.pa.us>
  References: <384B6971.2A893599@yahoo.com> <18704.944496086@sss.pgh.pa.us>

* 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