Poor Query - Mailing list pgsql-performance

From Pallav Kalva
Subject Poor Query
Date
Msg-id 41B47A97.1090708@deg.cc
Whole thread Raw
Responses Re: Poor Query
List 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





pgsql-performance by date:

Previous
From: Pierre-Frédéric Caillaud
Date:
Subject: Re: Config Check
Next
From: Pierre-Frédéric Caillaud
Date:
Subject: Re: Poor Query