Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id B1B1463272D for ; Sat, 9 Jan 2010 08:52:44 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 77349-01 for ; Sat, 9 Jan 2010 12:52:26 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-fx0-f227.google.com (mail-fx0-f227.google.com [209.85.220.227]) by mail.postgresql.org (Postfix) with ESMTP id A40046339E4 for ; Sat, 9 Jan 2010 08:52:33 -0400 (AST) Received: by fxm27 with SMTP id 27so5414071fxm.3 for ; Sat, 09 Jan 2010 04:52:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=WCKGVP1hXKC5rfNMmBAQTZPW/QjgqDzws505Qa5BlV0=; b=o7Gk+D1iBglEGU99IM+DCIaWVYoxvJ2V4QuMNMtn0yokmWBNGJ3UJ/OwNutbBSD+j1 9KI2o3cnF+j6B2/Nuj7IX9KONInj99Ah+igZMgUcgy//P5Fm0gQrKajG5fy+LnFAB9cd WMymtZRk4GOiEVPAVUmt0OGZHrwEL5h/r1sjE= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=ZMsAeAbzuo8MXusiVrru0NCdwlEd1jPp9xSk0ijenKI4jK3C00y3AvOENw433Eau3A Ob5+fBQjAl74nd+xyU5Vv7cJHSSIIpelxP9QMOC3OxXcpKnfZS0/qptA+BjwErBJdj11 v8Nv8F1fsgGYtcjTE2SyJI9vCGJzHW0dEYvvQ= MIME-Version: 1.0 Received: by 10.239.136.143 with SMTP id h15mr1441463hbh.171.1263041551325; Sat, 09 Jan 2010 04:52:31 -0800 (PST) In-Reply-To: <4B487A8F.6000003@cam.ac.uk> References: <4B487A8F.6000003@cam.ac.uk> Date: Sat, 9 Jan 2010 13:52:31 +0100 Message-ID: <852badbc1001090452w6c5295bve1cee840745bbba5@mail.gmail.com> Subject: Re: Joint index including MAX() ? From: Lefteris To: Richard Neill Cc: pgsql-performance@postgresql.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.972 tagged_above=-10 required=5 tests=AWL=0.627, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 201001/165 X-Sequence-Number: 37045 Hi, I first suggestion would be to either build the index only on parcel_id_code or on (parcel_id_code, id). But I am not sure because I am new in pg:) cheers, lefteris On Sat, Jan 9, 2010 at 1:46 PM, Richard Neill wrote: > Dear All, > > I'm trying to optimise the speed of some selects with the where condition= : > > WHERE id =3D > =A0(SELECT MAX(id) FROM tbl_sort_report WHERE parcel_id_code=3D'43024') > > > This is relatively slow, taking about 15-20ms, even though I have a joint > index on both fields: > > CREATE INDEX testidx3 ON tbl_sort_report (id, parcel_id_code); > > > So, my question is, is there any way to improve this? I'd expect that an > index on =A0 ( max(id),parcel_id_code ) would be ideal, excepting that > postgres won't allow that (and such an index probably doesn't make much > conceptual sense). > > > Explain Analyze is below. > > Thanks, > > Richard > > > > Here is part of the schema. id is the primary key; parcel_id_code loops f= rom > 0...99999 and back again every few hours. > > fsc_log=3D> \d tbl_sort_report > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Ta= ble "public.tbl_sort_report" > =A0 =A0 =A0 =A0Column =A0 =A0 =A0 =A0| =A0 =A0 =A0 =A0 =A0 Type =A0 =A0 = =A0 =A0 =A0 | =A0Modifiers > ----------------------+--------------------------+-----------------------= ------------------------------ > =A0id =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 | bigint =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 | not null default > nextval('master_id_seq'::regclass) > =A0timestamp =A0 =A0 =A0 =A0 =A0 =A0| timestamp with time zone | > =A0parcel_id_code =A0 =A0 =A0 | integer =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0| > (etc) > > > > > EXPLAIN ANALYZE (SELECT MAX(id) FROM tbl_sort_report WHERE > parcel_id_code=3D'43024'); > > QUERY PLAN > -------------------------------------------------------------------------= --------------------------------------------------------------------------- > =A0Result =A0(cost=3D7.34..7.35 rows=3D1 width=3D0) (actual time=3D17.712= ..17.714 rows=3D1 > loops=3D1) > =A0 InitPlan 1 (returns $0) > =A0 =A0 -> =A0Limit =A0(cost=3D0.00..7.34 rows=3D1 width=3D8) (actual tim= e=3D17.705..17.705 > rows=3D0 loops=3D1) > =A0 =A0 =A0 =A0 =A0 -> =A0Index Scan Backward using testidx3 on tbl_sort_= report > (cost=3D0.00..14.67 rows=3D2 width=3D8) (actual time=3D17.700..17.700 row= s=3D0 > loops=3D1) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Index Cond: (parcel_id_code =3D 43024) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Filter: (id IS NOT NULL) > =A0Total runtime: 17.786 ms > > > -- > Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org= ) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-performance >