pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Steven Lembark <lembark@wrkhors.com>
To: Dominique Devienne <ddevienne@gmail.com>
Cc: David Wheeler <hippysoyboy@gmail.com>
Cc: pgsql-general@postgresql.org
Cc: lembark@wrkhors.com
Subject: Re: Return rows in input array's order?
Date: Tue, 9 May 2023 17:11:04 -0400
Message-ID: <20230509171104.583e6435.lembark@wrkhors.com> (raw)
In-Reply-To: <CAFCRh-_w_b-12z1LO3u6C_enjbzGsSVN6PEZ4aZ5HYzBfdmioA@mail.gmail.com>
References: <CAFCRh-9qxbK5cwe=vooEaTPw+wZ1xQ+FDDnpWqRYEqzDwnSdgA@mail.gmail.com>
	<5C5270F1-4323-458F-99C2-1D187039EA9C@gmail.com>
	<CAFCRh-_w_b-12z1LO3u6C_enjbzGsSVN6PEZ4aZ5HYzBfdmioA@mail.gmail.com>

On Tue, 9 May 2023 11:37:29 +0200
Dominique Devienne <ddevienne@gmail.com> wrote:

> On Tue, May 9, 2023 at 11:23 AM David Wheeler <hippysoyboy@gmail.com>
> wrote:
> 
> > > Hi. With an integer identity primary key table,
> > > we fetch a number of rows with WHERE id = ANY($1),
> > > with $1 an int[] array. The API using that query must return
> > > rows in the input int[] array order, and uses a client-side
> > > mapping to achieve that currently.
> > >
> > > Is it possible to maintain $1's order directly in SQL?
> > > Efficiently?  
> >
> > We’ve done this before with an “order by array_index(id,
> > input_array)”. I forget the actual function consider that pseudo
> > code 
> 
> Thanks David. I see how this would work.
> 
> It was only used for small arrays but never noticed any performance
> issues
> 

Depending on your PG version:

    Create a temp table via unnest, join that with what you need 
    and order by tmp.seq.

Forgot which version allows inlining of CTE's but you can 
use a CTE (12?):

    with int_seq
    as
    (
        select  unnest( int_array_col ) "order_by"
        from    whatever
        where   blah
    )
    select
        <whatever>
    from
        foobar  a
        join 
        int_seq b
        on 
        a.foo = b.order_by
    order by
        b.order_by
      , <whatever else>
        

This dodges the tmp table and the optimizer can inline the 
results, probably gets you the fastest result. 


-- 
Steven Lembark
Workhorse Computing
lembark@wrkhors.com
+1 888 359 3508





view thread (11+ messages)  latest in thread

Message-ID: <20230509171104.583e6435.lembark@wrkhors.com>
Permalink:  ../20230509171104.583e6435.lembark@wrkhors.com/
Also on:    postgresql.org/message-id/20230509171104.583e6435.lembark@wrkhors.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: lembark@wrkhors.com, ddevienne@gmail.com, hippysoyboy@gmail.com
  Subject: Re: Return rows in input array's order?
  In-Reply-To: <20230509171104.583e6435.lembark@wrkhors.com>
  References: <CAFCRh-9qxbK5cwe=vooEaTPw+wZ1xQ+FDDnpWqRYEqzDwnSdgA@mail.gmail.com> <5C5270F1-4323-458F-99C2-1D187039EA9C@gmail.com> <CAFCRh-_w_b-12z1LO3u6C_enjbzGsSVN6PEZ4aZ5HYzBfdmioA@mail.gmail.com> <20230509171104.583e6435.lembark@wrkhors.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox