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 1muHzz-0002BE-Er for pgsql-general@arkaria.postgresql.org; Mon, 06 Dec 2021 17:41:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1muHzx-0002Em-J4 for pgsql-general@arkaria.postgresql.org; Mon, 06 Dec 2021 17:41:57 +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 1muHzx-0002Ed-2T for pgsql-general@lists.postgresql.org; Mon, 06 Dec 2021 17:41:57 +0000 Received: from outbound5i.eu.mailhop.org ([35.156.234.212]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1muHzs-0003PZ-RU for pgsql-general@postgresql.org; Mon, 06 Dec 2021 17:41:56 +0000 ARC-Seal: i=1; a=rsa-sha256; t=1638812512; cv=none; d=outbound.mailhop.org; s=arc-outbound20181012; b=MgyCKtY6ag5qm0Szlgg5Y084W7mTC/eFb9s7lnwqUfPtc6SPDEgQYjFEyGaRV2W+ZE8xnyz1kh/QR nAiomsio/9u9TzPeXCZBO+xVU+WGParLTeqEEPeuLwEwknPkPZ/uxvCFmoV9HBcmAwqgQRikGc10Bp GhjCVWfwueu1p5sskWgRYLDUF4+crBwMcTga34XF4l778NXiK1rqBFvoUysLjwQ+JLmDbFcJp2U2BD fWY8Da1TL0LWb5v/hArpIxZfiOOJyADDMrTQsdv04Z2MjJxO3ebzSLaqiqvBQ5qS/jz67W/2ym1AJx 2Lj1HZ7faM/mcCHEczM2JH+aH+L0S3g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=outbound.mailhop.org; s=arc-outbound20181012; h=content-transfer-encoding:mime-version:content-type:references:in-reply-to: date:to:from:subject:message-id:dkim-signature:dkim-signature:dkim-signature: from; bh=WIqpYiOmgmXqCg4gb+d8uiTl7mrcEkb37OHxEJcJAKo=; b=IfGAIfsSfgDpTuqSxJFpwhN0WukrNTHA8GWzteJtbiiVPq6Hp4STPXLHBEGdrGo2T6qdz4mXo14Uz p/yvoIgrSf3d8deWiZRrywvfL5tZvZOBYqrSDZ1mCvreETQYQQS5KXLrNqdiFDNaUD/w14pW8ka+oW 1BoGtZxErtSTtGFhcynmanRF0HO/rSAK3Bx0UYWavq8zOx39LaH94RqhTJ83jnEg8fJDA0cm6WKfkQ A126he19Q2XMIbFJucwPoUVHzL3/wdUr3GJ8sFZyAKE3QTVzaIoc3WwRiZB6/DZFDRMgVF3CM7SpwO zneNLgyCzDGD8Bafr4OkNteegnQzL/Q== ARC-Authentication-Results: i=1; outbound2.eu.mailhop.org; spf=pass smtp.mailfrom=lists.simkin.ca smtp.remote-ip=172.105.109.24; dkim=pass header.d=lists.simkin.ca header.s=athena header.a=rsa-sha256 header.b=Hcpu0fxM; dmarc=pass header.from=lists.simkin.ca; arc=none header.oldest-pass=0; DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lists.simkin.ca; s=duo-1610662143981-d3c4c825; h=content-transfer-encoding:mime-version:content-type:references:in-reply-to: date:to:from:subject:message-id:from; bh=WIqpYiOmgmXqCg4gb+d8uiTl7mrcEkb37OHxEJcJAKo=; b=bwjRbdOL7fVOESpBq0U3y7r4NqtgXjCGqnjRYb1Ofq1nkUhF2PGMWyZWH21IIzFeFn4LlOl+An1H7 Y6b8w4Kvbci+frAz6Ay5aRBiRg9Si5j2aplawzw8k7Jpxw4Slsb704ufx3Jf64eP1KhxdY447o0WKZ Ora5Wl8lbK1LDhe8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outbound.mailhop.org; s=dkim-high; h=content-transfer-encoding:mime-version:content-type:references:in-reply-to: date:to:from:subject:message-id:from; bh=WIqpYiOmgmXqCg4gb+d8uiTl7mrcEkb37OHxEJcJAKo=; b=NvEMHFYQxWsnZyEtePpG9trYst8kxYcb57bQGuJxitUvCeCoI9Gi1Lbkw485+44exwIEzLxuNzZQI GtCrw9OLD0zfjx1ffUEgZjqdK1h/VkAXsjqwUb+qItEzHqrn5VRyaUAdrleQgOQ1eYtiBi0znlKcgQ SdEEcnK4f3/4pO0sRPKjHj/7XDLlfBRUkDT9yJIcTovlMvcFz9iJsvSpvPk25RkBndmwbLNl6IkoWi UK8o8SlKtyRnOb25eF4Xujy0wpi651Rxh/FSVST4GCjfxhLGLLz5zS2jQz+kG/EiRmfSAqUDX/4XLL ugCgQwfiOB+FpEAGkXoepsDMVVBhrCg== X-Originating-IP: 172.105.109.24 X-MHO-RoutePath: c2lta2lu X-MHO-User: ca8db9c5-56bb-11ec-a073-973b52397bcb X-Report-Abuse-To: https://support.duocircle.com/support/solutions/articles/5000540958-duocircle-standard-smtp-abuse-information X-Mail-Handler: DuoCircle Outbound SMTP Received: from athena.simkin.ca (athena.simkin.ca [172.105.109.24]) by outbound2.eu.mailhop.org (Halon) with ESMTPSA id ca8db9c5-56bb-11ec-a073-973b52397bcb; Mon, 06 Dec 2021 17:41:50 +0000 (UTC) Received: from skynet.simkin.ca (node-1w7jr9sr73xr4ze19xwgjcteu.ipv6.telus.net [IPv6:2001:569:bce0:6400:fa75:a4ff:fe2f:92e6]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by athena.simkin.ca (Postfix) with ESMTPSA id A4452C6A13DB for ; Mon, 6 Dec 2021 09:41:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=lists.simkin.ca; s=athena; t=1638812508; bh=TrUUA25gd36OAQ+jR9xx90SoQ5WlvDNJBP3sWroPhsc=; h=Subject:From:To:Date:From:To:Subject:Date:Reply-To; b=Hcpu0fxMtaMa4czzAAvaCd+dEHEp0T2wki2Ivy7xtNPPThsqF5xVn5UfQmo7MuSKU ZGU/8McnO1YgYF9yOeuRLKQ3B/Z16cu5jJB9yJZ2J6abliHAiRpxLZ3abC4aCCltTC MRNZQCIfF5wI2GanNq03w90NIiYX/hVhWJQhxKUVLhhZNLhCyaEf6JgGUuWzPV7at3 s2dOWXiMzsfZgol3O76Hg3m2tvsbu7dnzyYB62NF/cDgW0f2KEUSb8gAJmbhvotpdi Jul0n7VNXEArxG4A+HrEQFnu2ZsGiPUwW/MK07IbfbM2rKo3jfygxYl/d+oHEEFvgA g1x0Vo++FpN1w== Received: from skynet.simkin.ca (skynet.simkin.ca [IPv6:::1]) by skynet.simkin.ca (Postfix) with ESMTP id 62C9F105B1B00 for ; Mon, 6 Dec 2021 09:41:46 -0800 (PST) Message-ID: <024ebf6667151e797210f6f418b60283377e3bb5.camel@lists.simkin.ca> Subject: Re: Query planner issue with preferring primary key over a better index when using ORDER BY and LIMIT From: Alan Hodgson To: Postgres General Date: Mon, 06 Dec 2021 09:41:46 -0800 In-Reply-To: References: Content-Type: text/plain; charset="ISO-8859-15" X-PGP-Key: fp="EF82 6F8D A3BA A096 FFA3 05F2 A7A0 40A5 34E5 AA0B"; id="0x34E5AA0B"; get= User-Agent: Evolution 3.40.4 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, 2021-12-06 at 10:18 -0700, Michael Lewis wrote: > What is your default_statistics_target and how accurate is that > estimate of 5668 rows? What is random_page_cost set to by the way? > > > default_statistics_target = 1000 random_page_cost = 2.0 (it's on AWS on a 9000 iops gp2 volume) Postgresql 13.5 btw. The estimate was reasonably accurate, there were 5069 actual rows matching. > More importantly, what is the better plan that you'd like the planner > to use with your existing indexes? Well, it takes a few ms to grab all 5000 rows by shipment_import_id and then sort/limit them. It takes 30 seconds to do what it is doing instead, and only when the table is mostly cached already, more like 4-5 minutes otherwise. #explain analyze SELECT "shipment_import_records".* FROM shipment_import_records" WHERE shipment_import_records"."shipment_import_id" = 5090609 ORDER BY "shipment_import_records"."id" ASC LIMIT 1; --------------------------------------------------------------------- --------------------------------------------------------------------- ------------------------------------- Limit (cost=0.44..873.08 rows=1 width=243) (actual time=31689.725..31689.726 rows=1 loops=1) -> Index Scan using shipment_import_records_pkey on shipment_import_records (cost=0.44..5122405.71 rows=5870 width=243) (actual time=31689.723..31689.724 rows=1 loops=1) Filter: (shipment_import_id = 5090609) Rows Removed by Filter: 28710802 Planning Time: 0.994 ms Execution Time: 31689.744 ms (6 rows) Just with a kludge to force the better index: # explain analyze SELECT * FROM (SELECT "shipment_import_records".* FROM "shipment_import_records" WHERE "shipment_import_records"."shipment_import_id" = 5090609 OFFSET 0) AS x ORDER BY "id" ASC LIMIT 1; QUERY PLAN --------------------------------------------------------------------- --------------------------------------------------------------------- ------------------ Limit (cost=10655.34..10655.34 rows=1 width=243) (actual time=4.868..4.869 rows=1 loops=1) -> Sort (cost=10655.34..10670.02 rows=5870 width=243) (actual time=4.867..4.868 rows=1 loops=1) Sort Key: shipment_import_records.id Sort Method: top-N heapsort Memory: 27kB -> Index Scan using index_shipment_import_records_on_shipment_import_id on shipment_import_records (cost=0.44..10567.29 rows=5870 width=243) (actual time=0.037..3.560 rows=5069 loops=1) Index Cond: (shipment_import_id = 5090609) Planning Time: 0.135 ms Execution Time: 4.885 ms (8 rows) > > Certainly a composite index would be very helpful here. Using explain > analyze and sharing the output would give more info to go on. > Yeah I am going to just do the composite index for now, but was hoping for a more generic option. Thanks for looking at it.