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 1g5u5B-0003Gv-L5 for pgsql-general@arkaria.postgresql.org; Fri, 28 Sep 2018 14:49:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1g5u59-0000RB-8l for pgsql-general@arkaria.postgresql.org; Fri, 28 Sep 2018 14:49:27 +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 1g5u58-0000R4-LJ for pgsql-general@lists.postgresql.org; Fri, 28 Sep 2018 14:49:27 +0000 Received: from mail-pf1-x444.google.com ([2607:f8b0:4864:20::444]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1g5u50-0000FG-B1 for pgsql-general@postgresql.org; Fri, 28 Sep 2018 14:49:25 +0000 Received: by mail-pf1-x444.google.com with SMTP id d4-v6so4519457pfn.0 for ; Fri, 28 Sep 2018 07:49:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zeit-io.20150623.gappssmtp.com; s=20150623; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=T0W0dq1D0HdxyCq/YvS4bP5RoQ7PgrM6s8nfSFRWDKw=; b=VaaF+jSh1MBTrdu8cp/SWoh281wry/mMlTm8G2Cqk9z6agRtmlQQUDkGO/e03LL7/m f1SIOfWg9XgK1FjqAb09dB5S6HsozvehigCI8rvpAMpUrF4K0opNImClrQnI3tRgv593 X+f2p7BBY0YcGAr1EE63nLHCiMDK9B5VmLCUOYkMeMHI/WiD8zEsPs3h2h7MIOFuLsHe KRxiD1L8gSySwPLL6G6Me3KmrSnMOUhNDJvmHTd69SLpvqrAPHbTMEVV5sW4zzYu2Z97 r8Osaxt+DQBIrelgcUNh8lTYJ1qfI9szeDc2LgeKZzIKMVoZXoAP/oAZadyK4/ldysK3 3d3Q== 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=T0W0dq1D0HdxyCq/YvS4bP5RoQ7PgrM6s8nfSFRWDKw=; b=o0JIRjjgxZ1fk6B5R3V77EX4yI0KBXV5eK6JVe6/TiiD8bHvnmF/o+x03LEiiDgWDx jGT12RKKv7OhHG1/hcYfB6zhlMM+Hd4WxK3fZaGz66WxY0yeGL7CPYPl6B3rJK6Vfz+2 4HXLL2J8mzbT1l+6hGrq6vfSyrE48qLT6z4YP16Ub7hepjUZsuRGo9wgzb4JCzZo5s95 v8uCGP0NR98/NbtAf2M8sHpu0b258M15X92rah+ZDnFY7a8y47//GdRey9ou2MbL5Ql/ BBV5FcRk8GtZLjNHv6URL0GRsGsBPIEslWBWN9d0yFdskCJObQmb7zOeAfCsScj2QGmQ kSYw== X-Gm-Message-State: ABuFfoh1IvBTaLO0tKqrY9GtU7GYy342Yq0nUG7uf31swWsi8056sq06 CJ4lbwHIPxWATvQSdrtXc8Skh+0CBAI= X-Google-Smtp-Source: ACcGV63R4vI5giWpiIUt+KPKFMY3aT4XdXn29/ctHjmtkFVKSvpahkos4/mEj3yMOgpHkhGYObQl1Q== X-Received: by 2002:a62:6547:: with SMTP id z68-v6mr17238519pfb.20.1538146157101; Fri, 28 Sep 2018 07:49:17 -0700 (PDT) Received: from [192.168.0.100] ([45.123.160.163]) by smtp.gmail.com with ESMTPSA id j184-v6sm8331844pge.77.2018.09.28.07.49.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 28 Sep 2018 07:49:16 -0700 (PDT) From: Arup Rakshit Message-Id: <97DDEE09-FE33-43DB-A451-665F3535B5BA@zeit.io> Content-Type: multipart/alternative; boundary="Apple-Mail=_097A9338-90D1-4622-8BB5-3AF2638CDCA5" Mime-Version: 1.0 (Mac OS X Mail 11.5 \(3445.9.1\)) Subject: Re: Why my query not using index to sort? Date: Fri, 28 Sep 2018 20:19:12 +0530 In-Reply-To: <20311.1538141875@sss.pgh.pa.us> Cc: pgsql-general@postgresql.org To: Tom Lane References: <20311.1538141875@sss.pgh.pa.us> X-Mailer: Apple Mail (2.3445.9.1) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --Apple-Mail=_097A9338-90D1-4622-8BB5-3AF2638CDCA5 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Hello Tom, Here is the plan with `enable_sort =3D off`. # set enable_sort =3D off; = = SET # explain analyze select "vessels" .* from "vessels" where = "vessels"."deleted_at" is null and "vessels"."company_id" =3D = '86529964-6e9b-4bfa-ba9e-62bd24eaa954' order by "vessels"."name" ASC; QUERY PLAN = --------------------------------------------------------------------------= ----------------------------------------------------------- Index Scan using inspector_tool_idx20_1 on vessels (cost=3D0.27..40.76 = rows=3D76 width=3D107) (actual time=3D0.047..0.120 rows=3D77 loops=3D1) Index Cond: (company_id =3D = '86529964-6e9b-4bfa-ba9e-62bd24eaa954'::uuid) Filter: (deleted_at IS NULL) Rows Removed by Filter: 4 Planning time: 1.867 ms Execution time: 0.252 ms (6 rows) Why it is showing *6 rows*? Also it seems less than what I had before: # explain analyze select "vessels" .* from "vessels" where = "vessels"."deleted_at" is null and "vessels"."company_id" =3D = '86529964-6e9b-4bfa-ba9e-62bd24eaa954' order by "vessels"."name" ASC; QUERY PLAN = --------------------------------------------------------------------------= ------------------------------------------------------------- Sort (cost=3D17.29..17.48 rows=3D76 width=3D107) (actual = time=3D0.789..0.796 rows=3D77 loops=3D1) Sort Key: name Sort Method: quicksort Memory: 38kB -> Bitmap Heap Scan on vessels (cost=3D4.90..14.91 rows=3D76 = width=3D107) (actual time=3D0.090..0.122 rows=3D77 loops=3D1) Recheck Cond: (company_id =3D = '86529964-6e9b-4bfa-ba9e-62bd24eaa954'::uuid) Filter: (deleted_at IS NULL) Rows Removed by Filter: 4 Heap Blocks: exact=3D3 -> Bitmap Index Scan on inspector_tool_idx20_1 = (cost=3D0.00..4.88 rows=3D81 width=3D0) (actual time=3D0.059..0.059 = rows=3D81 loops=3D1) Index Cond: (company_id =3D = '86529964-6e9b-4bfa-ba9e-62bd24eaa954'::uuid) Planning time: 1.743 ms Execution time: 0.954 ms (12 rows) Thanks, Arup Rakshit ar@zeit.io > On 28-Sep-2018, at 7:07 PM, Tom Lane wrote: >=20 > Arup Rakshit writes: >> My query is not using name index to sort the result. >=20 > Given the rowcounts here, I think the planner is making the right = choice. > Sorting 70-some rows with a Sort node is probably cheaper than doing > random disk I/O to get them in sorted order. With more rows involved, > it might make the other choice. >=20 > As a testing measure (don't do it in production!), you could set > enable_sort =3D off, which will force the planner to pick a non-Sort > plan if possible. Then you could see whether that's actually faster > or slower, and by how much. >=20 > regards, tom lane --Apple-Mail=_097A9338-90D1-4622-8BB5-3AF2638CDCA5 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=us-ascii
Hello Tom,

Here is the plan with `enable_sort =3D off`.

# set = enable_sort =3D off;               =                     =                     =                     =                     =                     =                     =                     = SET
# explain analyze select "vessels" .* from = "vessels" where "vessels"."deleted_at" is null and = "vessels"."company_id" =3D '86529964-6e9b-4bfa-ba9e-62bd24eaa954' order = by "vessels"."name" ASC;
      =                     =                     =                QUERY = PLAN
---------------------------------------------------------------= ----------------------------------------------------------------------
 Index Scan using inspector_tool_idx20_1 on = vessels  (cost=3D0.27..40.76 rows=3D76 width=3D107) (actual = time=3D0.047..0.120 rows=3D77 loops=3D1)
  =  Index Cond: (company_id =3D = '86529964-6e9b-4bfa-ba9e-62bd24eaa954'::uuid)
  =  Filter: (deleted_at IS NULL)
  =  Rows Removed by Filter: 4
 Planning = time: 1.867 ms
 Execution time: 0.252 = ms
(6 rows)

Why it is showing *6 rows*? Also it = seems less than what I had before:

# explain analyze = select "vessels" .* from "vessels" where "vessels"."deleted_at" is null = and "vessels"."company_id" =3D '86529964-6e9b-4bfa-ba9e-62bd24eaa954' = order by "vessels"."name" ASC;
      =                     =                     =                 QUERY = PLAN
---------------------------------------------------------------= ------------------------------------------------------------------------
 Sort  (cost=3D17.29..17.48 rows=3D76 = width=3D107) (actual time=3D0.789..0.796 rows=3D77 loops=3D1)
   Sort Key: name
  =  Sort Method: quicksort  Memory: 38kB
   ->  Bitmap Heap Scan on vessels =  (cost=3D4.90..14.91 rows=3D76 width=3D107) (actual = time=3D0.090..0.122 rows=3D77 loops=3D1)
  =        Recheck Cond: (company_id =3D = '86529964-6e9b-4bfa-ba9e-62bd24eaa954'::uuid)
  =        Filter: (deleted_at IS NULL)
         Rows Removed by Filter: = 4
         Heap Blocks: = exact=3D3
         -> =  Bitmap Index Scan on inspector_tool_idx20_1  (cost=3D0.00..4.88= rows=3D81 width=3D0) (actual time=3D0.059..0.059 rows=3D81 = loops=3D1)
          =      Index Cond: (company_id =3D = '86529964-6e9b-4bfa-ba9e-62bd24eaa954'::uuid)
 Planning time: 1.743 ms
 Execution time: 0.954 ms
(12 = rows)



Thanks,

Arup = Rakshit



On 28-Sep-2018, at 7:07 PM, Tom Lane <tgl@sss.pgh.pa.us> = wrote:

Arup Rakshit <ar@zeit.io> writes:
My query is not using name index to sort the = result.

Given the rowcounts = here, I think the planner is making the right choice.
Sorting 70-some rows with a Sort node is probably cheaper = than doing
random disk I/O to get them in sorted order. =  With more rows involved,
it might make the other = choice.

As a testing measure (don't do it = in production!), you could set
enable_sort =3D off, which = will force the planner to pick a non-Sort
plan if = possible.  Then you could see whether that's actually faster
or slower, and by how much.

regards, = tom lane

= --Apple-Mail=_097A9338-90D1-4622-8BB5-3AF2638CDCA5--