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 1kcj2n-0006Bf-4T for pgsql-general@arkaria.postgresql.org; Wed, 11 Nov 2020 05:51:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kcj2m-0005FI-39 for pgsql-general@arkaria.postgresql.org; Wed, 11 Nov 2020 05:51:44 +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 1kcj2l-0005FB-J7 for pgsql-general@lists.postgresql.org; Wed, 11 Nov 2020 05:51:43 +0000 Received: from mail-wr1-x42e.google.com ([2a00:1450:4864:20::42e]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kcj2i-0007XB-Rr for pgsql-general@lists.postgresql.org; Wed, 11 Nov 2020 05:51:42 +0000 Received: by mail-wr1-x42e.google.com with SMTP id c17so1305584wrc.11 for ; Tue, 10 Nov 2020 21:51:40 -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=0hqxw/jHvEMXvJJiM/U9G+Z1u02JjwFV0iaaQMrPmTA=; b=MtTksYM8pc2gGMy2RGFuoZuez6Qz+2ctdB40jQi0Y+LlXsPZhp0BQHO+c7u/6bCe7f yVuERqcWcdRafOxAZFlC+GPOEJb0TxGMqaVZTzZvaclR1tb1/nW/mdweZNayIrUwR9DP rlztrly3CjmFUIJMagZRkalmvKbZCqi+n6BovcqHdEAGQd6BDBX1CeA9cOwIAlc9N0ie rSHwUthJz22+lzxIcZWpnMqpIIsroyhROpuq26MredqbA1ptIH2/OkgMP0HqsDghZIZO Ad8CLqMAvYTfhON7l0HadgLfi6Dme3/u1uUuuvx2QjZPVmmX8mnU4zX3kdfitNJ2epFY LpzQ== 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=0hqxw/jHvEMXvJJiM/U9G+Z1u02JjwFV0iaaQMrPmTA=; b=H/BUlKlRjLXUGbMBmdvCQNCdq7Vb4ShHB54qfX2tjyyA9g0ONBFeNLAdPkk1fCQd/Y dzhJtEmmmkdI2f7FJzjOjDdAR7tHkO0akH/TOd4KJbT/pqCvtboDs548wmhGU9j+7LOt 9INgV5R50r6yLjiZHbvBWaeik+N6Xd/WyImSImPB7qF5vvOxvcowuM3rc5e2nhkM0gBC CKwrlwKIOSHjgKHuY76ktzXyflKyQeHFNWwirnVfzMxH+H/AZKh5iZziOw0+KzgNV7co knLg/HbIg+PkccoSMRS4dwc1a7Qpn/VLqhAiijob5FLexDRJrR6RPlXfuUaf5T/K9U/a ZUzg== X-Gm-Message-State: AOAM532vRAvs6HWKfq9HRk5j6wLXeIDRmSrqwmNYt/D0mDjOweZLr3AE RUjQ7cmI7NFdPc0bEMi1alnq3A== X-Google-Smtp-Source: ABdhPJzuXBsomFNe+6n2W0it6Uk73SjMiqvwsaHD9T1UVimbuy9G8KP6aLZpurpp08IvtdJZSy5P4w== X-Received: by 2002:a5d:6550:: with SMTP id z16mr27364783wrv.266.1605073898741; Tue, 10 Nov 2020 21:51:38 -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 y16sm633040wrt.25.2020.11.10.21.51.37 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 10 Nov 2020 21:51:38 -0800 (PST) From: Peter Coppens Message-Id: Content-Type: multipart/alternative; boundary="Apple-Mail=_96100E73-03AC-4C0F-BD42-95BB5B91D0A7" Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.4\)) Subject: Re: Execution plan does not use index Date: Wed, 11 Nov 2020 06:51:36 +0100 In-Reply-To: Cc: Pavel Stehule , PostgreSQL General To: Michael Lewis References: <1C729C96-97BB-46CF-9457-876DCC395923@datylon.com> <64AAA8DD-FD2F-4DFC-BDA4-109C024609B1@datylon.com> <70D29A79-C968-4310-B4D7-3203A739F261@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=_96100E73-03AC-4C0F-BD42-95BB5B91D0A7 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > Curious, how accurate is that row count of 1.2 million records for 3 = days? Not to bad actually select count(mv_inner.*) from measurement_value AS mv_inner where mv_inner.timestamp > '2020-11-06'::timestamp - interval '1 day' = and mv_inner.timestamp < '2020-11-07'::timestamp + interval '1 day' offset 0 =3D=3D> 1128736 > How many total records in the table? =C2=B1168 million > If you disable sequential scan, does it choose the index and what cost = does it show? It chooses the index, but apparently to create some intermediate = structure that then later still needs to be joined on the device_id. = Probably requires scanning all pages of the index, which might explain = why the performance is still not ok set enable_seqscan to false; explain select d.short_id,mv.timestamp,mv.I64_01 from=20 device d , device_configuration dc , ( select mv_inner.* from measurement_value AS mv_inner where mv_inner.timestamp > '2020-11-06'::timestamp - interval '1 = day' and mv_inner.timestamp < '2020-11-07'::timestamp + interval '1 day' offset 0 ) 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> Hash Join (cost=3D6677594.18..9545649.57 rows=3D434126 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))) -> Bitmap Heap Scan on measurement_value mv_inner = (cost=3D6676540.29..9446603.90 rows=3D1220458 width=3D1006) Recheck Cond: (("timestamp" > '2020-11-05 00:00:00'::timestamp = without time zone) AND ("timestamp" < '2020-11-08 00:00:00'::timestamp = without time zone)) -> Bitmap Index Scan on measurement_values_pkey = (cost=3D0.00..6676235.18 rows=3D1220458 width=3D0) Index Cond: (("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=3D1026.55..1026.55 rows=3D2187 width=3D20) -> Hash Join (cost=3D471.95..1026.55 rows=3D2187 width=3D20) Hash Cond: (dc.timezone =3D pg_timezone_names.name) -> Hash Join (cost=3D449.45..903.76 rows=3D615 width=3D18)= Hash Cond: (dc.device_id =3D d.id) -> Bitmap Heap Scan on device_configuration dc = (cost=3D242.72..688.58 rows=3D615 width=3D30) Filter: latest -> Bitmap Index Scan on = device_configuration_device_latest_idx (cost=3D0.00..242.57 rows=3D615 = width=3D0) Index Cond: (latest =3D true) -> Hash (cost=3D198.19..198.19 rows=3D683 = width=3D20) -> Index Scan using device_short_id_key on = device d (cost=3D0.28..198.19 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)= --Apple-Mail=_96100E73-03AC-4C0F-BD42-95BB5B91D0A7 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
Curious, how accurate is that = row count of 1.2 million records for 3 = days?

Not to bad actually

select = count(mv_inner.*)
from measurement_value AS mv_inner
where mv_inner.timestamp > = '2020-11-06'::timestamp - interval '1 day' and mv_inner.timestamp < = '2020-11-07'::timestamp + interval '1 day'
offset 0

=3D=3D> 1128736


How many total records in the = table?

=C2=B1= 168 million

If = you disable sequential scan, does it choose the index and what cost does = it show?

It chooses the index, but = apparently to create some intermediate structure that then later still = needs to be joined on the device_id. Probably requires scanning all = pages of the index, which might explain why the performance is still not = ok

set enable_seqscan to false;
explain
select = d.short_id,mv.timestamp,mv.I64_01
  from 
    device d
    , device_configuration = dc
    = , (
    =   select mv_inner.*
        from measurement_value AS = mv_inner
  =       where mv_inner.timestamp > = '2020-11-06'::timestamp - interval '1 day' and mv_inner.timestamp < = '2020-11-07'::timestamp + interval '1 day'
        offset = 0
    =   ) 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>


Hash Join =  (cost=3D6677594.18..9545649.57 rows=3D434126 = 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)))
  -> =  Bitmap Heap Scan on measurement_value mv_inner =  (cost=3D6676540.29..9446603.90 rows=3D1220458 = width=3D1006)
        Recheck Cond: (("timestamp" > = '2020-11-05 00:00:00'::timestamp without time zone) AND ("timestamp" = < '2020-11-08 00:00:00'::timestamp without time = zone))
        ->  Bitmap Index Scan on = measurement_values_pkey  (cost=3D0.00..6676235.18 rows=3D1220458 = width=3D0)
              Index Cond: = (("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=3D1026.55..1026.55 = rows=3D2187 width=3D20)
        ->  Hash Join =  (cost=3D471.95..1026.55 rows=3D2187 width=3D20)
      =         Hash Cond: (dc.timezone =3D = pg_timezone_names.name)
              -> =  Hash Join  (cost=3D449.45..903.76 rows=3D615 = width=3D18)
                =     Hash Cond: (dc.device_id =3D d.id)
                =     ->  Bitmap Heap Scan on device_configuration dc =  (cost=3D242.72..688.58 rows=3D615 width=3D30)
      =                     = Filter: latest
                =           ->  Bitmap Index Scan on = device_configuration_device_latest_idx  (cost=3D0.00..242.57 = rows=3D615 width=3D0)
                =                 Index Cond: = (latest =3D true)
                =     ->  Hash  (cost=3D198.19..198.19 rows=3D683 = width=3D20)
                =           ->  Index Scan using = device_short_id_key on device d  (cost=3D0.28..198.19 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)
= --Apple-Mail=_96100E73-03AC-4C0F-BD42-95BB5B91D0A7--