Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bf9WP-00049L-2R for pgsql-bugs@arkaria.postgresql.org; Wed, 31 Aug 2016 17:41:57 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1bf9WO-00050w-Ev for pgsql-bugs@arkaria.postgresql.org; Wed, 31 Aug 2016 17:41:56 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bf9Vk-0004CG-Dq for pgsql-bugs@postgresql.org; Wed, 31 Aug 2016 17:41:16 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by makus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1bf9Vd-0002D0-BH for pgsql-bugs@postgresql.org; Wed, 31 Aug 2016 17:41:15 +0000 Received: from localhost ([127.0.0.1]:53754 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtp (Exim 4.84 (FreeBSD)) (envelope-from ) id 1bf9Va-000ErO-Qt; Wed, 31 Aug 2016 17:41:06 +0000 From: Andrew Gierth To: Kaijiang Chen Cc: Jeff Janes , Pg Bugs Subject: Re: BUG #14302: SQL with LIMIT degrades performance seriously In-Reply-To: (Kaijiang Chen's message of "Wed, 31 Aug 2016 20:42:06 +0800") Message-ID: <87inug4zvx.fsf@news-spur.riddles.org.uk> References: <20160830064858.15676.40929@wrigleys.postgresql.org> <87zint4joz.fsf@news-spur.riddles.org.uk> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (berkeley-unix) Date: Wed, 31 Aug 2016 18:41:06 +0100 MIME-Version: 1.0 Content-Type: text/plain X-Pg-Spam-Score: -3.4 (---) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org >>>>> "Kaijiang" == Kaijiang Chen writes: Kaijiang> It couldn't solve the problem. Kaijiang> I've already created 2 btree indexes, one for parent_id, the Kaijiang> other for user_id. Do you mean to create an multi-column Kaijiang> index on (parent_id, user_id)? Yes. The 2 separate indexes are not sufficient, but you can omit the index on parent_id alone if you create the multi-column index. Kaijiang> still couldn't solve the problem, since we still need index Kaijiang> for user_id (for other sql) and planner will turn to user_id Kaijiang> index. The planner should not do that (if it does, it's a bug). The plan you're looking for is: Limit -> MergeAppend -> Index scan on parent_id_user_id_idx Index Cond: (parent_id = ?) -> Index scan on parent_id_user_id_idx Index Cond: (parent_id = ?) ... Note the use of Index Cond rather than Filter, this is important. -- Andrew (irc:RhodiumToad) -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs