pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
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 18:34:22 +0100
Message-ID: <opsilkfkrucq72hf@musicbox> (raw)
In-Reply-To: <41B48ADB.7000100@deg.cc>
References: <41B47A97.1090708@deg.cc>
	<opsilfifducq72hf@musicbox>
	<41B48053.7090805@deg.cc>
	<opsilgs8b4cq72hf@musicbox>
	<41B48ADB.7000100@deg.cc>



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



view thread (9+ messages)  latest in thread

Message-ID: <opsilkfkrucq72hf@musicbox>
Permalink:  ../opsilkfkrucq72hf@musicbox/
Also on:    postgresql.org/message-id/opsilkfkrucq72hf@musicbox

 · 

reply

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