Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kcOfo-0006wJ-3M for pgsql-general@arkaria.postgresql.org; Tue, 10 Nov 2020 08:06:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kcOfm-0006iN-LE for pgsql-general@arkaria.postgresql.org; Tue, 10 Nov 2020 08:06:38 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kcOfm-0006iG-5T for pgsql-general@lists.postgresql.org; Tue, 10 Nov 2020 08:06:38 +0000 Received: from mail-wm1-x330.google.com ([2a00:1450:4864:20::330]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kcOfi-0005b4-OA for pgsql-general@lists.postgresql.org; Tue, 10 Nov 2020 08:06:36 +0000 Received: by mail-wm1-x330.google.com with SMTP id 23so1456437wmg.1 for ; Tue, 10 Nov 2020 00:06:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=datylon-com.20150623.gappssmtp.com; s=20150623; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=I+jmqXuRImT3V/n699IQZ9WWVEncWsfLkPbt4KCpf84=; b=Rx3TFYKmTAkqwHxEWNiPI5Sysddf053t/x+hWIL5V5HzrAkfoS/kO5BOk4lXBl4hZ0 lsrKPAMnD/1SyUQjeqCS4BB5JIhBrM15JREe1p72jeBC6SZTs5uUhfMF2SqQTdoICmkv jIwVCgBDEZcjo0u7yxeKMRd/SeWNMyttyCK6M2zwPJp8grzUev5usGWHzmjOG4xOV2dr kFRSrQ5VMqgTTkX+yt4mEHC7/JNHbAfg2KWJzu47VAteqodO7MvwJgPRbtrJPxxjjhZC IX+ae4z2NOTA4mjann2CwEPjuw9En3s5LGtoePFQLElk2D6owiwKQv9+Cb6jWQVnDT2M Ra0g== 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=I+jmqXuRImT3V/n699IQZ9WWVEncWsfLkPbt4KCpf84=; b=U6JJKgC0SJkq/m6nHk47qm8b/QuXGdBgiLNQwAaCQIF/MWQUhBsQXEMLs5qoewGsYD Hp8I+IQgTbpFBLJzy9YRC+yczFFsREujA7tM7sgafFXLjz9xngjnG0FMB5bUfdvUAzml AqqwT1Mm32ZGhj5BBQkUAJLqBViKQhzxwkwHuTH1scmxzzqR8m6zz7lgqJtaJr2gxpfR 4IesTPY+ElClpGUoUPh9Qn8oHuDH7F4Ay3Sa+BqEdeuyCQnusKyTE+0kuohxXy7G8ABz cHfDRzJPyd1FweY73foT88WiatwggpnSwJFeLz2gV/MIXLcqdUkiv4SrItQO0Z5o7OdZ C6Jw== X-Gm-Message-State: AOAM5327fqKPzqPTxw2YeqYNH4K1sescQeZbInjDvtDP5bT1CL+hI800 stNvtNf2SMc5/2UJOEo/row4yw== X-Google-Smtp-Source: ABdhPJy6apyhe+edKkdReCfLpjmsSaJEByOKvNX4OcTyBJs9NNkwICa9PQqmTYuROvPYN8xiDP2RSw== X-Received: by 2002:a1c:4e0e:: with SMTP id g14mr3297089wmh.9.1604995593183; Tue, 10 Nov 2020 00:06:33 -0800 (PST) Received: from [192.169.1.6] (84-198-87-90.access.telenet.be. [84.198.87.90]) by smtp.gmail.com with ESMTPSA id s8sm12096271wrn.33.2020.11.10.00.06.32 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 10 Nov 2020 00:06:32 -0800 (PST) From: Peter Coppens Message-Id: <1C729C96-97BB-46CF-9457-876DCC395923@datylon.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_8DBB0FCF-856B-4105-B532-E2944F25E595" Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.4\)) Subject: Re: Execution plan does not use index Date: Tue, 10 Nov 2020 09:06:31 +0100 In-Reply-To: Cc: Michael Lewis , PostgreSQL General To: Pavel Stehule References: X-Mailer: Apple Mail (2.3608.120.23.2.4) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --Apple-Mail=_8DBB0FCF-856B-4105-B532-E2944F25E595 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Pavel Tx for the tip. But given that if the I64_01 column is removed from the = select list, the index is used I guess the cast is not likely to be the = cause. Like so explain=20 select d.short_id,mv.timestamp --,mv.I64_01=20 from device d, device_configuration dc, measurement_value mv, = pg_timezone_names tzn=20 where mv.device_id=3Dd.short_id and dc.device_id =3D d.id and = dc.latest=3Dtrue and dc.timezone=3Dtzn.name and=20 mv.timestamp > '2020-11-06'::timestamp - tzn.utc_offset and = mv.timestamp < '2020-11-07'::timestamp - tzn.utc_offset =3D=3D> Nested Loop (cost=3D1.13..6217004.08 rows=3D60425437 width=3D12) -> Nested Loop (cost=3D0.56..21334.84 rows=3D2186 width=3D20) Join Filter: (dc.timezone =3D pg_timezone_names.name) -> Nested Loop (cost=3D0.56..7497.34 rows=3D615 width=3D18) -> Index Scan using device_short_id_key on device d = (cost=3D0.28..2423.90 rows=3D683 width=3D20) -> Index Scan using = device_configuration_device_latest_idx on device_configuration dc = (cost=3D0.28..7.42 rows=3D1 width=3D30) Index Cond: ((device_id =3D d.id) AND (latest =3D = true)) Filter: latest -> Function Scan on pg_timezone_names (cost=3D0.00..10.00 = rows=3D1000 width=3D48) -> Index Only Scan using measurement_values_pkey on measurement_value = mv (cost=3D0.57..2399.33 rows=3D43492 width=3D12) Index Cond: ((device_id =3D d.short_id) AND ("timestamp" > = ('2020-11-06 00:00:00'::timestamp without time zone - = pg_timezone_names.utc_offset)) AND ("timestamp" < ('2020-11-07 = 00:00:00'::timestamp without time zone - pg_timezone_names.utc_offset))) Peter > On 10 Nov 2020, at 08:25, Pavel Stehule = wrote: >=20 >=20 >=20 > =C3=BAt 10. 11. 2020 v 8:18 odes=C3=ADlatel Peter Coppens = > napsal: > Michael >=20 > Many thanks for spending your time on this. Your alternative does not = help unfortunately (see execution plan) >=20 > Still a sequential scan on the complete table. I have tried many = alternatives and somehow whenever I add a column that is not in the = index (I64_01) the optimizer decides not to use the index. If I remove = that column, the index is used. I guess it estimates that the extra = indirection from index pages to the row pages is more costly than = scanning the 168M records. Pretty sure it=E2=80=99s not, but I cannot = explain it to the stubborn thing :) >=20 > Btw, thanks for the >=3D tip (I was aware of it) >=20 > Wkr, >=20 > Peter >=20 >=20 > Hash Join (cost=3D683.93..7270857.46 rows=3D458127 width=3D20) > Hash Cond: (mv_inner.device_id =3D d.short_id) > Join Filter: ((mv_inner."timestamp" > ('2020-11-06 = 00:00:00'::timestamp without time zone - pg_timezone_names.utc_offset)) = AND (mv_inner."timestamp" < ('2020-11-07 00:00:00'::timestamp without = time zone - pg_timezone_names.utc_offset))) > -> Seq Scan on measurement_value mv_inner (cost=3D0.00..7166797.33 = rows=3D1287989 width=3D1006) > Filter: (("timestamp" > '2020-11-05 00:00:00'::timestamp = without time zone) AND ("timestamp" < '2020-11-08 00:00:00'::timestamp = without time zone)) >=20 > when you see cast in filter, then you should check type equality in = constraints. With some exception Postgres uses indexes only when = filtered value has same type like column type. >=20 > Maybe there is inconsistency between timestamp (with time zone), and = timestamp without time zone >=20 > Regards >=20 > Pavel >=20 >=20 > -> Hash (cost=3D656.61..656.61 rows=3D2186 width=3D20) > -> Hash Join (cost=3D77.87..656.61 rows=3D2186 width=3D20) > Hash Cond: (dc.timezone =3D pg_timezone_names.name = ) > -> Hash Join (cost=3D55.37..533.83 rows=3D615 = width=3D18) > Hash Cond: (dc.device_id =3D d.id ) > -> Seq Scan on device_configuration dc = (cost=3D0.00..470.01 rows=3D615 width=3D30) > Filter: latest > -> Hash (cost=3D46.83..46.83 rows=3D683 = width=3D20) > -> Seq Scan on device d (cost=3D0.00..46.83 = rows=3D683 width=3D20) > -> Hash (cost=3D10.00..10.00 rows=3D1000 width=3D48) > -> Function Scan on pg_timezone_names = (cost=3D0.00..10.00 rows=3D1000 width=3D48) >=20 >=20 >=20 >> On 10 Nov 2020, at 01:15, Michael Lewis > wrote: >>=20 >> On Mon, Nov 9, 2020 at 1:11 PM Peter Coppens = > wrote: >> Adding the tzn.utc_offset results in the fact that the execution plan = no longer considers to use the index on the measurement_value table. Is = there any way the SQL can be rewritten so that the index is used? Or any = other solution so that the query with the timezone offset returns in a = comparable time? >>=20 >> I am not aware of a best practice to handle this. Your where = condition on mv.timestamp now depends on several joins to do a filtering = that used to be a static range that can be scanned into the index as a = first node in the plan. I have sometimes used a sub-query on a broader = condition that allows the use of the index, and then fully reducing the = set later. Something like this- >>=20 >> select d.short_id,mv.timestamp,mv.I64_01 >> from device d, device_configuration dc, ( >> select mv.* >> from measurement_value AS mv_inner >> where mv.timestamp > '2020-11-06'::timestamp - interval '1 day' and = mv.timestamp < '2020-11-07'::timestamp + interval '1 day' >> offset 0 /* to prevent in-lining the join to the outside set */ >> ) mv, pg_timezone_names tzn >> where mv.device_id=3Dd.short_id and dc.device_id =3D d.id = and dc.latest=3Dtrue and dc.timezone=3Dtzn.name = and >> mv.timestamp > '2020-11-06'::timestamp - tzn.utc_offset and = mv.timestamp < '2020-11-07'::timestamp - tzn.utc_offset >>=20 >> By the way, it seems a little odd to be exclusive on both the begin = and end. I'd usually expect timestamp >=3D start_date and timestamp < = end_date + interval '1 day' to fully capture a 24 hour period. Right = now, you are excluding any data that happens to have a timestamp value = with .000000 seconds (midnight exactly). >=20 --Apple-Mail=_8DBB0FCF-856B-4105-B532-E2944F25E595 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8 Pavel

Tx = for the tip. But given that if the I64_01 column is removed from the = select list, the index is used I guess the cast is not likely to be the = cause.

Like = so

explain 
  select d.short_id,mv.timestamp = --,mv.I64_01 
  from device d, device_configuration dc, = measurement_value mv, pg_timezone_names tzn 
  where = mv.device_id=3Dd.short_id and dc.device_id =3D d.id and dc.latest=3Dtrue and dc.timezone=3Dtzn.name = and 
        mv.timestamp > = '2020-11-06'::timestamp - tzn.utc_offset and mv.timestamp < = '2020-11-07'::timestamp - tzn.utc_offset


=3D=3D>

Nested Loop =  (cost=3D1.13..6217004.08 rows=3D60425437 = width=3D12)
  ->  Nested Loop  (cost=3D0.56..21334.84 = rows=3D2186 width=3D20)
        Join Filter: (dc.timezone =3D = pg_timezone_names.name)
        ->  Nested Loop =  (cost=3D0.56..7497.34 rows=3D615 width=3D18)
      =         ->  Index Scan using = device_short_id_key on device d  (cost=3D0.28..2423.90 rows=3D683 = width=3D20)
              -> =  Index Scan using device_configuration_device_latest_idx on = device_configuration dc  (cost=3D0.28..7.42 rows=3D1 = width=3D30)
                =     Index Cond: ((device_id =3D d.id) AND (latest =3D true))
      =               Filter: = latest
        ->  Function Scan on = pg_timezone_names  (cost=3D0.00..10.00 rows=3D1000 = width=3D48)
  ->  Index Only Scan using = measurement_values_pkey on measurement_value mv =  (cost=3D0.57..2399.33 rows=3D43492 width=3D12)
  =       Index Cond: ((device_id =3D d.short_id) AND = ("timestamp" > ('2020-11-06 00:00:00'::timestamp without time zone - = pg_timezone_names.utc_offset)) AND ("timestamp" < ('2020-11-07 = 00:00:00'::timestamp without time zone - = pg_timezone_names.utc_offset)))


Peter

On 10 Nov 2020, at 08:25, Pavel = Stehule <pavel.stehule@gmail.com> wrote:



=C3=BAt 10. 11. 2020 v 8:18 odes=C3=ADlatel = Peter Coppens <peter.coppens@datylon.com> napsal:
Michael

Many thanks for spending your time on this. Your alternative = does not help unfortunately (see execution plan)

Still a sequential scan on the complete = table. I have tried many alternatives and somehow whenever I add a = column that is not in the index (I64_01) the optimizer decides not to = use the index. If I remove that column, the index is used. I guess it = estimates that the extra indirection from index pages to the row pages = is more costly than scanning the 168M records. Pretty sure it=E2=80=99s = not, but I cannot explain it to the stubborn thing :)

Btw, thanks for the = >=3D tip (I was aware of it)

Wkr,

Peter


Hash Join =  (cost=3D683.93..7270857.46 rows=3D458127 = width=3D20)
  Hash Cond: (mv_inner.device_id =3D = d.short_id)
  Join Filter: ((mv_inner."timestamp" > = ('2020-11-06 00:00:00'::timestamp without time zone - = pg_timezone_names.utc_offset)) AND (mv_inner."timestamp" < = ('2020-11-07 00:00:00'::timestamp without time zone - = pg_timezone_names.utc_offset)))
  -> =  Seq Scan on measurement_value mv_inner =  (cost=3D0.00..7166797.33 rows=3D1287989 = width=3D1006)
      =   Filter: (("timestamp" > '2020-11-05 00:00:00'::timestamp = without time zone) AND ("timestamp" < '2020-11-08 = 00:00:00'::timestamp without time = zone))

when you see cast in filter, then you = should check type equality in constraints. With some exception Postgres = uses indexes only when filtered value has same type like column = type.

Maybe = there is inconsistency between timestamp (with time zone), and timestamp = without time zone

Regards

Pavel


  ->  Hash =  (cost=3D656.61..656.61 rows=3D2186 width=3D20)
  =       ->  Hash Join  (cost=3D77.87..656.61 = rows=3D2186 width=3D20)
          =     Hash Cond: (dc.timezone =3D pg_timezone_names.name)
        =       ->  Hash Join  (cost=3D55.37..533.83 = rows=3D615 width=3D18)
          =           Hash Cond: (dc.device_id =3D d.id)
          =           ->  Seq Scan on = device_configuration dc  (cost=3D0.00..470.01 rows=3D615 = width=3D30)
                =           Filter: latest
  =                   -> =  Hash  (cost=3D46.83..46.83 rows=3D683 = width=3D20)
                =           ->  Seq Scan on device d =  (cost=3D0.00..46.83 rows=3D683 width=3D20)
  =             ->  Hash =  (cost=3D10.00..10.00 rows=3D1000 width=3D48)
  =                   -> =  Function Scan on pg_timezone_names  (cost=3D0.00..10.00 = rows=3D1000 width=3D48)



On 10 Nov 2020, at 01:15, Michael Lewis <mlewis@entrata.com> wrote:

On = Mon, Nov 9, 2020 at 1:11 PM Peter Coppens <peter.coppens@datylon.com> wrote:
Adding the tzn.utc_offset results in the fact that the = execution plan no longer considers to use the index on the = measurement_value table. Is there any way the SQL can be rewritten so = that the index is used? Or any other solution so that the query with the = timezone offset returns in a comparable time?

I am not aware of a best practice to = handle this. Your where condition on mv.timestamp now depends on several = joins to do a filtering that used to be a static range = that can be scanned into the index as a first node in the plan. I = have sometimes used a sub-query on a broader condition that allows the = use of the index, and then fully reducing the set later. Something like = this-

select = d.short_id,mv.timestamp,mv.I64_01
  from device d, = device_configuration dc, (
select mv.*
  from measurement_value AS mv_inner
  = where mv.timestamp > '2020-11-06'::timestamp - interval '1 = day' and mv.timestamp < = '2020-11-07'::timestamp + interval '1 day'
offset 0 /* to prevent in-lining the join to the outside = set */
) mv, pg_timezone_names tzn
  where mv.device_id=3Dd.short_id and dc.device_id = =3D d.id and dc.latest=3Dtrue and dc.timezone=3Dtzn.name and
      =   mv.timestamp > '2020-11-06'::timestamp - = tzn.utc_offset and mv.timestamp < '2020-11-07'::timestamp - = tzn.utc_offset


By the way, it seems a little odd = to be exclusive on both the begin and end. I'd usually expect timestamp >=3D start_date = and timestamp < end_date + interval '1 day' to = fully capture a 24 hour period. Right now, you are excluding any data = that happens to have a timestamp value with .000000 seconds (midnight = exactly).


= --Apple-Mail=_8DBB0FCF-856B-4105-B532-E2944F25E595--