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 1kcOxl-0007cR-R7 for pgsql-general@arkaria.postgresql.org; Tue, 10 Nov 2020 08:25:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kcOxk-0000sy-Pi for pgsql-general@arkaria.postgresql.org; Tue, 10 Nov 2020 08:25:12 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kcOxk-0000s2-Bg for pgsql-general@lists.postgresql.org; Tue, 10 Nov 2020 08:25:12 +0000 Received: from mail-wr1-x433.google.com ([2a00:1450:4864:20::433]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kcOxg-0003D1-TL for pgsql-general@lists.postgresql.org; Tue, 10 Nov 2020 08:25:11 +0000 Received: by mail-wr1-x433.google.com with SMTP id o15so3834581wru.6 for ; Tue, 10 Nov 2020 00:25:08 -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=R6pyEobDzl7CUhNK0Qatu+10nQ2l5CWjmEbycb8EQgI=; b=z5KHuMkf+mMzuRSI1CaK2nDoS8eBgoMbJwXieJDj6XvvEd5dS8upK0/Y561yMDucf5 17M1gXqOSgc3gfpP72/RqHZfB/aVroQyIHAu7YQPu8hvNzvznXHpwBwHsUswwqsFjTuU ziBq3gcPTEXO21qRikCDirlPv9FYwLiIzB30nvtqnyIkbBg6G7OAmH1M5CimxxMDIKFt cXCcspAE8aM6ronk00M8iJthohMllERDawrSGw4VZ/y8PKObVd+XFRG4Z0DU3ief7qnb vpqiPdcFMoLXfFzkUk/kA68KQfy3fIw7XnnzP+IsSS9uF+8QrLuN+V6o4eHpjOMQGXZ1 2xyg== 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=R6pyEobDzl7CUhNK0Qatu+10nQ2l5CWjmEbycb8EQgI=; b=EMVL/3RvA85KLc+W9yddKahT590uKMuh0QQMOu/FNlKrutR1mYnwnGzCLNx7+MvMoD JBR/4AXRrphgnv/jlkD+B0saTw8ZhiaHGfOK8Bmm7RAU9p5hxM+kdAdwRIR13xFhNZQ6 J6lGOZ+RHgEYkmdQ8j3hjbzRnQq045HMelTwXNrCcM0F1OqL5ioOM4k0tXuTMkzzxfLr 3R5bIf88P66U5Z5RhhHyTKJrWnYvi1BAuynKBPD4uWVwmuK08A9hpFhzSjPkNLEbxCKw 7bk+vsQPkJY7bIF9YhoAKsMRttJbgXHNg3TXuLhWpTu0r7tUKmlCeuzH88/jWCZLkzkt AymA== X-Gm-Message-State: AOAM530fH96E+N6XTlmwsSu9a9Nmtglmsb7t8qzlVAZWxbeI9xHbkisS T2iN2bUbOhOKuRkV4V3LK2d/nA== X-Google-Smtp-Source: ABdhPJyaTc+F6tx0CreS0k71SAX2TtUK8xo5OEeVMs8Rv+Xrj3Ubxu5t6PmBPyJC7/GoDbBq3sMdYw== X-Received: by 2002:a05:6000:182:: with SMTP id p2mr12164284wrx.116.1604996706993; Tue, 10 Nov 2020 00:25:06 -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 u203sm1186942wme.32.2020.11.10.00.25.05 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 10 Nov 2020 00:25:06 -0800 (PST) From: Peter Coppens Message-Id: <64AAA8DD-FD2F-4DFC-BDA4-109C024609B1@datylon.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_A0972DD4-D5DD-48CC-ABF2-4BFAE56FCEDB" 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:25:04 +0100 In-Reply-To: <1C729C96-97BB-46CF-9457-876DCC395923@datylon.com> Cc: PostgreSQL General To: Pavel Stehule , Michael Lewis References: <1C729C96-97BB-46CF-9457-876DCC395923@datylon.com> 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=_A0972DD4-D5DD-48CC-ABF2-4BFAE56FCEDB Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Triggered by Michael mentioning subqueries I ended up trying explain select d.short_id,mv.timestamp ,mv.I64_01 from device d, device_configuration dc, measurement_value mv where mv.device_id=3Dd.short_id and dc.device_id =3D d.id and = dc.latest=3Dtrue and=20 mv.timestamp > '2020-11-06'::timestamp - (select tzn.utc_offset = from pg_timezone_names tzn where tzn.name=3Ddc.timezone) and = mv.timestamp < '2020-11-07'::timestamp - (select tzn.utc_offset from = pg_timezone_names tzn where tzn.name=3Ddc.timezone) =3D=3D> Nested Loop (cost=3D25.85..84540074.64 rows=3D16996885 width=3D20) -> Seq Scan on device_configuration dc (cost=3D0.00..470.01 rows=3D615= width=3D30) Filter: latest -> Nested Loop (cost=3D25.85..137027.83 rows=3D43494 width=3D36) -> Index Scan using device_pkey on device d (cost=3D0.28..7.23 = rows=3D1 width=3D20) Index Cond: (id =3D dc.device_id) -> Index Scan using measurement_values_pkey on = measurement_value mv (cost=3D25.58..136585.66 rows=3D43494 width=3D20) Index Cond: ((device_id =3D d.short_id) AND ("timestamp" > = ('2020-11-06 00:00:00'::timestamp without time zone - (SubPlan 1))) AND = ("timestamp" < ('2020-11-07 00:00:00'::timestamp without time zone - = (SubPlan 2)))) SubPlan 1 -> Function Scan on pg_timezone_names = (cost=3D0.00..12.50 rows=3D5 width=3D16) Filter: (name =3D dc.timezone) SubPlan 2 -> Function Scan on pg_timezone_names = pg_timezone_names_1 (cost=3D0.00..12.50 rows=3D5 width=3D16) Filter: (name =3D dc.timezone) Now returns the 320K in less than 5sec.=20 I was till now convinced that correlated subqueries or joins are = equivalent. I guess I was wrong :). Wonder how stable this plan will be = though Peter > On 10 Nov 2020, at 09:06, Peter Coppens = wrote: >=20 > Pavel >=20 > 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. >=20 > Like so >=20 > 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 >=20 >=20 > =3D=3D> >=20 > 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))) >=20 >=20 > Peter >=20 >> 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 >=20 --Apple-Mail=_A0972DD4-D5DD-48CC-ABF2-4BFAE56FCEDB Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8 Triggered by Michael mentioning subqueries I ended up = trying

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

=3D=3D>
Nested Loop =  (cost=3D25.85..84540074.64 rows=3D16996885 = width=3D20)
  ->  Seq Scan on device_configuration dc =  (cost=3D0.00..470.01 rows=3D615 width=3D30)
      =   Filter: latest
  ->  Nested Loop =  (cost=3D25.85..137027.83 rows=3D43494 width=3D36)
      =   ->  Index Scan using device_pkey on device d =  (cost=3D0.28..7.23 rows=3D1 width=3D20)
      =         Index Cond: (id =3D = dc.device_id)
        ->  Index = Scan using measurement_values_pkey on measurement_value mv =  (cost=3D25.58..136585.66 rows=3D43494 = width=3D20)
            =   Index Cond: ((device_id =3D d.short_id) AND ("timestamp" > = ('2020-11-06 00:00:00'::timestamp without time zone - (SubPlan 1))) AND = ("timestamp" < ('2020-11-07 00:00:00'::timestamp without time zone - = (SubPlan 2))))
              SubPlan = 1
 =               ->  Function = Scan on pg_timezone_names  (cost=3D0.00..12.50 rows=3D5 = width=3D16)
                =       Filter: (name =3D dc.timezone)
      =         SubPlan 2
          =       ->  Function Scan on pg_timezone_names = pg_timezone_names_1  (cost=3D0.00..12.50 rows=3D5 = width=3D16)
                =       Filter: (name =3D = dc.timezone)


Now returns the 320K in less than = 5sec. 

I was till now convinced = that correlated subqueries or joins are equivalent. I guess I was wrong = :). Wonder how stable this plan will be though

Peter

On 10 Nov 2020, at 09:06, Peter = Coppens <peter.coppens@datylon.com> wrote:

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=_A0972DD4-D5DD-48CC-ABF2-4BFAE56FCEDB--