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 1qC43e-0007zD-IQ for pgsql-admin@arkaria.postgresql.org; Wed, 21 Jun 2023 20:04:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1qC43d-0003R5-F5 for pgsql-admin@arkaria.postgresql.org; Wed, 21 Jun 2023 20:04:01 +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 1qC43c-0003Qw-Vz for pgsql-admin@lists.postgresql.org; Wed, 21 Jun 2023 20:04:01 +0000 Received: from mail-ej1-x634.google.com ([2a00:1450:4864:20::634]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qC43Z-003smR-SE for pgsql-admin@postgresql.org; Wed, 21 Jun 2023 20:04:00 +0000 Received: by mail-ej1-x634.google.com with SMTP id a640c23a62f3a-9741caaf9d4so765599166b.0 for ; Wed, 21 Jun 2023 13:03:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20221208.gappssmtp.com; s=20221208; t=1687377836; x=1689969836; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:from:to:cc:subject:date :message-id:reply-to; bh=Pzw34yXANToEDzxH9iVrp9H5oywa8hLQWx3ee50wo9w=; b=kcBUhywROceFajEZ9ldSCfj9Vopq/A8ROAhHvAaOu3tRCZHylVj6MiFBgEinfv+bkQ Yetzer75bypLk4GPLlMLqFjG1OgpS+CKxtstxW75Ivc7UIVRpodRHCQrAgAUVM9JPkwx wc9JbbUZD2OYcEraQTslbzUph8+hzp+9WcJTb2+rSDuvsFyy/svvBUYKVbbJRzZ6ppMQ z6mBYRfm8d2bw0L1BHQoeX/X06bg4S0Cx07f7OM/Sl/bXL3SNZv6WJI1uJnFDLUab/BX hH9hZ9bSad7gZyZPx5x8gecBOy2fY0dKZLjuKacCo+VI0LBjccveSWu4j3mmXl8t/BWP Qrnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687377836; x=1689969836; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=Pzw34yXANToEDzxH9iVrp9H5oywa8hLQWx3ee50wo9w=; b=CH036LkAnxop7+qLEzVYCj0KN7azFj2d952xchSIuryD4HeDX2DYORrnGc60EcVOrB +/iycGoCjEPYwAWfxiZT+7wtlagvLkmm5PRxKo2oieU2yRWtOcU5+mCjwUaRBb/rDTZz 4dakf5NIF7L6gcGSA3wOWlQtD24IYdIYHD0fcs5Uh6It4795IwluHWKb6ruKu8k6OB9a yoNThTwxt9edN6EgeZMUIaHRVlzmflhysS3nN6rWaypAJwAiEhbWRxQaIdlBn2ewUoF2 4ZboMGJ5DtXymD86iiEy1GoOa1pXA+PvfHdpx0Fgm+Dq8eox9hBbbIha18IjZHdK29bJ ejZA== X-Gm-Message-State: AC+VfDyLrKbVHfZIbBt7jSHhtl5YKDQmwaMv/7FGkKdrHHnt1eUdS6Y8 xEwm3uAQjZQjQAs3dft7v7oTvg== X-Google-Smtp-Source: ACHHUZ4kAhm/bHjLQ7f94bzAsCiT0oyFZ3/gDrnKDjk8wHGstP9TtpinFy4PsXzJ+uqAjKIixliXPw== X-Received: by 2002:a17:907:96a5:b0:988:76ed:e55e with SMTP id hd37-20020a17090796a500b0098876ede55emr11113349ejc.13.1687377836407; Wed, 21 Jun 2023 13:03:56 -0700 (PDT) Received: from localhost.localdomain (62-46-253-120.adsl.highway.telekom.at. [62.46.253.120]) by smtp.gmail.com with ESMTPSA id m12-20020a1709062acc00b00982b8cd7a44sm3588741eje.139.2023.06.21.13.03.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Jun 2023 13:03:55 -0700 (PDT) Message-ID: <00fec03aaaaa9a18a6d351987f3bb37a6ecce170.camel@cybertec.at> Subject: Re: Poor performance due to parallel seq scan on indexed date field From: Laurenz Albe To: Wells Oliver , pgsql-admin Date: Wed, 21 Jun 2023 22:03:55 +0200 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.46.4 (3.46.4-1.fc37) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, 2023-06-21 at 11:31 -0700, Wells Oliver wrote: > Dead simple date scan across a big-ish table (est.=C2=A023,153,666 rows) >=20 > explain analyze select count(*) from vw_pitches where game_date >=3D '202= 2-06-21' and game_date <=3D '2023-06-21'; >=20 > The view does do some joins but those don't seem to be the issue to me. >=20 > Planner does: >=20 > Finalize Aggregate =C2=A0(cost=3D3596993.88..3596993.89 rows=3D1 width=3D= 8) (actual time=3D69980.491..69982.076 rows=3D1 loops=3D1) > =C2=A0 -> =C2=A0Gather =C2=A0(cost=3D3596993.46..3596993.87 rows=3D4 widt= h=3D8) (actual time=3D69979.137..69982.071 rows=3D5 loops=3D1) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 Workers Planned: 4 > =C2=A0 =C2=A0 =C2=A0 =C2=A0 Workers Launched: 4 > =C2=A0 =C2=A0 =C2=A0 =C2=A0 -> =C2=A0Partial Aggregate =C2=A0(cost=3D3595= 993.46..3595993.47 rows=3D1 width=3D8) (actual time=3D69975.136..69975.137 = rows=3D1 loops=3D5) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 -> =C2=A0Nested Loop =C2= =A0(cost=3D0.44..3591408.37 rows=3D1834034 width=3D0) (actual time=3D0.882.= .69875.934 rows=3D1458419 loops=3D5) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 -> = =C2=A0Parallel Seq Scan on pitches p =C2=A0(cost=3D0.00..3537431.89 rows=3D= 1834217 width=3D12) (actual time=3D0.852..68914.256 rows=3D1458419 loops=3D= 5) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 Filter: ((game_date >=3D '2022-06-21'::date) AND (game_da= te <=3D '2023-06-21'::date)) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 Rows Removed by Filter: 3212310 > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 -> = =C2=A0Memoize =C2=A0(cost=3D0.44..0.47 rows=3D1 width=3D4) (actual time=3D0= .000..0.000 rows=3D1 loops=3D7292095) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 Cache Key: p.pitcher_identity_id > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 Cache Mode: logical > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 Hits: 1438004 =C2=A0Misses: 21042 =C2=A0Evictions: 0 =C2= =A0Overflows: 0 =C2=A0Memory Usage: 2138kB > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 Worker 0: =C2=A0Hits: 1429638 =C2=A0Misses: 21010 =C2=A0E= victions: 0 =C2=A0Overflows: 0 =C2=A0Memory Usage: 2134kB > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 Worker 1: =C2=A0Hits: 1456755 =C2=A0Misses: 21435 =C2=A0E= victions: 0 =C2=A0Overflows: 0 =C2=A0Memory Usage: 2177kB > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 Worker 2: =C2=A0Hits: 1433557 =C2=A0Misses: 21201 =C2=A0E= victions: 0 =C2=A0Overflows: 0 =C2=A0Memory Usage: 2154kB > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 Worker 3: =C2=A0Hits: 1428727 =C2=A0Misses: 20726 =C2=A0E= victions: 0 =C2=A0Overflows: 0 =C2=A0Memory Usage: 2105kB > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 -> =C2=A0Index Only Scan using identity_pkey on identity = idpitcher =C2=A0(cost=3D0.43..0.46 rows=3D1 width=3D4) (actual time=3D0.007= ..0.007 rows=3D1 loops=3D105414) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Index Cond: (identity_id =3D p.pitch= er_identity_id) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Heap Fetches: 83 > Planning Time: 1.407 ms > Execution Time: 69982.927 ms >=20 > Is there something to be done here? Kind of a frequent style of query and= quite slow. First, make sure that PostgreSQL isn't picking the correct plan. Play with "enable_seqscan" and see if the query becomes slower. Then, make sure that "random_page_cost" is set correctly. For SSDs and other storage where random I/O is not more expensive, 1.1 or 1.0 are better values than the default 4. Finally, try disabling or reducing parallel query. PostgreSQL does not opt= imize for low resource usage, but for short execution time. Perhaps running the = qurey with 5 processes actually is faster this way, but it might use way more res= orces than a different execution plan. Yours, Laurenz Albe