From: Pierre-Frédéric Caillaud <lists@boutiquenumerique.com>
To: Pallav Kalva <pkalva@deg.cc>
Cc: pgsql-performance@postgresql.org
Subject: Re: Poor Query
Date: Mon, 06 Dec 2004 21:18:28 +0100
Message-ID: <opsilr02qxcq72hf@musicbox> (raw)
In-Reply-To: <41B49E3B.7030407@deg.cc>
References: <41B47A97.1090708@deg.cc>
<opsilfifducq72hf@musicbox>
<41B48053.7090805@deg.cc>
<opsilgs8b4cq72hf@musicbox>
<41B48ADB.7000100@deg.cc>
<opsilkfkrucq72hf@musicbox>
<41B49E3B.7030407@deg.cc>
Your suffering comes from the "where ba.bankaccountID = u.bankaccountID"
in the subselect. It means postgres has to run the subselect once for each
row in Users. You want the subselect to run only once, and return one (or
more?) bankaccountid's, then fetch the users from Users.
Just remove the "where ba.bankaccountID = u.bankaccountID" !
> select userID, fname, lname, email, phone, dateEntered, dateCanceled,
> dateSuspended, billAmount, billDate, dateBilled, datePaid, '?' as
> searches
> from Users u
> where bankaccountid in (select bankaccountid
> from bankaccount ba
> where ba.bankaccountID = u.bankaccountID
> and ba.accountnumber = '12345678'
> and ba.routingNumber = '12345678')
> order by UserID desc
> limit 500
New version :
select userID, fname, lname, email, phone, dateEntered, dateCanceled,
dateSuspended, billAmount, billDate, dateBilled, datePaid, '?' as
searches
from Users u
where bankaccountid in (select bankaccountid
from bankaccount ba
WHERE ba.accountnumber = '12345678'
and ba.routingNumber = '12345678')
You could also do this :
select u.* from Users u, bankaccount ba
where u.bankaccountid = ba.bankaccountid
and ba.accountnumber = '12345678'
and ba.routingNumber = '12345678')
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: lists@boutiquenumerique.com, pkalva@deg.cc
Subject: Re: Poor Query
In-Reply-To: <opsilr02qxcq72hf@musicbox>
* 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