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 1kcc3f-0006ea-Ii for pgsql-general@arkaria.postgresql.org; Tue, 10 Nov 2020 22:24:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kcc3d-00051p-KN for pgsql-general@arkaria.postgresql.org; Tue, 10 Nov 2020 22:24:09 +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 1kcc3d-00051i-80 for pgsql-general@lists.postgresql.org; Tue, 10 Nov 2020 22:24:09 +0000 Received: from mail-wr1-x42c.google.com ([2a00:1450:4864:20::42c]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kcc3W-0002DM-TG for pgsql-general@lists.postgresql.org; Tue, 10 Nov 2020 22:24:08 +0000 Received: by mail-wr1-x42c.google.com with SMTP id p8so21899wrx.5 for ; Tue, 10 Nov 2020 14:24:02 -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=VJDSzv7aFVqfNd+ruWqxAifXWgVr+jcUWJBXZ3x+BGg=; b=Ye4l4AI/wlC6V+LRa+U/dPyefWOHGzStcIgVwOh08TaI2MvsYsIwiJiWbYIYZJvLS8 snN93RmHPeSt+JQiRcBHX93PMkLQDbxM/RngNIDeL6311gJOu052FeUAi5obgIdv5AUA yotZ+xg3pwWt7cyCqWRdJcmzdaVOC1lhYvEqbbb5oQ9tbTzuJNecp2GiEo/BUMAP3WrS gqT48bpVJI2UkhJH1sgJdKEIumIpE96HTGN8Wy6ef66znuvMSkUBDtu3vN/IxLGxOySJ YopZH6BlG2M910/w6Ty9aEqbQxtLrbLYtLQZuG1ZHcCL531kk75eipe7nky133bvdUYx edUA== 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=VJDSzv7aFVqfNd+ruWqxAifXWgVr+jcUWJBXZ3x+BGg=; b=ZS0DbCHS2kfsmcAdT2C5Qf925KWjTtEkxaFcGsqZjykPAKdyGpYp3ejLO40X9TLluu n1qGfPBi/LaKH/gphorvZA24wzmD0Q+R6hgcWjhmNw2l47d+vvbjhn9vrIvYBD7zwIMW 0qu4SOCUtgkxe7uZ/BgCJIsu88qO3NN5KfTJjq8PEDiuh4f72mXv5aBHZt733pIBFquE zhb3mxPF1CBPj2ckbUUbng0p/f6vqFhY34gpk4FW4kICjBxkl6IfS16TQ4zH3jOxEI3e 8zne45+Z7JeVEP0HG8Gh/5cr1U+x6kQ/+UWyxDLIjTV8WSpc7YKmqOAkA2lzl92QuluM 6hAQ== X-Gm-Message-State: AOAM5322uJq6UtA9mUjHEiyOno4jiclgWNjcLhyvp6bDIXSvlQ2K2eSy bz+ga73DV18ORM7TFtxQEZWqcw== X-Google-Smtp-Source: ABdhPJxwWXA4Lnh5Dl0IeCq5W84pmGcxRpmKZTC5LyPPRk8SKy/hDBz84AbsBtUWeJxkXMtt8rhGuw== X-Received: by 2002:a5d:54c7:: with SMTP id x7mr26529053wrv.359.1605047041391; Tue, 10 Nov 2020 14:24:01 -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 k22sm208428wmi.34.2020.11.10.14.24.00 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 10 Nov 2020 14:24:00 -0800 (PST) From: Peter Coppens Message-Id: <70D29A79-C968-4310-B4D7-3203A739F261@datylon.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_D0957EF2-3D60-4F80-B69C-02DD05B22D15" 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 23:23:59 +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> 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=_D0957EF2-3D60-4F80-B69C-02DD05B22D15 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 >=20 > Curious, what is seq_page_cost and random_page_cost? show seq_page_cost ->1 show random_page_cost ->4 > Any idea of your cache hits for indexes? No, I am afraid not. It=E2=80=99s been a long time since I went that = deep in the RDBMS behaviour (must have been Oracle 7.something :) ) > If they are very high and/or you have SSD or similar fast storage, = then maybe random_page_cost should be 1.1-2 and not default 4 (assuming = seq_page_cost is still 1). The planner will be more likely to use an = index scan if the expected cost for scanning an index (random) is closer = to a sequential read. Sorry if this explanation is completely = superfluous and you have already configured this. I played around with some of the settings (also with setting = enable_seqscan to false). While some of the plans did no longer use a = full table scan, the performance did not really work out when I tried. = Tbh, I never got too deep in those plans to find out why that would be, = as I am not too keen on changing these settings in the first place. >=20 > It would be interesting to see explain (analyze buffers) output so we = can see the actual counts for these nodes. I'm rather surprised that the = query I provided didn't use the timestamp index unless the inclusion of = 3 days worth of range meant that it was estimated to be too high a = fraction of the table. If you just execute only the subquery, is the = index used by chance? >=20 Index is not used for the subquery explain=20 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 =3D=3D> Seq Scan on measurement_value mv_inner (cost=3D0.00..7175777.00 = rows=3D1219616 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)) > Top posting (reply with all previous copied below) is discouraged on = these lists. I think because it makes the archives rather messy.=20 Will try to remember= --Apple-Mail=_D0957EF2-3D60-4F80-B69C-02DD05B22D15 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8

Curious, what is seq_page_cost and = random_page_cost?

show seq_page_cost
->1

show random_page_cost
->4


Any idea of your cache hits for = indexes?

No, I am afraid not. It=E2=80=99s = been a long time since I went that deep in the RDBMS behaviour (must = have been Oracle 7.something  :) )


If they are very high and/or you have SSD or similar = fast storage, then maybe random_page_cost should be 1.1-2 and not = default 4 (assuming seq_page_cost is still 1). The planner will be more = likely to use an index scan if the expected cost for scanning an index = (random) is closer to a sequential read. Sorry if this explanation is = completely superfluous and you have already configured = this.

I played around with some of the = settings (also with setting enable_seqscan to false). While some of the = plans did no longer use a full table scan, the performance did not = really work out when I tried. Tbh, I never got too deep in those plans = to find out why that would be, as I am not too keen on changing these = settings in the first place.


It would be interesting = to see explain (analyze buffers) output so we can see the actual = counts for these nodes. I'm rather surprised that the query I provided = didn't use the timestamp index unless the inclusion of 3 days worth of = range meant that it was estimated to be too high a fraction of the = table. If you just execute only the subquery, is the index used by = chance?

Index= is not used for the subquery

explain 
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

=3D=3D>

Seq Scan on measurement_value = mv_inner (cost=3D0.00..7175777.00 rows=3D1219616 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))



Top posting = (reply with all previous copied below) is discouraged on these lists. I = think because it makes the archives rather = messy. 

Will try to remember= --Apple-Mail=_D0957EF2-3D60-4F80-B69C-02DD05B22D15--