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 1kcNvH-0005Bu-5G for pgsql-general@arkaria.postgresql.org; Tue, 10 Nov 2020 07:18:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kcNvG-0002Z6-43 for pgsql-general@arkaria.postgresql.org; Tue, 10 Nov 2020 07:18:34 +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 1kcNvF-0002Yz-Mu for pgsql-general@lists.postgresql.org; Tue, 10 Nov 2020 07:18:33 +0000 Received: from mail-wr1-x42f.google.com ([2a00:1450:4864:20::42f]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kcNvC-0002fg-UT for pgsql-general@lists.postgresql.org; Tue, 10 Nov 2020 07:18:33 +0000 Received: by mail-wr1-x42f.google.com with SMTP id b6so129979wrt.4 for ; Mon, 09 Nov 2020 23:18:30 -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=qzXJhO8wDNo/8i+8lAEwh/GdspiELbHuVfOE8gg2rE4=; b=r9W1owqvP6ltUmT2TO4pQ3VDYeNBQxR2sZVt3/jIwGfEfYKbWa2ERy+b5v9WmOj6Wt RM7VhCEh+9qehQqk1LqNlokubAQlKikiQ5UDXdwLtuBAonOVNHcLhATJEe7s7b5ocRWR vzBd8zF2y/J9bqQ4LCSM22FhIhJddodgh9UBtvYlylckDeUZCqcv42q3pH8l1TerSHUN 0Cc5PxmZObz2nl2aCa9kHVQsBFGF7ke0l0ZutyoJt04UWRcz+c5zQVCTlzZrPU2mZ3cn MFWXkgMnegwjlKN0mscZSKkLU12WB3IMjRLhzXs3PvYs+wSXlrLKWI7XAHgQ2WXOJxGo WZaw== 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=qzXJhO8wDNo/8i+8lAEwh/GdspiELbHuVfOE8gg2rE4=; b=NPXlff4uwyqPHlinXf3+tQZml8WSLdwjvEs/APW9ewdxu0ho4A5z1ERV6IN+cp597W blXJhHIBAu+6ooKU5I8OTHw8jutic2VMOIe4ZuqFySkixWFnUDLXssN9eiQCRfjdR51a IXaHTiyfrHmubTKgQfwbXZFawlETPMbpP1W7D0c0OgJDGyfDU46W/bX6mBha+GicUUVt xLyCVec4vzieAFazqtZejXXJyB5pz/5k/odGMfbRgoSfeJNQ2WtLW306wueknq9GEhO2 CBc2tIwE/BK57i0ASDpYqZy2ay10ILjPv4+NlWkS41kW4djWyk5HmCyxyE7BzlOUeVk2 GSoA== X-Gm-Message-State: AOAM531XuQ0MMz2ufLokaT9b1zfYo4rCEUZvtAG54NsZ9HNBSM6bLsd7 ppnSoNBKcKcX9v9ol6yOU+cZIw== X-Google-Smtp-Source: ABdhPJytAaHyG74wN75m3BZ+WAwL57tbyaxrlER26Y1VmgVIAlHlNnf/U9qCDH8VuCvucwy9cFLyAw== X-Received: by 2002:a5d:640d:: with SMTP id z13mr21334999wru.28.1604992709215; Mon, 09 Nov 2020 23:18:29 -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 t15sm1939308wmn.19.2020.11.09.23.18.28 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 09 Nov 2020 23:18:28 -0800 (PST) From: Peter Coppens Message-Id: Content-Type: multipart/alternative; boundary="Apple-Mail=_955DE704-6286-4E23-824E-AA7BCE7B54AE" 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 08:18:27 +0100 In-Reply-To: Cc: PostgreSQL General To: Michael Lewis 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=_955DE704-6286-4E23-824E-AA7BCE7B54AE Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 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)) -> 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 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). --Apple-Mail=_955DE704-6286-4E23-824E-AA7BCE7B54AE Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8 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))
  ->  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=_955DE704-6286-4E23-824E-AA7BCE7B54AE--