From: Pallav Kalva <pkalva@deg.cc>
To: pgsql-performance@postgresql.org
Subject: Poor Query
Date: Mon, 06 Dec 2004 10:28:23 -0500
Message-ID: <41B47A97.1090708@deg.cc> (raw)
Hi Everybody,
I have a performance problem with this query , it takes lot of time
on the production database. is there a way to improve it ? i do vacuumdb
on this database and do anlyze on the users table separately daily
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
QUERY PLAN
-----------------------------------------------------------------------------------------------------
Limit (cost=0.00..12752.61 rows=500 width=120)
-> Index Scan Backward using users_pkey on users u
(cost=0.00..2460462.79 rows=96469 width=120)
Filter: ((subplan) AND (subplan))
SubPlan
-> Index Scan using bankaccount_pkey on bankaccount ba
(cost=0.00..3.07 rows=1 width=0)
Index Cond: (bankaccountid = $1)
Filter: (routingnumber = '12345678'::text)
-> Index Scan using bankaccount_pkey on bankaccount ba
(cost=0.00..3.07 rows=1 width=0)
Index Cond: (bankaccountid = $1)
Filter: (accountnumber = '12345678'::text)
I tried changing it but it still takes lot of time
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 ba.routingNumber = '12345678')
order by UserID desc
limit 500
QUERY PLAN
-------------------------------------------------------------------------------------------------------
Limit (cost=0.00..3309.62 rows=500 width=120)
-> Index Scan Backward using users_pkey on users u
(cost=0.00..1277101.86 rows=192938 width=120)
Filter: (subplan)
SubPlan
-> Index Scan using bankaccount_pkey on bankaccount ba
(cost=0.00..3.07 rows=1 width=0)
Index Cond: (bankaccountid = $1)
Filter: ((accountnumber = '12345678'::text) AND
(routingnumber = '12345678'::text))
the users_pkey index on the primary key userid is on users table. it
seems to be using index but it still takes lot of time.
here is the output from the pg_class for the users and bankaccount
table . Table doesnt have lot of records but this query take anywhere
from 3 to 5 min to run which is really bad for us. Can we improve the
performance on this query ?
relname | relpages | reltuples
---------+----------+-----------
users | 39967 | 385875
bankaccount | 242 | 16453
Thanks!
Pallav
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: pkalva@deg.cc
Subject: Re: Poor Query
In-Reply-To: <41B47A97.1090708@deg.cc>
* 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