Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rxPVd-008LZd-H1 for pgsql-general@arkaria.postgresql.org; Thu, 18 Apr 2024 11:00:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rxPVc-003WFj-4r for pgsql-general@arkaria.postgresql.org; Thu, 18 Apr 2024 11:00:52 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rxPVb-003WDr-P5 for pgsql-general@lists.postgresql.org; Thu, 18 Apr 2024 11:00:51 +0000 Received: from smtp-relay-02-7.dondominio.net ([31.214.176.40]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rxPVV-001bzQ-2j for pgsql-general@lists.postgresql.org; Thu, 18 Apr 2024 11:00:51 +0000 Received: from localhost (smtp-relay-local.scip.local [127.0.0.1]) by smtp-relay.dondominio.com (Postfix) with SMTP id 40F0E409EE for ; Thu, 18 Apr 2024 13:00:40 +0200 (CEST) Received: from mail-node.dondominio.com by smtp-relay.dondominio.com (Postfix) with ESMTP id 23988408D7 for ; Thu, 18 Apr 2024 13:00:39 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=negora.com; s=dddk; t=1713438039; bh=dSR8OLcXkTWe+jkrTJj3lzuHz/rGSkjINMMUt/4n/Cc=; h=Content-Type:Message-ID:Date:From:To:Subject; b=ZOV2dhGoCtLIJWunCJoBDzsfpUlAo/YDSS+5Auhrla2ONVirwfPUhzMKcWVlO27Sy CZ6aLaz7NU8JzJ0EWk/yl3gQYDJXCUlKj7GK4OvIiq3Azign1hUvGVRDOcSRU+rjRY jtWhEuk9TwDDo3VCFCP+aZEufLzB0RD4Oe28nWUU= Received: from [10.8.0.6] (201.red-83-58-241.dynamicip.rima-tde.net [83.58.241.201]) (Authenticated sender: public@negora.com) by mail-node.dondominio.com (Postfix) with ESMTPA id 01940401DF for ; Thu, 18 Apr 2024 13:00:38 +0200 (CEST) Content-Type: multipart/alternative; boundary="------------juLiX0FD7C660vGZuKWvaeEp" Message-ID: <07b89cad-b2a1-48c2-8a14-67d1b93098c9@negora.com> Date: Thu, 18 Apr 2024 13:00:38 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird From: negora Content-Language: en-US, es-ES, en-GB To: PostgreSQL - General Subject: Why does it sort rows after a nested loop that uses already-sorted indexes? X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.6.2 (mail-node.dondominio.com [0.0.0.0]); Thu, 18 Apr 2024 13:00:39 +0200 (CEST) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------juLiX0FD7C660vGZuKWvaeEp Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Hi: I've a question regarding nested loops and the order in which they return rows. Can you help me, please? Suppose that I've two tables:     - Table [sales_order]         * Columns [id]         * Index [sales_order_pkey] on [id]     - Table [order_line]         * Columns [id], [sales_order_id]         * Index [order_line_ukey] on [sales_order_id], [id] Then, I run the following query: ----------------------------------------------------------- SELECT sales_order.id, order_line.id FROM main.sales_order JOIN main.order_line ON order_line.sales_order_id = sales_order.id WHERE sales_order.customer_id = 2 ORDER BY sales_order.id, order_line.id; ----------------------------------------------------------- The query planner decides to use the following nested loop: ----------------------------------------------------------- Incremental Sort  (cost=26.90..16020.06 rows=144955 width=8)   Sort Key: sales_order.id, order_line.id   Presorted Key: sales_order.id   ->  Nested Loop  (cost=0.70..4593.99 rows=144955 width=8)         ->  Index Scan using sales_order_pkey on sales_order  (cost=0.28..19.31 rows=79 width=4)               Filter: (customer_id = 2)         ->  Index Only Scan using order_line_ukey on order_line  (cost=0.42..39.22 rows=1869 width=8)               Index Cond: (sales_order_id = sales_order.id) ----------------------------------------------------------- As you can see, the planner does detect that the outer loop returns the rows presorted by [sales_order.id]. However, it's unable to detect that the rows returned by the inner loop are also sorted by [sales_order.id] first, and then by [order_line.id]. Why is it? Is it because the planner is designed to always ignore the order of the inner loop, even although it could take advantage of it (for example, because the analysis time rarely is worth it)? Or is there something that I'm missing? If I'm not mistaken, in this case both index scans seem to be done serially, in an N x M style, so I think the row order would be preserved, Right? Thank you! negora --------------juLiX0FD7C660vGZuKWvaeEp Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit

Hi:

I've a question regarding nested loops and the order in which they return rows. Can you help me, please?

Suppose that I've two tables:

    - Table [sales_order]

        * Columns [id]
        * Index [sales_order_pkey] on [id]

    - Table [order_line]

        * Columns [id], [sales_order_id]
        * Index [order_line_ukey] on [sales_order_id], [id]

Then, I run the following query:

-----------------------------------------------------------

SELECT sales_order.id, order_line.id
FROM main.sales_order
JOIN main.order_line ON order_line.sales_order_id = sales_order.id
WHERE sales_order.customer_id = 2
ORDER BY sales_order.id, order_line.id;

-----------------------------------------------------------

The query planner decides to use the following nested loop:

-----------------------------------------------------------

Incremental Sort  (cost=26.90..16020.06 rows=144955 width=8)
  Sort Key: sales_order.id, order_line.id
  Presorted Key: sales_order.id
  ->  Nested Loop  (cost=0.70..4593.99 rows=144955 width=8)
        ->  Index Scan using sales_order_pkey on sales_order  (cost=0.28..19.31 rows=79 width=4)
              Filter: (customer_id = 2)
        ->  Index Only Scan using order_line_ukey on order_line  (cost=0.42..39.22 rows=1869 width=8)
              Index Cond: (sales_order_id = sales_order.id)

-----------------------------------------------------------

As you can see, the planner does detect that the outer loop returns the rows presorted by [sales_order.id]. However, it's unable to detect that the rows returned by the inner loop are also sorted by [sales_order.id] first, and then by [order_line.id].

Why is it? Is it because the planner is designed to always ignore the order of the inner loop, even although it could take advantage of it (for example, because the analysis time rarely is worth it)? Or is there something that I'm missing?

If I'm not mistaken, in this case both index scans seem to be done serially, in an N x M style, so I think the row order would be preserved, Right?

Thank you!

negora


--------------juLiX0FD7C660vGZuKWvaeEp--