From: Pierre-Frédéric Caillaud
Subject: Re: Poor Query
Date: ,
Msg-id: opsilkfkrucq72hf@musicbox
(view: Whole thread, Raw)
In response to: Re: Poor Query  (Pallav Kalva)
Responses: Re: Poor Query  (Pallav Kalva)
List: pgsql-performance

Tree view

Poor Query  (Pallav Kalva, )
 Re: Poor Query  (Pierre-Frédéric Caillaud<>, )
  Re: Poor Query  (Pallav Kalva, )
   Re: Poor Query  (Pierre-Frédéric Caillaud<>, )
    Re: Poor Query  (Pallav Kalva, )
     Re: Poor Query  (Pierre-Frédéric Caillaud<>, )
      Re: Poor Query  (Pallav Kalva, )
       Re: Poor Query  (Pierre-Frédéric Caillaud<>, )
        Re: Poor Query  (Pallav Kalva, )


> 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:

From: Pallav Kalva
Date:
Subject: Re: Poor Query
From: "Rosny"
Date:
Subject: Re: TableSpace Design issues on Postgres 8.0 beta 5