Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g0Wns-0006PB-NA for pgsql-general@arkaria.postgresql.org; Thu, 13 Sep 2018 18:57:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1g0Wnq-00085p-Gq for pgsql-general@arkaria.postgresql.org; Thu, 13 Sep 2018 18:57:22 +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.89) (envelope-from ) id 1g0Wnq-00085i-2f for pgsql-general@lists.postgresql.org; Thu, 13 Sep 2018 18:57:22 +0000 Received: from mail-pf1-x42b.google.com ([2607:f8b0:4864:20::42b]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1g0Wnn-0002Rd-7i for pgsql-general@lists.postgresql.org; Thu, 13 Sep 2018 18:57:20 +0000 Received: by mail-pf1-x42b.google.com with SMTP id b11-v6so3091416pfo.3 for ; Thu, 13 Sep 2018 11:57:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=K8eS1ZNK9khjaUfbSasmosQvTT3csVahLOHqVyDhqUA=; b=g8bPga8n9RRfO/0yM3F1nG7mDibp63BA8q1qkO6KKK94G1hzgE+lKeb5VrEWIwiUwR diEMn1iY8GGzuYIVYhHrNq0lOBXA4V7zwA3/Fm6nQds+Pi16siS5eUfIcmROmzr/dN6T XzLxztezPmYKe9b7WWNycV/MXBzcj3CvGK3vFjXAI+v2AIZhXZbC9eO5PNrjc+Glmdin BpoGWWbwEq+YBM2PNYmM081Jh11jVxJrVB+I8tFZrP7PYh6+GtnXRcNzKiJ5ZVoZ5E1n S3FjOnYroLvvWFaPwnE8m33UISUe7SPr8FT76CCRllvaVY+cX9I++5kayexfp9aIuWqH IDpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=K8eS1ZNK9khjaUfbSasmosQvTT3csVahLOHqVyDhqUA=; b=V7QD/CpejdoDIJ45sX9MQ590wGQPh27CYlMulry8aHW/tBUDTwrgvYzLcBDltROuqa zqbTpKt5lx2R8N0MJbaFy3CYNe1YOsceP2g8cjht+VYiM2bSuwe9Efxsv6CdBibb+DKL kaaeEFUxWZCMRbCjPiXtcn2ds6DGzMx1kpHj8TZc0vrx4DADZ8l3FF7Nnimi6oLq2sXl /b5G3bPzbAJjScuBEEuZRIC+ajUPs2Fq33/9bNNrFEOo5KRkKKTvnEGPypPdivv3xcgO NUrzERT3nxdwz1PUWwSHQ+WtCX2RA+Jha9ingIsn0euuExiNpVQ7rSdIE0fvaP/oREth 1ANA== X-Gm-Message-State: APzg51AZreJAoT7SyHBEFF7inpDQ9zwP10snqZRjJ2FJPnHvi4qCSBV4 4uqJxI0bif9q5j+81xWvJ+0= X-Google-Smtp-Source: ANB0VdaR4JW1DfOwosfCN4glwVTZWW1oRtQ3H3dGvPZ/C03GC4fCYW2LbOOKJCEm2bZy8Yxn4LLQSQ== X-Received: by 2002:a63:3f45:: with SMTP id m66-v6mr8268262pga.51.1536865037471; Thu, 13 Sep 2018 11:57:17 -0700 (PDT) Received: from kitselas.hci.utah.edu ([155.100.47.2]) by smtp.gmail.com with ESMTPSA id y124-v6sm6812306pfg.63.2018.09.13.11.57.16 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 13 Sep 2018 11:57:16 -0700 (PDT) From: Rob Sargent Message-Id: Content-Type: multipart/alternative; boundary="Apple-Mail=_D88DC331-9050-49A7-B03D-B7B69A70E78E" Mime-Version: 1.0 (Mac OS X Mail 11.5 \(3445.9.1\)) Subject: Re: Can I add Index to make a query faster which involves joins on unnest ? Date: Thu, 13 Sep 2018 12:57:15 -0600 In-Reply-To: <3FA56263-735B-4146-B6F5-0003C1A953C4@zeit.io> Cc: pgsql-general@lists.postgresql.org To: Arup Rakshit References: <3FA56263-735B-4146-B6F5-0003C1A953C4@zeit.io> X-Mailer: Apple Mail (2.3445.9.1) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --Apple-Mail=_D88DC331-9050-49A7-B03D-B7B69A70E78E Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii > On Sep 13, 2018, at 12:17 PM, Arup Rakshit wrote: >=20 > The below query basically gives the result by maintaining the order of = the sizes in the list. >=20 > explain analyze select > "price_levels"."name", > "price_levels"."size" > from > "price_levels" > join unnest(array['M', > 'L', > 'XL', > '2XL', > '3XL', > '4XL', > '5XL', > '6XL', > 'S']) with ordinality t(size, > ord) > using (size) > order by > t.size >=20 >=20 > I have a Btree index on the size column. >=20 > Explain output is: >=20 > Merge Join (cost=3D4.61..5165.38 rows=3D60000 width=3D46) (actual = time=3D0.157..57.872 rows=3D60000 loops=3D1) > Merge Cond: ((price_levels.size)::text =3D t.size) > -> Index Scan using price_levels_size_idx on price_levels = (cost=3D0.29..4111.05 rows=3D60000 width=3D14) (actual = time=3D0.044..25.941 rows=3D60000 loops=3D1) > -> Sort (cost=3D4.32..4.57 rows=3D100 width=3D32) (actual = time=3D0.108..3.946 rows=3D53289 loops=3D1) > Sort Key: t.size > Sort Method: quicksort Memory: 25kB > -> Function Scan on unnest t (cost=3D0.00..1.00 rows=3D100 = width=3D32) (actual time=3D0.030..0.033 rows=3D9 loops=3D1) > Planning time: 0.667 ms > Execution time: 62.846 ms >=20 >=20 >=20 > Thanks, >=20 > Arup Rakshit > ar@zeit.io >=20 >=20 There are not value of size fit it to be a worthwhile key. >=20 --Apple-Mail=_D88DC331-9050-49A7-B03D-B7B69A70E78E Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=us-ascii

On Sep 13, 2018, at 12:17 PM, Arup Rakshit <ar@zeit.io> wrote:

The = below query basically gives the result by maintaining the order of the = sizes in the list.

explain analyze select
        "price_levels"."name",
        "price_levels"."size"
from
        = "price_levels"
join unnest(array['M',
        'L',
  =       'XL',
      =   '2XL',
        = '3XL',
        '4XL',
        '5XL',
  =       '6XL',
      =   'S']) with ordinality t(size,
    =     ord)
        =         using (size)
order = by
        = t.size

I have a Btree index on the size = column.

Explain = output is:

Merge Join  (cost=3D4.61..5165.38 rows=3D60000 width=3D46)= (actual time=3D0.157..57.872 rows=3D60000 loops=3D1)
  Merge Cond: ((price_levels.size)::text =3D = t.size)
  ->  Index Scan using = price_levels_size_idx on price_levels  (cost=3D0.29..4111.05 = rows=3D60000 width=3D14) (actual time=3D0.044..25.941 rows=3D60000 = loops=3D1)
  ->  Sort =  (cost=3D4.32..4.57 rows=3D100 width=3D32) (actual = time=3D0.108..3.946 rows=3D53289 loops=3D1)
  =       Sort Key: t.size
    =     Sort Method: quicksort  Memory: 25kB
        ->  Function Scan on = unnest t  (cost=3D0.00..1.00 rows=3D100 width=3D32) (actual = time=3D0.030..0.033 rows=3D9 loops=3D1)
Planning = time: 0.667 ms
Execution time: 62.846 = ms



Thanks,

Arup Rakshit

There = are not value of size fit it to be a worthwhile key.


= --Apple-Mail=_D88DC331-9050-49A7-B03D-B7B69A70E78E--