Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bwoYF-0002zO-KG for pgsql-performance@arkaria.postgresql.org; Wed, 19 Oct 2016 10:56:51 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1bwoYF-0004C4-6z for pgsql-performance@arkaria.postgresql.org; Wed, 19 Oct 2016 10:56:51 +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.84_2) (envelope-from ) id 1bwoWY-0001PY-Vo for pgsql-performance@postgresql.org; Wed, 19 Oct 2016 10:55:07 +0000 Received: from smtp-relay-00-1.dondominio.net ([31.214.176.14]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bwoWR-0000OL-QH for pgsql-performance@postgresql.org; Wed, 19 Oct 2016 10:55:06 +0000 Received: from mail-node.dondominio.com by smtp-relay.dondominio.com (Postfix) with ESMTP id 3313442D94 for ; Wed, 19 Oct 2016 12:54:57 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=negora.com; s=dddk; t=1476874497; bh=s3KxACroXNWr4yVYgNVpoRyR4zvXRwkw+MOlOiiGFiY=; h=From:Subject:To:Message-ID:Date:Content-Type; b=BtrbQnt2CfytbejB2qAtmjGGJvgqbTpaEwJh1t4Tcv5lguD4zkyc2uK7ruG+08Q1R WyiQwMGSINLXgLYsaSFZbpvx4L04nGPDHnIqBEybd3LDubymdYPJQR2fnnzDbGShza 9H6aK+p4fodnLX2mvATDkbNafzYnUpduGDBw+O1Q= Received: by mail-node.dondominio.com (Postfix, from userid 4000) id 2BE9D417FD; Wed, 19 Oct 2016 12:54:57 +0200 (CEST) Received: from [192.168.1.2] (177.red-88-14-80.dynamicip.rima-tde.net [88.14.80.177]) (Authenticated sender: public@negora.com) by mail-node.dondominio.com (Postfix) with ESMTPA id 055B44155D for ; Wed, 19 Oct 2016 12:54:56 +0200 (CEST) From: negora X-Enigmail-Draft-Status: N1110 Subject: Performance of a nested loop, whose inner loop uses an index scan. To: pgsql-performance@postgresql.org Message-ID: <5cb92dc3-af70-3cb1-5b2b-87ccc31f62cb@negora.com> Date: Wed, 19 Oct 2016 12:54:46 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.3.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.2.6 (mail-node.dondominio.com [0.0.0.0]); Wed, 19 Oct 2016 12:54:57 +0200 (CEST) X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org Hello: I've a question about the performance of a query plan that uses a nested loop, and whose inner loop uses an index scan. Would you be so kind to help me, please? I'm using PostgreSQL 9.5.4 on Ubuntu 14.04 64-bit (kernel 4.8.2). I've 3 tables, which are "answers", "test_completions" and "courses". The first one contains around ~30 million rows, whereas the others only have a few thousands each one. The query that I'm performing is very simple, although retrieves lots of rows: --------------------- SELECT answers.* FROM answers JOIN test_completions ON test_completions.test_completion_id = answers.test_completion_id JOIN courses ON courses.course_id = test_completions.course_id WHERE courses.group_id = 2; --------------------- This yields the following plan: --------------------- Nested Loop (cost=245.92..383723.28 rows=7109606 width=38) (actual time=1.091..2616.553 rows=8906075 loops=1) -> Hash Join (cost=245.36..539.81 rows=3081 width=8) (actual time=1.077..6.087 rows=3123 loops=1) Hash Cond: (test_completions.course_id = courses.course_id) -> Seq Scan on test_completions (cost=0.00..214.65 rows=13065 width=16) (actual time=0.005..1.051 rows=13065 loops=1) -> Hash (cost=204.11..204.11 rows=3300 width=8) (actual time=1.063..1.063 rows=3300 loops=1) Buckets: 4096 Batches: 1 Memory Usage: 161kB -> Bitmap Heap Scan on courses (cost=45.86..204.11 rows=3300 width=8) (actual time=0.186..0.777 rows=3300 loops=1) Recheck Cond: (group_id = 2) Heap Blocks: exact=117 -> Bitmap Index Scan on fki_courses_group_id_fkey (cost=0.00..45.03 rows=3300 width=0) (actual time=0.172..0.172 rows=3300 loops=1) Index Cond: (group_id = 2) ### HERE ### -> Index Scan using fki_answers_test_completion_id_fkey on answers (cost=0.56..96.90 rows=2747 width=38) (actual time=0.007..0.558 rows=2852 loops=3123) ### HERE ### Index Cond: (test_completion_id = test_completions.test_completion_id) Planning time: 0.523 ms Execution time: 2805.530 ms --------------------- My doubt is about the inner loop of the nested loop, the one that I've delimited with ### HERE ### . This loop is the part that, obviously, more time consumes. Because its run 3,123 times and requires lots of accesses to multiple database pages. But, Is there anything that I can do to reduce even more the time spent in this part? Apart of: * Clustering the "answers" table. * Upgrading PostgreSQL to version 9.6, to take advantage of the index scans in parallel. * Upgrading the hardware. Thank you! -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance