Re: Poor Query - Mailing list pgsql-performance

From Pierre-Frédéric Caillaud
Subject Re: Poor Query
Date
Msg-id opsilkfkrucq72hf@musicbox
Whole thread Raw
In response to Re: Poor Query  (Pallav Kalva <pkalva@deg.cc>)
Responses Re: Poor Query  (Pallav Kalva <pkalva@deg.cc>)
List 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...

pgsql-performance by date:

Previous
From: Josh Berkus
Date:
Subject: Processor optimization compile options?
Next
From: Pallav Kalva
Date:
Subject: Re: Poor Query