Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id 3B56A930E1E for ; Fri, 3 Aug 2012 06:18:54 -0300 (ADT) Received: from mail-gg0-f174.google.com ([209.85.161.174]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SxE1i-00054C-Rg for pgsql-performance@postgresql.org; Fri, 03 Aug 2012 09:18:53 +0000 Received: by ggnl2 with SMTP id l2so568710ggn.19 for ; Fri, 03 Aug 2012 02:18:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=g7uk+GRHaYU6ZhvC/7cJ5NNJQg6YRu3J/uJyNvorxZY=; b=Eovjx6Jae9hABNVQGRt9z+1Hs8Frg4KyBnRzv0qLmzcNqanNFcQTcLgMV5PXolFkmB utaajFfu1pHKi4Z288NDZqnL7rCiYvUKhWoLYIUacYgrYDNrMtucmP4sT4wQfRf5T2Fq ASaZ1pWmdamBDx0CvbcHqtqKvtzlrZTAED1DG72/TZGNigbsfHsN/sWsnP4I6d17tmgI 9LGTReaKTvthn7kJ65P04aUVMzCcRBR3VWYogWhWqDioipHJpto2vMeLm/H21a3dWQLi ZJ8M30XlCQb5b6P8COYWfQFoRBLRlO4qh7rwp4TMi9IQTluVRd8zaQ+4lX1845ZjFYwN FBvg== MIME-Version: 1.0 Received: by 10.50.212.35 with SMTP id nh3mr2224814igc.19.1343985500773; Fri, 03 Aug 2012 02:18:20 -0700 (PDT) Received: by 10.64.89.74 with HTTP; Fri, 3 Aug 2012 02:18:20 -0700 (PDT) In-Reply-To: <8D0E5D045E36124A8F1DDDB463D548557CEF4D2F7D@mxsvr1.is.inps.co.uk> References: <8D0E5D045E36124A8F1DDDB463D548557CEF4D2F7D@mxsvr1.is.inps.co.uk> Date: Fri, 3 Aug 2012 11:18:20 +0200 Message-ID: Subject: Re: query using incorrect index From: Robert Klemme To: Russell Keane , pgsql-performance Content-Type: multipart/alternative; boundary=14dae9340bab5b5dac04c6590299 X-Pg-Spam-Score: -2.7 (--) X-Archive-Number: 201208/6 X-Sequence-Number: 47544 --14dae9340bab5b5dac04c6590299 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable On Thu, Aug 2, 2012 at 4:54 PM, Russell Keane wro= te: > ** ** > > Using PG 9.0 and given 2 queries (messageq_current is a view on the > messageq_table):**** > > ** ** > > select entity_id from messageq_current**** > > where entity_id =3D 123456;**** > > ** ** > > select entity_id from messageq_current**** > > where incoming =3D true**** > > and inactive =3D false**** > > and staff_ty =3D 2**** > > and staff_id =3D 2**** > > order by entity_id desc**** > > limit 1;**** > > ** ** > > and 2 indexes (there are 15 indexes in total but they are left out here > for brevity):**** > > ** ** > > messageq1:**** > > CREATE INDEX messageq1**** > > ON messageq_table**** > > USING btree**** > > (entity_id);**** > > ** ** > > And messageq4:**** > > ** ** > > CREATE INDEX messageq4**** > > ON messageq_table**** > > USING btree**** > > (inactive, staff_ty, staff_id, incoming, tran_dt);**** > > ** > Of course *a lot* of detail is missing (full schema of table, all the other indexes) but with "inactive" a boolean column I suspect selectivity might not be too good here and so having it as a first column in a covering index is at least questionable. If query 2 is frequent you might also want to consider creating a partial index only on (staff_ty, staff_id) with filtering criteria on incoming and active as present in query 2. Btw, why don't you formulate query 2 as max query? select max(entity_id) as entity_id from messageq_current where incoming =3D true and inactive =3D false and staff_ty =3D 2 and staff_id =3D 2; > ** > > With the messageq1 index present, query 1 is very quick (0.094ms) and > query 2 is very slow (241.515ms).**** > > If I remove messageq1 then query 2 uses messageq4 and is very quick > (0.098ms) but then query 1 must use a different index and is therefore > slower (> 5ms).**** > > ** ** > > So, to the Query plans:**** > Of which query? Shouldn't there be four plans in total? I'd post plans here: http://explain.depesz.com/ > With messageq1:**** > > "Limit (cost=3D0.00..2670.50 rows=3D1 width=3D4) (actual time=3D241.481.= .241.481 > rows=3D0 loops=3D1)"**** > > " Output: messageq_table.entity_id"**** > > " Buffers: shared hit=3D32 read=3D18870 written=3D12"**** > > " -> Index Scan Backward using messageq1 on > prac_live_10112.messageq_table (cost=3D0.00..66762.53 rows=3D25 width=3D= 4) > (actual time=3D241.479..241.479 rows=3D0 loops=3D1)"**** > > " Output: messageq_table.entity_id"**** > > " Filter: (messageq_table.incoming AND (NOT > messageq_table.inactive) AND (messageq_table.staff_ty =3D 2) AND > (messageq_table.staff_id =3D 2) AND > (aud_status_to_flag(messageq_table.aud_status) =3D 1))"**** > > " Buffers: shared hit=3D32 read=3D18870 written=3D12"**** > > "Total runtime: 241.515 ms"**** > > ** ** > > Without messageq1:**** > > "Limit (cost=3D12534.45..12534.45 rows=3D1 width=3D4) (actual time=3D0.0= 55..0.055 > rows=3D0 loops=3D1)"**** > > " Output: messageq_table.entity_id"**** > > " Buffers: shared read=3D3"**** > > " -> Sort (cost=3D12534.45..12534.51 rows=3D25 width=3D4) (actual > time=3D0.054..0.054 rows=3D0 loops=3D1)"**** > > " Output: messageq_table.entity_id"**** > > " Sort Key: messageq_table.entity_id"**** > > " Sort Method: quicksort Memory: 17kB"**** > > " -> Bitmap Heap Scan on prac_live_10112.messageq_table > (cost=3D174.09..12534.32 rows=3D25 width=3D4) (actual time=3D0.043..0.043= rows=3D0 > loops=3D1)"**** > > " Output: messageq_table.entity_id"**** > > " Recheck Cond: ((messageq_table.staff_ty =3D 2) AND > (messageq_table.staff_id =3D 2))"**** > > " Filter: (messageq_table.incoming AND (NOT > messageq_table.inactive) AND (aud_status_to_flag(messageq_table.aud_statu= s) > =3D 1))"**** > > " Buffers: shared read=3D3"**** > > " -> Bitmap Index Scan on messageq4 (cost=3D0.00..174.08 > rows=3D4920 width=3D0) (actual time=3D0.040..0.040 rows=3D0 loops=3D1)"**= ** > > " Index Cond: ((messageq_table.inactive =3D false) AND > (messageq_table.staff_ty =3D 2) AND (messageq_table.staff_id =3D 2) AND > (messageq_table.incoming =3D true))"**** > > " Buffers: shared read=3D3"**** > > "Total runtime: 0.098 ms"**** > > ** ** > > Clearly the statistics are off somehow but I really don=92t know where to > start.**** > > ** ** > > Any help you can give me would be very much appreciated.**** > Kind regards robert --=20 remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/ --14dae9340bab5b5dac04c6590299 Content-Type: text/html; charset=windows-1252 Content-Transfer-Encoding: quoted-printable

On Thu, Aug 2, 2012 at 4:54 PM, Russell = Keane <Russell.Keane@inps.co.uk> wrote:

=A0

Using PG 9.0 and given 2 queries (messageq_current i= s a view on the messageq_table):

=A0

select entity_id from messageq_current=

where entity_id =3D 123456;

=A0

select entity_id from messageq_current=

where incoming =3D true

and inactive =3D false

and staff_ty =3D 2

and staff_id =3D 2

order by entity_id desc

limit 1;

=A0

and 2 indexes (there are 15 indexes in total but the= y are left out here for brevity):

=A0

messageq1:

CREATE INDEX messageq1

=A0 ON messageq_table

=A0 USING btree

=A0 (entity_id);

=A0

And messageq4:

=A0

CREATE INDEX messageq4

=A0 ON messageq_table

=A0 USING btree

=A0 (inactive, staff_ty, staff_id, incoming, tran_dt= );


Of cour= se a lot of detail is missing (full schema of table, all the other i= ndexes) but with "inactive" a boolean column I suspect selectivit= y might not be too good here and so having it as a first column in a coveri= ng index is at least questionable.=A0 If query 2 is frequent you might also= want to consider creating a partial index only on (staff_ty, staff_id) wit= h filtering criteria on incoming and active as present in query 2.

Btw, why don't you formulate query 2 as max query?

select max(entity_id) as entity_id

from messageq_current

where incoming =3D true

and inactive =3D false

and staff_ty =3D 2

and staff_id =3D 2;

=A0

=A0

With the messageq1 index present, query 1 is very qu= ick (0.094ms) and query 2 is very slow (241.515ms).

If I remove messageq1 then query 2 uses messageq4 an= d is very quick (0.098ms) but then query 1 must use a different index and i= s therefore slower (> 5ms).

=A0

So, to the Query plans:


Of which query?=A0 Shouldn't there be four plans= in total?=A0 I'd post plans here:
http://explain.depesz.com/
=A0

With messageq1:

"Limit=A0 (cost=3D0.00..2670.50 rows=3D1 width= =3D4) (actual time=3D241.481..241.481 rows=3D0 loops=3D1)"

"=A0 Output: messageq_table.entity_id"<= /u>

"=A0 Buffers: shared hit=3D32 read=3D18870 writ= ten=3D12"

"=A0 ->=A0 Index Scan Backward using message= q1 on prac_live_10112.messageq_table=A0 (cost=3D0.00..66762.53 rows=3D25 wi= dth=3D4) (actual time=3D241.479..241.479 rows=3D0 loops=3D1)"

"=A0=A0=A0=A0=A0=A0=A0 Output: messageq_table.e= ntity_id"

"=A0=A0=A0=A0=A0=A0=A0 Filter: (messageq_table.= incoming AND (NOT messageq_table.inactive) AND (messageq_table.staff_ty =3D= 2) AND (messageq_table.staff_id =3D 2) AND (aud_status_to_flag(messageq_ta= ble.aud_status) =3D 1))"

"=A0=A0=A0=A0=A0=A0=A0 Buffers: shared hit=3D32= read=3D18870 written=3D12"

"Total runtime: 241.515 ms"<= /p>

=A0

Without messageq1:

"Limit=A0 (cost=3D12534.45..12534.45 rows=3D1 w= idth=3D4) (actual time=3D0.055..0.055 rows=3D0 loops=3D1)"

"=A0 Output: messageq_table.entity_id"<= /u>

"=A0 Buffers: shared read=3D3"

"=A0 ->=A0 Sort=A0 (cost=3D12534.45..12534.5= 1 rows=3D25 width=3D4) (actual time=3D0.054..0.054 rows=3D0 loops=3D1)"= ;

"=A0=A0=A0=A0=A0=A0=A0 Output: messageq_table.e= ntity_id"

"=A0=A0=A0=A0=A0=A0=A0 Sort Key: messageq_table= .entity_id"

"=A0=A0=A0=A0=A0=A0=A0 Sort Method:=A0 quicksor= t=A0 Memory: 17kB"

"=A0=A0=A0=A0=A0=A0=A0 ->=A0 Bitmap Heap Sca= n on prac_live_10112.messageq_table=A0 (cost=3D174.09..12534.32 rows=3D25 w= idth=3D4) (actual time=3D0.043..0.043 rows=3D0 loops=3D1)"

"=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 Output= : messageq_table.entity_id"

"=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 Rechec= k Cond: ((messageq_table.staff_ty =3D 2) AND (messageq_table.staff_id =3D 2= ))"

"=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 Filter= : (messageq_table.incoming AND (NOT messageq_table.inactive) AND (aud_statu= s_to_flag(messageq_table.aud_status) =3D 1))"

"=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 Buffer= s: shared read=3D3"

"=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 ->= =A0 Bitmap Index Scan on messageq4=A0 (cost=3D0.00..174.08 rows=3D4920 widt= h=3D0) (actual time=3D0.040..0.040 rows=3D0 loops=3D1)"<= /p>

"=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0 Index Cond: ((messageq_table.inactive =3D false) AND (messageq= _table.staff_ty =3D 2) AND (messageq_table.staff_id =3D 2) AND (messageq_ta= ble.incoming =3D true))"

"=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0 Buffers: shared read=3D3"

"Total runtime: 0.098 ms"

=A0

Clearly the statistics are off somehow but I really = don=92t know where to start.

=A0

Any help you can give me would be very much apprecia= ted.


Kind regards

robert

-- remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
--14dae9340bab5b5dac04c6590299--