Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1u1n7A-0089jh-Jw for pgsql-general@arkaria.postgresql.org; Mon, 07 Apr 2025 14:06:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1u1n79-00Gt3N-0x for pgsql-general@arkaria.postgresql.org; Mon, 07 Apr 2025 14:06:15 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1u1n78-00Gt3F-Mj for pgsql-general@lists.postgresql.org; Mon, 07 Apr 2025 14:06:15 +0000 Received: from mail-ej1-x62d.google.com ([2a00:1450:4864:20::62d]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1u1n77-003TwX-0Z for pgsql-general@lists.postgresql.org; Mon, 07 Apr 2025 14:06:14 +0000 Received: by mail-ej1-x62d.google.com with SMTP id a640c23a62f3a-ac2a81e41e3so802547766b.1 for ; Mon, 07 Apr 2025 07:06:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1744034772; x=1744639572; darn=lists.postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=awu2liFAxVejQaJP24R2eADJ3bljRBOxWqkDyCa2N0s=; b=iLtI27rLk2LH1VQ+clzrpy53rDhLhsixhR1i4aO7sVDVpmaY43swM33I/DwR3GlhPQ Lt/uM02fR/u62GICBGmTAkX9QSbjPfaNwUKA3fP7VaNlQQEvnjuKSzVxy74VITGFuweX vqp7On4a90F53hsbnQ5q9t/ti+I8IaXVVIMx2Qk4+Xv0XVVXF3VNRZQS3QAYB9Sl/XRp 5TAwcOlG76QnUY5ACSo+vI7ygM4AieVWuCVQSaODnNdm5ZTcJ8+v/09xf+Hb17563dfn QcRjxCeOQyO7YksFCcZztTCPdeQOYsHZLW9msCRBB0UYxkpHM37+lPpCa9gs8XgQyAW2 vIaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744034772; x=1744639572; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=awu2liFAxVejQaJP24R2eADJ3bljRBOxWqkDyCa2N0s=; b=LHgoGwmxJyM9ouUBltwmS/4WQdqHPIa9rwv5uStr3ST31IpvHzypjOwMZYast9jG9q XkwiEIdS8ahsul4q4jKO2yxVpGr6/5sdODzQb0r15a8d2jZaTGQRVMCP08MM0q7oBCpK bf+xSVnc+I2jD8lwDkyL59DDvj698m0VaOz2wJyggF8NnpUIW6x5ktPGYp7JVbtbNdad pIad3WhtYr3/UqgXXNntmiiHRz3rnR2xVWZnI7NcPckHSBwTOv1bbfmK8WP9Qn8/D4Ov 1hnGegMVfkauk/mco3oBmPeC7WFiownohGngqj1MCNQSketEeoqBNnJIr6FhRnQcgBYg J7Aw== X-Gm-Message-State: AOJu0YyUXm5TV5EMBZXAnrXfdSWFDOGiwbF3P2rFvNGc//d40N/nBqhc zIGMjPpX5VFEVy3rr3j5cNXaUZ1D7wc+s83b/6GgD+cpuAUpS/dNyThoaLufbInFSNi6Qps/EZ/ CT4W8kTWDWjlF4cffYZJIuvOqqxp47Roc X-Gm-Gg: ASbGncu5p9omQXR9yJx1agvtKTCi/D8q06vgtTVnppj15yEaNH2lMKp+II+Jl1zQiSK 6QM1gH4Hs5X/Iy2tvcTnxO31aEBzkSQLX3mGDbFgbph3vogAX7V06JN5LVqXdThG60m9EkJsVmT A9C2Tob6rD3fS18vOfMcOjkjvUhZs= X-Google-Smtp-Source: AGHT+IFPRT67EjG3um2qbEANdiZPCn/KgczsUz6Dw7LQwU8bBQWGirfSVdfT/SPeYYZRmg/pIH2ZqWh6cl1gYQMqhJQ= X-Received: by 2002:a17:906:d54f:b0:ac7:16ee:9112 with SMTP id a640c23a62f3a-ac7e6ea534cmr854742966b.0.1744034771390; Mon, 07 Apr 2025 07:06:11 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Artur Zakirov Date: Mon, 7 Apr 2025 16:05:59 +0200 X-Gm-Features: ATxdqUHNoDL986WmekYLOVaL0gj2jhkfTVtGn-hK3NUdThxcBaHMny3i2d1VD-g Message-ID: Subject: Re: Performance regression when adding LIMIT 1 to a query To: Costa Alexoglou Cc: pgsql-general@lists.postgresql.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, 7 Apr 2025 at 14:45, Costa Alexoglou wrote: > ... > with a plan: > ``` > Gather Merge (cost=3D115584.47..118515.35 rows=3D25120 width=3D824) (act= ual time=3D46.004..74.267 rows=3D29653 loops=3D1) > Workers Planned: 2 > Workers Launched: 2 > -> Sort (cost=3D114584.45..114615.85 rows=3D12560 width=3D824) (actua= l time=3D41.200..47.322 rows=3D9884 loops=3D3) > Sort Key: id > Sort Method: external merge Disk: 16360kB > Worker 0: Sort Method: external merge Disk: 15552kB > Worker 1: Sort Method: external merge Disk: 14536kB > -> Parallel Bitmap Heap Scan on databases_metrics (cost=3D990.7= 7..109175.83 rows=3D12560 width=3D824) (actual time=3D3.326..14.295 rows=3D= 9884 loops=3D3) > Recheck Cond: ((db_instance_id =3D 'c4c97a60-b88e-4cd3-a2f1= -random-uuid'::uuid) AND (created_at >=3D '2023-03-15 10:00:00+00'::timesta= mp with time zone) AND (created_at <=3D '2025-04-03 10:00:00+00'::timestamp= with time zone))"}, > Filter: ((metrics -> 'perf_average_query_runtime'::text) IS= NOT NULL) > Rows Removed by Filter: 68 > Heap Blocks: exact=3D4272 > -> Bitmap Index Scan on idx_databases_metrics_instance_dat= e_custom_created_debugging (cost=3D0.00..983.24 rows=3D30294 width=3D0) (a= ctual time=3D3.786.786 rows=3D29856 loops=3D1)"}, > Index Cond: ((db_instance_id =3D 'c4c97a60-b88e-4cd3-= a2f1-random-uuid'::uuid) AND (created_at >=3D '2023-03-15 10:00:00+00'::tim= estamp with time zone) AND (created_at <=3D '2025-04-03 10:00:00+00'::times= tamp with time zone))"}, > ... > With a plan: > ``` > Limit (cost=3D0.43..229.66 rows=3D1 width=3D824) (actual time=3D7538.004= ..7538.005 rows=3D1 loops=3D1) > -> Index Scan using databases_metrics_pkey on databases_metrics (cost= =3D0.43..6909156.38 rows=3D30142 width=3D824) (actual time=3D7538.002..7538= .003 rows=3D1 loops=3D1) > Filter: ((created_at >=3D '2023-03-15 10:00:00+00'::timestamp wit= h time zone) AND (created_at <=3D '2025-04-03 10:00:00+00'::timestamp with = time zone) A((metrics -> 'perf_average_query_runtime'::text) IS NOT NULL) A= ND (db_instance_id =3D 'c4c97a60-b88e-4cd3-a2f1-random-uuid'::uuid))"}, > Rows Removed by Filter: 10244795 > Planning Time: 0.128 ms > Execution Time: 7538.032 ms > ``` On your second query Postgres uses the index "databases_metrics_pkey". I assume that it is built using the "id" column. It could be very fast with the statement "ORDER BY ... LIMIT", but due to the additional filter Postgres firstly has to remove 10mln rows, which doesn't satisfy the filter, only to reach one single row. On the first query Postgres has to read and sort only 29k rows using the index "idx_databases_metrics_instance_date_custom_created_debugging", which is better suited for the used filter if it includes the columns used in the filter. I'm not sure why Postgres chooses the index "databases_metrics_pkey". Maybe you have outdated statistics. Did you try to run VACUUM ANALYZE on the table? --=20 Kind regards, Artur