Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id D312D633127 for ; Wed, 6 Jan 2010 21:58:37 -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 07020-09 for ; Thu, 7 Jan 2010 01:58:19 +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 E0FD26328BC for ; Wed, 6 Jan 2010 21:58:26 -0400 (AST) Received: by fxm27 with SMTP id 27so3313709fxm.3 for ; Wed, 06 Jan 2010 17:58:24 -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=jm687bcMSkh6SRaWw2eLmKYF2adZ2BJ3dwam4NPDumU=; b=SL6dDJUUS1Mu+WeuIdS0QxC6LcmswYNtOxIjlALid8Uye29om8UIk3zoVjMBssWhSt SlweScvr9jZ7YhLkQp4WP9YI8ypkNlur4YH2Pb6fUjM1pT1ISMLgatlg5T0KD4M1GEvk oEQH3OAVfnr+eD781WAiZO5/45GacSJm5BQkY= 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=BF8ygS9Y3+ottZrTYYGPfWVT9UKSlAsNewyl8upmVJNnW8LqZSyC1Ntg0mgCNcKCUX CWCKbPugw+l+bNVkaoK37rubqZViT6UudCDhlTs3Jq+WL5dthFaRz393w6fxV77uS5ea Ob/YC8khp4VmRplwsSWH/PkpPQZ6rygb6Aht8= MIME-Version: 1.0 Received: by 10.239.163.143 with SMTP id p15mr1974665hbd.135.1262829504693; Wed, 06 Jan 2010 17:58:24 -0800 (PST) In-Reply-To: <4B44E02A.6080109@krogh.cc> References: <4B44E02A.6080109@krogh.cc> Date: Wed, 6 Jan 2010 20:58:24 -0500 Message-ID: <603c8f071001061758x2dc16cffhc006517ab57f1eea@mail.gmail.com> Subject: Re: Digesting explain analyze From: Robert Haas To: Jesper Krogh 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=0.429 tagged_above=-10 required=5 tests=AWL=-2.760, BAYES_50=0.001, FH_DATE_PAST_20XX=3.188 X-Spam-Level: X-Archive-Number: 201001/68 X-Sequence-Number: 36948 On Wed, Jan 6, 2010 at 2:10 PM, Jesper Krogh wrote: > Hi. > > I have a table that consists of somewhere in the magnitude of 100.000.000 > rows and all rows are of this tuples > > (id1,id2,evalue); > > Then I'd like to speed up a query like this: > > explain analyze select id from table where id1 =3D 2067 or id2 =3D 2067 o= rder > by evalue asc limit 100; > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0QUERY PLAN > -------------------------------------------------------------------------= ---------------------------------------------------------------------- > =A0Limit =A0(cost=3D1423.28..1423.28 rows=3D100 width=3D12) (actual > time=3D2.565..2.567 rows=3D100 loops=3D1) > =A0 -> =A0Sort =A0(cost=3D1423.28..1424.54 rows=3D505 width=3D12) (actual > time=3D2.560..2.560 rows=3D100 loops=3D1) > =A0 =A0 =A0 =A0 Sort Key: evalue > =A0 =A0 =A0 =A0 Sort Method: =A0top-N heapsort =A0Memory: 25kB > =A0 =A0 =A0 =A0 -> =A0Bitmap Heap Scan on table =A0(cost=3D16.58..1420.75= rows=3D505 > width=3D12) (actual time=3D0.709..1.752 rows=3D450 loops=3D1) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 Recheck Cond: ((id1 =3D 2067) OR (id2 =3D 206= 7)) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 -> =A0BitmapOr =A0(cost=3D16.58..16.58 rows= =3D506 width=3D0) (actual > time=3D0.676..0.676 rows=3D0 loops=3D1) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 -> =A0Bitmap Index Scan on id1_ev= alue_idx > (cost=3D0.00..11.44 rows=3D423 width=3D0) (actual > time=3D0.599..0.599 rows=3D450 loops=3D1) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Index Cond: (id1_id = =3D 2067) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 -> =A0Bitmap Index Scan on id2_ev= alue_idx > (cost=3D0.00..4.89 rows=3D83 width=3D0) (actual > time=3D0.070..0.070 rows=3D1 loops=3D1) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Index Cond: (id2_id = =3D 2067) > =A0Total runtime: 2.642 ms > (12 rows) > > > What I had expected was to see the "Bitmap Index Scan on id1_evalue_idx" > to chop it off at a "limit 1". The inner sets are on average 3.000 for > both id1 and id2 and a typical limit would be 100, so if I could convince > postgresql to not fetch all of them then I would reduce the set retrieved > by around 60. The dataset is quite large so the random query is not very > likely to be hitting the same part of the dataset again, so there is goin= g > to be a fair amount of going to disk., > > I would also mean that using it in a for loop in a stored-procedure in > plpgsql it would not get any benefit from the CURSOR effect? > > I actually tried to stuff id1,id2 into an array and do a GIST index on th= e > array,evalue hoping that it directly would satisfy this query.. it used > the GIST index fetch the rows the post-sorting and limit on the set. > > What it boils down to is more or less: > > Does a "bitmap index scan" support ordering and limit ? > Does a "multicolummn gist index" support ordering and limit ? > > Have I missed anything that can hugely speed up fetching these (typically > 100 rows) from the database. Bitmap index scans always return all the matching rows. It would be nice if they could fetch them in chunks for queries like this, but they can't. I am not sure whether there's any way to make GIST do what you want. You might try something like this (untested): SELECT * FROM ( select id from table where id1 =3D 2067 order by evalue asc limit 100 union all select id from table where id2 =3D 2067 order by evalue asc limit 100 ) x ORDER BY evalue LIMIT 100 If you have an index by (id1, evalue) and by (id2, evalue) then I would think this would be pretty quick, as it should do two index scans (not bitmap index scans) to fetch 100 rows for each, then append the results, sort them, and then limit again. ...Robert