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 1nGxNr-00018H-5m for pgsql-hackers@arkaria.postgresql.org; Mon, 07 Feb 2022 06:20:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nGxNp-00083O-Jy for pgsql-hackers@arkaria.postgresql.org; Mon, 07 Feb 2022 06:20:17 +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 1nGxMs-0005Cf-CW for pgsql-hackers@lists.postgresql.org; Mon, 07 Feb 2022 06:19:18 +0000 Received: from mail-pf1-x436.google.com ([2607:f8b0:4864:20::436]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nGxMp-0005VZ-Ts for pgsql-hackers@lists.postgresql.org; Mon, 07 Feb 2022 06:19:17 +0000 Received: by mail-pf1-x436.google.com with SMTP id a8so11267228pfa.6 for ; Sun, 06 Feb 2022 22:19:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=TZz1aV4PF/tWOqdX/txDOKnvdpJ5+uPQuN46u7Hki3k=; b=C9A/7jqSSgjd08lXITxN9fu+afJ86A/BqCdX1pPXmr5HUVHFUMdX8hYJwh9pBNKKib sp50hZb4bh3Ecw+lhij3YUWjsoZHZguUtJDLrRljd0l/gFUdHEoKiBzditU1fxwYKAmx ltdPrfgGEo5Zqm3j+4u7nbKixPMhmfXhuNBQ/MGsmpInZaW82eUhnlUA7G7E+I6JirfR CsLDeRenVyIL0gQvQqZ9LUt167DsZg7cHfwSUF97Yd8x1fzwSwn/ZARJ9MX49KmaKCa2 /B2jA1IHiYWfeHqq2yHSsJa7uHf4GLTMcMY30xFPKcd8+b3d4dOA/XMKH6p8bOvHq/ME a2vg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=TZz1aV4PF/tWOqdX/txDOKnvdpJ5+uPQuN46u7Hki3k=; b=Zq/3iDiZxqaXYz7MMHkDV+zBkU157pwQutfoNemHFWjlmVMPOdTzcAbWckwVo9XPhp Kf0sIdsBHdXguPKGxigK41mdbZt+6Ur7ZXFacXkAkIfd7GlO7aMPYgahE+DgrJN7DN/j 6EHuNeuQIt63/M9sPrmA7FrVk1qKN3RYJBAp2vIPUdwJKsgDqEIDfxDRCHK6fBa/Ib9f OdYO+OdngXhTyouajpNmXs1yYV7pN2zUw0irrzvp+FhpdqgncnMTHXOBNBImXpRjB+2r t0dguY5cfYCVp8YDOQcGFKKFPSf5uupgldIvgexTmhfoG72z03avr7oDuLGGHHII9Vls qIyA== X-Gm-Message-State: AOAM531gqIjIG4VfD6CFOXxfK9/qWUSa6kj03uFPdE18SY766JRNlP2A q6uTrydKq+dsTWyj+jZ0dRQ= X-Google-Smtp-Source: ABdhPJxJSkX9CYBUqzMqtACB1WNO5XLMBpdSxfZxbihi43UufE28U7KSaCgc+dKVwBh2X512nCa1Ig== X-Received: by 2002:a05:6a00:1382:: with SMTP id t2mr14206869pfg.31.1644214754877; Sun, 06 Feb 2022 22:19:14 -0800 (PST) Received: from jrouhaud (1-171-45-184.dynamic-ip.hinet.net. [1.171.45.184]) by smtp.gmail.com with ESMTPSA id bj26sm6975817pgb.81.2022.02.06.22.19.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Feb 2022 22:19:14 -0800 (PST) Date: Mon, 7 Feb 2022 14:18:40 +0800 From: Julien Rouhaud To: Valli Annamalai Cc: pgsql-hackers@lists.postgresql.org Subject: Re: Query choosing Bad Index Path Message-ID: <20220207061840.7emplrlgoeehadn5@jrouhaud> References: <17ec4a8632e.129f0591f14187.1873039138136685603@zohocorp.com> <17ec4f92317.114ea3ebf16204.6527580001465167381@zohocorp.com> <17ed298eb59.b775c0e124435.7693195395182731900@zohocorp.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, On Mon, Feb 07, 2022 at 11:36:17AM +0530, Valli Annamalai wrote: > *Postgres version:* 11.4 > > *Problem:* > Query choosing Bad Index Path. Details are provided below: > > > *Table :* > > > > > > > *Doubt* > 1. Why is this Query choosing *Index Scan Backward using table1_pkey > Index *though it's cost is high. It can rather choose > BITMAP OR > (Index on RECORDID) i.e; table1_idx6 > (Index on RELATEDID) i.e; table1_idx7 > > Below is the selectivity details from *pg_stats* table > - Recordid has 51969 distinct values. And selectivity > (most_common_freqs) for *recordid = 15842006928391817* is 0.00376667 > - Relatedid has 82128 distinct values. And selectivity > (most_common_freqs) for *recordid = 15842006928391817* is 0.0050666 > > Since, selectivity is less, this should logically choose this Index, which > would have improve my query performance here. > I cross-checked the same by removing PrimaryKey to this table and query now > chooses these indexes and response is in 100ms. Please refer the plan below > (after removing primary key): You already sent the same email less than an hour ago on pgsql-performance ([1]), which is the correct mailing list, please don't post on this mailing list also. Note also that sending information as images can be problematic as some people here (including me) won't be able to see them. I tried on a web-based MUA and that's not really better though, as the images are hardly readable and definitely not grep-able. You will likely have more answers sending information in plain text. [1] https://www.postgresql.org/message-id/CADkhgiJ+gT_FDKZWgP8oZsy6iRbYMYkmRjsPhqhcT1A2KBgcHA@mail.gmail.com