Skip site navigation (1) Skip section navigation (2)

Re: Everlasting SQL query

From: Chris <list(at)1006(dot)org>
To: Joost Kraaijeveld <J(dot)Kraaijeveld(at)Askesis(dot)nl>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Everlasting SQL query
Date: 2004-07-28 10:58:57
Message-ID: 1091012337.1958.50.camel@dell (view raw, whole thread or download thread mbox)
Lists: pgsql-general
On Wed, 2004-07-28 at 12:08, Joost Kraaijeveld wrote:
> Hi everyone,
> I have a customer table (17518 records) and an orders table (88393 records). One of the columns of orders is customerid, containing the customerid (what else, but it is not a foreign key as this table is imported from a database that did not support foreign keys). 
> If I do this query (with pgadmin III):
> select,,
> from customers, orders
> order by,
> limit 25
> The query runs forever (the longest I let it run is 500 seconds).
> [...]

> If I only order by or by the query return within a second.
> Can anyone give me a reason why this all happens?

This is an inner join without a where clause.
It gives the crsoo product of 17518*88393 = 1548468574 results.

If you order by just one, PG can manage to give you the first 25 results
out of the 1.5 billion (!). If you order by both there's no other
way than to (try) computing everything - which PG does.

You most likely don't want this. Add a where clause:
where order.customer_id =
or something like that (I'm just guessing your scheme).

Bye, Chris.

In response to

pgsql-general by date

Next:From: Pierre-Frédéric CaillaudDate: 2004-07-28 11:06:18
Subject: Re: Before/After trigger sequencing questiont
Previous:From: Michael GlaesemannDate: 2004-07-28 10:58:27
Subject: Re: Everlasting SQL query

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group