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 1uCywl-004syP-Hq for pgsql-general@arkaria.postgresql.org; Thu, 08 May 2025 10:57:48 +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 1uCywk-002Yg2-0d for pgsql-general@arkaria.postgresql.org; Thu, 08 May 2025 10:57:46 +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 1uCywj-002Yfu-Hy for pgsql-general@lists.postgresql.org; Thu, 08 May 2025 10:57:45 +0000 Received: from mail-db8eur05olkn20800.outbound.protection.outlook.com ([2a01:111:f403:2e14::800] helo=EUR05-DB8-obe.outbound.protection.outlook.com) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1uCywg-000nyR-2o for pgsql-general@lists.postgresql.org; Thu, 08 May 2025 10:57:45 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=P/WsiyAEL5pj2EfXb4QC1tE0dB3zCjEyfjLk3TfFBa6+OtuosFIHCI0aZarVYd4rpRixdxJgYJ9xIjGPlJpzSTby1+YRSK0mfhEzkgbXZJ3RIreeCvwPDrZW6k8PY6rDub2+Yi2XafwJSSmzbB7nDLa+w+8f8OCpgYgNdtx5HXWwwt0AyLAKiDMpDEow/S/dpNKYrZE+QtuAR2x1mHLAndvcpU381Fa9jF1wVxYHGfYRoyHTIoJI+FCqfbHTGNSRJbXfS9w0dmsk5Bs4l+NcVw+LsT+UMjVixRVjK3u7fB2QpysdYkiQjdx8RoHLuXck/rIiTaAzDfx8HE4m6vYImg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=hzS5UIUtlnSK10annZUaNfXMGiO4TdtwrRmp5GPF45U=; b=YdHP6ElDidUF3h3uV1hznDI+l0tjx1JysUCbq5diKfXbwMRcoZGIIjfmsoYLmxoa0I/XNH+VDDJkfKDdAzH915xuP6vWn76+jI85VTJBg4IauWq47OtfI/b5kMsl3XcIZR/EgRKckP6G/9IT2NmCteSOhL2Kec6ieMiyJXmlpe9pWlc2ZrVngHS1txaw1HgZgEvmye1iAORp1JtMuGDYuVEj/+GgpyCROk6mkvFm6Rhtg5SBxKEU+hR1CaNezfvzBKlF4oZeBHikctBcSkOpTejcl2vcu1e9XD0s8xZqZSdegwXwSxMKjzKWoKr0CvvN6nX76l6oPiDXasU7ewS3cw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hotmail.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=hzS5UIUtlnSK10annZUaNfXMGiO4TdtwrRmp5GPF45U=; b=nvYzs4iTIPhW2eU5QfBLJftFXkqpb2FhSpXVHHU4Zcq+mLmp390L0X7fsjyPNF2JblqwaTyADzSZPmOM2S/cw4Gu2Aa0IMKyL2z5SuEzziMSahvQtbTN9uGX0+ZPJybNIrawwPIIbp/njIKqe0XCw10HtHEn8rWsdwY+Vl6QIr4SnZGDten4IMifoHDvJc4r6rhkulHKkQCxRRPjwxmDRaGEkA+5Vco/rtASZRrz9O2G/77gek/2c654uq3C9wfmWTuvDgAEdB+iOVxmFaSrglnXKOHOgwcA6ARgdEi7E/GhSxoDTQlaJ9fZWz3Ivw8Be0aIc/vRhExoeoREGuGzVA== Received: from DB7PR08MB3081.eurprd08.prod.outlook.com (2603:10a6:5:25::13) by PAXPR08MB7526.eurprd08.prod.outlook.com (2603:10a6:102:24c::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8699.23; Thu, 8 May 2025 10:57:41 +0000 Received: from DB7PR08MB3081.eurprd08.prod.outlook.com ([fe80::4573:810c:281d:4094]) by DB7PR08MB3081.eurprd08.prod.outlook.com ([fe80::4573:810c:281d:4094%6]) with mapi id 15.20.8722.020; Thu, 8 May 2025 10:57:41 +0000 Message-ID: Date: Thu, 8 May 2025 12:57:40 +0200 User-Agent: Mozilla Thunderbird Subject: Re: Feature-Request: Performance-Optimization when using limit in combination with order by on querys using union From: Karsten P To: pgsql-general@lists.postgresql.org References: Content-Language: de-DE In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-ClientProxiedBy: FR4P281CA0270.DEUP281.PROD.OUTLOOK.COM (2603:10a6:d10:e8::20) To DB7PR08MB3081.eurprd08.prod.outlook.com (2603:10a6:5:25::13) X-Microsoft-Original-Message-ID: <6e60d6ce-8e65-4fb1-a29e-dcfa2f2ab893@hotmail.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DB7PR08MB3081:EE_|PAXPR08MB7526:EE_ X-MS-Office365-Filtering-Correlation-Id: 4190c998-a8fc-4ed7-153b-08dd8e1f2747 X-MS-Exchange-SLBlob-MailProps: HeyTOiyEx6YqUhv2IENF9WKNLXnn5i2fIYj8vz5F5ZJsmuGYqM3PHlnCE6SOSkE1eYnkdBHNG/dpGm5yWZtszZnNTNcbctfSD+v7zLF00mqaiYGTU2uXOPprX/RZJulvI3Px2+/KMqoqKyqXYwG4yElkMBlmz5iKXh+hfNsnKggxzzrFCj5q9/C8qVgDUeCfxD1b6AojjKqjgTPAQAmCAgPlma2Cle6tcjSRxJg4JG0K0T3ZNBfA4PMZPJTdk8CvppPg7HVchlqsLiyE3NjYgAhYnY225ltsNARYW750ZBNoN5Y3A3k9Cs2NBLx1a4LrwRt2r2da+4tvZuY7+/SnSYfyifyZwObjjg4VU1FZ/IZLZH95bPfcp4yqCr+HNKWyLlfxoBrFhKuIKFq2qH/qCom/lZdM9nPsimdmFRNC4ZzQfp+UKuPHkAbcCUxLTGpjcX9a4MPSBYQ9sdVnqqoxvn7O6pwe79d/LHOH78IKFZippLgeAMubhRejh+6kXfSpAAW4RH9AdbjIkJp6AZlIjIjSvFW5yeKpoZBu7WPGq2SzLtvQtJea1olzgjJER0EK4NxlKoZebboyJVUUlRKrHK1WPwdzFjBPGfcroIIN4rdWpi5BYNUkd9E2l5W2kUZ3xIZ9U33UVqa6hWKiBUp8fxmBlUj+75iOyKUIDNPanl5FeIGHPpEvOz3JpZGl3wDQOD+Kl4tu37kvvl5xVz2H/Ys24JpIiZRZOAKDxpN0fUGtHZeLUAiBR5c3gn/+NXTiZjJ1/kMBJeIwl344NOohRA== X-Microsoft-Antispam: BCL:0;ARA:14566002|5072599009|6090799003|461199028|41001999006|7092599006|19110799006|8060799009|15080799009|3412199025|440099028|3430499032; X-Microsoft-Antispam-Message-Info: =?utf-8?B?dHZld2l2UDdMbXB1TVJUb3Z6MFFINjZSOFUrZlNuMWRVK0NvN1lqZUdCcU83?= =?utf-8?B?WkdSNUpzYkgxQnJxeEpwZUN2R0tqeVhXTkZaQWZNNGF6QXh1cElWaExBL25i?= =?utf-8?B?aDB6Rkt3NWN0bFZEaEswRVFhL3lRdHhaZE5oWU0zOTBkWWZSdWxUUk1KcEhm?= =?utf-8?B?ckhXVCszRjVoenRkbVBqZ2hoK0JmT1pCWWJteXNFQ0JNTDFNbzhTWkI3N3R6?= =?utf-8?B?N01BMTBGWk5HemUwazRUbFlvY0VXT3NtS1RqbVczQ3ZmS0QwTFl2L3RpYmZO?=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wNUlmYjBHYjkyRGVDMTFFTEFW?= =?utf-8?B?ZUVvZ0UzaEZZTkV6NDlGeUNKN3BsRThEdEw3TG11eGVyaDBBY3ZudXVBZG1D?= =?utf-8?B?ZlNNcDhzTlZnaThaM24yRUNUcWZKM1dwNktUQ3VlRzAyTjNPeXh1QjcvTThS?= =?utf-8?B?MEFSUG0wZHpZbWd3TXRkNStjVkZuckpSY0tNZVFWbFJXK0xEZUdPR0dGOW1Y?= =?utf-8?B?elN4RGlNZkxIbUI1R3dnaDdFNHdGTmNCZXRmQnBqc0NlbCsrYWo0V2F3dUwy?= =?utf-8?B?VmdLRjRKY29nWGJkRVNhdzZaQmJQb0lSdUIzNUl6cVZLY3ZCNlBWckRzRlMz?= =?utf-8?B?SUZZRmJtcE9GTForNVNyWkQyVElHRHZ4R3BGNG9ZOVIyeEp3OHQvSGI5TGtu?= =?utf-8?Q?vIgvjsxz9orc22PkldnA?= X-OriginatorOrg: sct-15-20-7719-20-msonline-outlook-de33f.templateTenant X-MS-Exchange-CrossTenant-Network-Message-Id: 4190c998-a8fc-4ed7-153b-08dd8e1f2747 X-MS-Exchange-CrossTenant-AuthSource: DB7PR08MB3081.eurprd08.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 May 2025 10:57:41.5714 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: PAXPR08MB7526 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Okay, forget what i've just written, sorry for that. I've digged deeper and checked my generic example - it works perfectly using a merge-append in combination with limit. i'm sorry i didn't check that first. it just won't work in my real-life example. though each part of the query is using an index-scan it is than using a 'normal' append instead of a merge-append, but i don't know why. here is the expected query plan as used with my generic example: Limit  (cost=0.86..0.90 rows=1 width=12)   ->  Merge Append  (cost=0.86..104314.37 rows=3435900 width=12)         Sort Key: kpkp_orders.buchungsdatum DESC         ->  Index Only Scan Backward using kpkp_orders_1 on kpkp_orders  (cost=0.43..34977.68 rows=1717950 width=12)         ->  Index Only Scan Backward using kpkp_invoices_1 on kpkp_invoices  (cost=0.43..34977.68 rows=1717950 width=12) this is fast. and here is the query plan used on my real-life example: Limit  (cost=918.40..918.41 rows=1 width=8)   ->  Sort  (cost=918.40..919.59 rows=473 width=8)         Sort Key: s.belegdatum DESC         ->  Subquery Scan on s  (cost=0.56..916.04 rows=473 width=8)               ->  Append  (cost=0.56..911.31 rows=473 width=327)                     ->  Subquery Scan on "*SELECT* 1" (cost=0.56..624.08 rows=318 width=187)                           ->  Index Scan using soit_erpprozesshistorie_12 on erpprozesshistorie eph (cost=0.56..620.10 rows=318 width=181)                                 Index Cond: ((clientid = '0'::numeric) AND (activeflag = 't'::bpchar) AND (prozessuntertyp = 2) AND (CASE WHEN ((typ = 1) OR (typ = 7)) THEN 1 ELSE CASE WHEN (typ = 3) THEN 2 ELSE CASE WHEN ((typ = 4) OR (typ = 9)) THEN 3 ELSE 4 END END END = 1) AND (CASE WHEN (prozesstyp = 1) THEN true ELSE false END = true) AND (artikelvariante_objid = '1064748816'::numeric))                     ->  Subquery Scan on "*SELECT* 2" (cost=0.43..284.87 rows=155 width=214)                           ->  Index Scan using soit_umsatz_alt_08 on umsatz_alt  (cost=0.43..282.93 rows=155 width=186)                                 Index Cond: ((clientid = '0'::numeric) AND (activeflag = 't'::bpchar) AND (verkauf = true) AND (artikelvariante_objid = '1064748816'::numeric)) so my question is: under wich circumstance does the query-planner use or prefer the 'merge append' over 'append'? Thanks in advance! Am 08.05.25 um 11:57 schrieb Karsten P: > Hi, > > i've already googled so far but didn't find anything regarding my > problem.. > I hope i'm here at the right place. > > Following situation (this is just an simplyfied example): > > suppose we have two tables, lets say > > orders >   - column 'order_number' -> varchar >   - column 'order_date' -> timestamp > > with index on order_date > > and > > invoices >   - column 'invoice_number' -> varchar >   - column 'invoice_date' -> timestamp > > with index on invoice_date > > and many records in both if them. > > now we have a view combining both of them as > > create view documents as > ( >     select order_number as document_number, order_date as > document_date from orders >     union all select invoice_number, invoice_date from invoices > ) > > > finding the last order placed in the database ist really easy: > >   select order_number from orders order by order_date desc limit 1 > > will result in an index scan backward on orders > > same with invoices only... > > but when querying the view > >   select document_number from documents order by document_date desc > limit 1 > > seems to break down to >   - collect all rows from orders >   - combine it with all rows from invoices >   - sort all rows (descending) >   - limit to one row > > with many data this is quite slow. > > I've tested this with PG9.6 and PG14, it doesn't seem to make a > difference (correct me if i'm wrong). > > > So my question is: What about optimizing the query-planner that if > > - a query with unions of selects is executed > - and an 'order by' in combination with 'limit' is applied on the > complete query (not only on subselects) > - and there is a matching index for each select > > the order by and limit - part of the sql is also beeing applied on > each sub-select ? > > actually >     select document_number from documents order by document_date desc > limit 1 > > is beeing processed as >     select order_number from orders >     union all select invoice_number from invoices >     order by document_number desc >     limit 1 > > but would it be possible to let the query-optimizer expand the query to >     select order_number from ( >         (select order_number, order_date from orders order by > order_date desc limit 1) >         union all (select invoice_number, invoice_date from invoices > order by invoice_date desc limit 1) >     ) as subselect >    order by order_date desc >    limit 1 > > as this would use two (or number of unions) index-backward-scans > and than only has to reorder at maximum two rows before limiting to > the first of it? > > this should be significantly faster. > > thanks a lot and greetz, > Karsten > > >