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 1lt7SC-0006Aw-MG for pgsql-general@arkaria.postgresql.org; Tue, 15 Jun 2021 11:42:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lt7SB-00088J-Fb for pgsql-general@arkaria.postgresql.org; Tue, 15 Jun 2021 11:41:59 +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 1lt7SA-000851-VP for pgsql-general@lists.postgresql.org; Tue, 15 Jun 2021 11:41:59 +0000 Received: from mail1.dalibo.net ([212.83.143.11] helo=mail.dalibo.com) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lt7S8-0002l1-8f for pgsql-general@lists.postgresql.org; Tue, 15 Jun 2021 11:41:57 +0000 Received: from firost (larco.ioguix.net [78.202.0.6]) by mail.dalibo.com (Postfix) with ESMTPSA id 1E71B20033; Tue, 15 Jun 2021 13:41:53 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=dalibo.com; s=a; t=1623757313; bh=RNJjkqCdDTHFBgdfvUZG/4K/8hOM7qomXFBdprrRhcc=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=ONK1boslKj89XtawvUwo/3J3uUg2oxVcWK3xJ7OtTv6BA0ympJQjpytBH3DI9wkZV D4136rCzIcRsoA6uus/NzlrukJl4tZhjLgl93Is/t51EJvcnZctFZnQ3nGr/sEidvp H9Oj7qtFC2MkpA3o1E1SkUB4Xud67QQm6bfgoQTY= Date: Tue, 15 Jun 2021 13:41:51 +0200 From: Jehan-Guillaume de Rorthais To: Atul Kumar Cc: pgsql-general Subject: Re: query issue Message-ID: <20210615134151.0e54b642@firost> In-Reply-To: References: Organization: Dalibo MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, 15 Jun 2021 16:12:11 +0530 Atul Kumar wrote: > Hi, > > I have postgres 10 running on RDS instance. > > I have query below: [...] > > So my doubt is initially when I run this query it takes around 42 > seconds to complete but later after few minutes it completes in 2-3 > seconds. > > I tried to create indexes on table for columns score & "updatedAt" > DESC seperately but found no proper satisfied solution. > > So please help me telling what I am exactly missing here ? The worst part of your plan is the Bitmap Heap Scan, where the plan is actually fetching the rows from the table. The bitmap index scan and sort are fast. There's not much to do about them. This query need to fetch 3882 rows from your table. So either the fetching part of the plan is really, really slow (IO/CPU bound), or the simple filter, on only ~4k, is really slow (CPU bound). You might want to avoid "SELECT *" and only specify the fields you really need. Try first with only "SELECT _id", just to compare. You have an average row size of 1.3k that the executor need to fetch and carry all the way to the result set. This can cost a lot of useless IO and CPU. You might want to tweak random_page_cost/seq_page_cost/effective_cache_size to find out if an index scan would do a better job, but I'm unsure because I lack of informations about your data and system. Regards,