Re: Poor Query

From: Pierre-Frédéric Caillaud <lists(at)boutiquenumerique(dot)com>
To: "Pallav Kalva" <pkalva(at)deg(dot)cc>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: Poor Query
Date: 2004-12-06 17:34:22
Message-ID: opsilkfkrucq72hf@musicbox
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

> Just One, user can i have only one bankaccount.

Ah well, in that case :
This is your query :

select userID, fname, lname, email, phone, dateEntered, dateCanceled,
dateSuspended, billAmount, billDate, dateBilled, datePaid, '?' as searches
from Users u
where 1=1 AND exists (select userID
from bankaccount ba
where ba.bankaccountID = u.bankaccountID
and ba.accountnumber = '12345678')
AND exists (select userID
from bankaccount ba
where ba.bankaccountID = u.bankaccountID
and ba.routingNumber = '12345678')
order by UserID desc
limit 500

What it does is scan all users, and for each user, test if it has the
accountnumber or the routingNumber you seek. You're reversing the problem
: you should first look for accountnumber and routingNumber, THEN look for
the user :

SELECT * FROM Users WHERE bankaccountID IN
(SELECT bankaccountID FROM bankaccount WHERE accountnumber = '12345678'
OR/AND routingNumber = '12345678')

or :

SELECT * FROM Users WHERE userID IN
(SELECT userID FROM bankaccount WHERE accountnumber = '12345678' OR/AND
routingNumber = '12345678')

There is something very strange in your query, it seems that bankaccount
and Users both have a UserID column and a bankaccountID column. Is this
normal ? It looks denormalized to me...

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Pallav Kalva 2004-12-06 18:00:27 Re: Poor Query
Previous Message Josh Berkus 2004-12-06 17:26:34 Processor optimization compile options?