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

Poor Query

From: Pallav Kalva <pkalva(at)deg(dot)cc>
To: pgsql-performance(at)postgresql(dot)org
Subject: Poor Query
Date: 2004-12-06 15:28:23
Message-ID: 41B47A97.1090708@deg.cc (view raw or flat)
Thread:
Lists: pgsql-performance
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





Responses

pgsql-performance by date

Next:From: Pierre-Frédéric CaillaudDate: 2004-12-06 15:48:05
Subject: Re: Poor Query
Previous:From: Pierre-Frédéric CaillaudDate: 2004-12-06 15:18:00
Subject: Re: Config Check

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