Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g2yhD-0007qY-2a for pgsql-general@arkaria.postgresql.org; Thu, 20 Sep 2018 13:08:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1g2yhB-0003mc-DH for pgsql-general@arkaria.postgresql.org; Thu, 20 Sep 2018 13:08:37 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g2yhB-0003mU-5C for pgsql-general@lists.postgresql.org; Thu, 20 Sep 2018 13:08:37 +0000 Received: from mail.i-mark.de ([188.138.104.222]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g2yh9-0003B5-1t for pgsql-general@lists.postgresql.org; Thu, 20 Sep 2018 13:08:36 +0000 Received: from [192.168.178.20] (unknown [185.38.49.8]) by mail.i-mark.de (Postfix) with ESMTPSA id E44261580707 for ; Thu, 20 Sep 2018 15:08:29 +0200 (CEST) Authentication-Results: loft7616.startdedicated.de; spf=pass (sender IP is 185.38.49.8) smtp.mailfrom=andreas@a-kretschmer.de smtp.helo=[192.168.178.20] Received-SPF: pass (loft7616.startdedicated.de: connection is authenticated) Subject: Re: *Regarding brin_index on required column of the table To: pgsql-general@lists.postgresql.org References: <1962c603-099a-e947-e376-548ae4b68f19@a-kretschmer.de> <410135d5-7791-58ed-2366-a358d7f95d79@a-kretschmer.de> From: Andreas Kretschmer Message-ID: <897f824e-a6d5-9c2d-29b3-84e2ee9e4cbf@a-kretschmer.de> Date: Thu, 20 Sep 2018 15:08:32 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Am 20.09.2018 um 13:11 schrieb Durgamahesh Manne: > Query was executed at less time without distinct > > As well as query was taking around 7 minutes to complete execution > with distinct > >  select   distinct  sub_head."vchSubmittersCode" ,rec."vchFileName" , > rec."vchCusipFundIDSubFundID" , rec."vchFundUnitPrice"  ,  > sub_head."vchValuationDate" ,rec."vchAdvisorLabel" from  table1 rec > join table2 sub_head on > rec."vchSubmittersCode"=sub_head."vchSubmittersCode" where > rec."bFetch"=false and sub_head."bFetch"=false ; > > I need to execute above distinct query at less time as distinct query > was taking more time to execute  even i have created indexes on > required columns of the tables > >  Unique  (cost=5871873.64..6580630.73 rows=7400074 width=89) (actual time=326397.551..389515.863 rows=3700000 loops=1)                                                 | > |   ->  Sort  (cost=5871873.64..5973124.65 rows=40500405 width=89) (actual time=326397.550..372470.846 rows=40500000 loops=1)                                                  | > |         Sort Key: sub_head."vchSubmittersCode", rec."vchFileName", rec."vchCusipFundIDSubFundID", rec."vchFundUnitPrice", sub_head."vchValuationDate", rec."vchAdvisorLabel" | > |         Sort Method: external merge  Disk: 3923224kB                                                  | as you can see: there are 40.500.000 rows to sort to filter out duplicate rows, the result contains 'only' 3.700.000 rows. But for this step the database needs nearly 4TB on-disk. This will, of course, need some time. If you have enough ram you can try to set work_mem to 5 or 6 GB to change the plan to a in-memory - sort. But keep in mind, this is dangerous! If the machine don't have enough free ram the kernal can decide to Out-Of-Memory - killing processes. What kind of disks do you have? Maybe you can use a separate fast SSD as temp_tablespaces? Regards, Andreas -- 2ndQuadrant - The PostgreSQL Support Company. www.2ndQuadrant.com