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

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 (view raw or flat)
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

pgsql-performance by date

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

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