Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dQCof-0001OB-Rv for pgsql-performance@arkaria.postgresql.org; Wed, 28 Jun 2017 13:15:34 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dQCof-00042c-DC for pgsql-performance@arkaria.postgresql.org; Wed, 28 Jun 2017 13:15:33 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dQCmv-0000tR-1q for pgsql-performance@postgresql.org; Wed, 28 Jun 2017 13:13:45 +0000 Received: from mail-pg0-x236.google.com ([2607:f8b0:400e:c05::236]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1dQCms-0004f5-Ga for pgsql-performance@postgresql.org; Wed, 28 Jun 2017 13:13:43 +0000 Received: by mail-pg0-x236.google.com with SMTP id j186so31714827pge.2 for ; Wed, 28 Jun 2017 06:13:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=yXQMxN1ICJP9t2NFxiXD95sfzQPoXDcezQQCStkYkHE=; b=qpBAjyOogJiE4EuG9W+mAjVIcr9en2kSvCRHgFuKIKXcbaur0mG07LbAr84tt65YLW O1I4FgeHPUAeQOGexxtUcdvbHrBNoJV4b+IssE3DQCD56q27NWew+l6nyceGvNr+NKdF yiGtNM1txR48WLNdbHB3RpP1Pv2IfFOtl9L53C40yQIDAfAsdbXVSR/ecXGlpyhKjvaA Tb4+qMXlru2HcJX723k9uocMTvnHc+JjWgf/o2KNEXjyZVR09p134g2+evk5FdJxIVqg uqz0d80BysXGoPj2grvZ8Ge/0ivJuMgr2WkP/7QGcw4KdGCrqFnB3PzTFy3JOZcd5+CB dkbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=yXQMxN1ICJP9t2NFxiXD95sfzQPoXDcezQQCStkYkHE=; b=UBFRkIsNAv6z5Dz2T2n0vTV3EaYHXcJRSUTWN4ZxCQUBnhfUUVL8L4KXk8SWOz+ndI fLTg10vePUOYvXuh5vTuVt3ajT5YVCZD4Hj9fw8quIMuUH6kFjkREa46YgukOQVt7iHk zILdN5B5VTRRCaIiYncHSl/5uvzSGxJ+zlIjqq/ba49CRo3UHrsjZwz1Tk4hX30r7jqO /IF2Ti99ZYbm+mR1VOqa5D9XXtrtmhbVjRv/lubcF+Dqgfu9eLTLf2aB1uRl5RPnuZ7a m3xUH4nprBTE4XViMVplQdHJF9TaJRS75DoRsZfPbq7/5Xhh1Q8LTPBNB4m5pd5n8Qnn wzbw== X-Gm-Message-State: AKS2vOxkcatohDVasT7kyf4e9FnV+9sjCyBgWmgmnM3yU1eF2XdF79xv a3arzPpGIq7SdUB32MU/imxcPKoHyw== X-Received: by 10.98.157.207 with SMTP id a76mr10773965pfk.25.1498655621164; Wed, 28 Jun 2017 06:13:41 -0700 (PDT) MIME-Version: 1.0 Received: by 10.100.160.202 with HTTP; Wed, 28 Jun 2017 06:13:40 -0700 (PDT) In-Reply-To: <18037.1498260805@sss.pgh.pa.us> References: <18037.1498260805@sss.pgh.pa.us> From: Merlin Moncure Date: Wed, 28 Jun 2017 08:13:40 -0500 Message-ID: Subject: Re: Efficiently merging and sorting collections of sorted rows To: Tom Lane Cc: Clint Miller , postgres performance list Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org On Fri, Jun 23, 2017 at 6:33 PM, Tom Lane wrote: > Clint Miller writes: >> That's a good plan because it's not doing a quick sort. Instead, it's ju= st >> reading the sort order off of the index, which is exactly what I want. (I >> had to disable enable_sort because I didn't have enough rows of test data >> in the table to get Postgres to use the index. But if I had enough rows, >> the enable_sort stuff wouldn't be necessary. My real table has lots of r= ows >> and doesn't need enable_sort turned off to do the sort with the index.) > > TBH, I think this whole argument is proceeding from false premises. > Using an indexscan as a substitute for an explicit sort of lots of > rows isn't all that attractive, because it implies a whole lot of > random access to the table (unless the table is nearly in index > order, which isn't a condition you can count on without expending > a lot of maintenance effort to keep it that way). seqscan-and-sort > is often a superior alternative, especially if you're willing to give > the sort a reasonable amount of work_mem. Hm, if he reverses the index terms he gets his sort order for free and a guaranteed IOS. This would only be sensible to do only if several conditions applied, you'd have to live under the IOS criteria generally, the number of rows returned to what relative to what was thrown out would have to be reasonably high (this is key), and the result set would have to be large making the sort an expensive consideration relative to the filtering. You'd also have to be uninterested in explicit filters on 's' or be willing to create another index to do that if you were. merlin postgres=3D# \d foo Table "public.foo" Column =E2=94=82 Type =E2=94=82 Modifiers =E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2= =94=BC=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=BC=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80= =E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80 s =E2=94=82 text =E2=94=82 i =E2=94=82 integer =E2=94=82 Indexes: "foo_i_s_idx" btree (i, s) -- reversed postgres=3D# set enable_sort =3D false; SET postgres=3D# explain analyze select * from foo where s =3D 'a' or s =3D 'b' order by i; QUERY PLAN =E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2= =94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80= =E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2= =94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80= =E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2= =94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80= =E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2= =94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80= =E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2= =94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80 Index Only Scan using foo_i_s_idx on foo (cost=3D0.15..68.75 rows=3D12 width=3D36) (actual time=3D0.004..0.004 rows=3D0 loops=3D1) Filter: ((s =3D 'a'::text) OR (s =3D 'b'::text)) Heap Fetches: 0 Planning time: 0.215 ms Execution time: 0.025 ms merlin --=20 Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance