X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 166FD9DC9AB for ; Mon, 23 Jan 2006 15:19:18 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 18514-02 for ; Mon, 23 Jan 2006 15:19:17 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id E0FB39DC85C for ; Mon, 23 Jan 2006 15:19:14 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.1/8.13.1) with ESMTP id k0NJJED4007600; Mon, 23 Jan 2006 14:19:14 -0500 (EST) To: =?ISO-8859-1?Q?=DCmit_=D6ztosun?= cc: pgsql-performance@postgresql.org Subject: Re: Slow queries consisting inner selects and order bys & hack to speed up In-reply-to: References: Comments: In-reply-to =?ISO-8859-1?Q?=DCmit_=D6ztosun?= message dated "Sat, 21 Jan 2006 22:55:23 +0200" Date: Mon, 23 Jan 2006 14:19:14 -0500 Message-ID: <7599.1138043954@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.096 required=5 tests=[AWL=0.096] X-Spam-Score: 0.096 X-Spam-Level: X-Archive-Number: 200601/415 X-Sequence-Number: 16893 =?ISO-8859-1?Q?=DCmit_=D6ztosun?= writes: > Our application uses typical queries similar to following (very simplified): > SELECT > part_id, > part_name, > (SELECT > SUM(amount) FROM part_movements M > WHERE P.part_id =3D M.part_id > ) as part_amount > FROM parts P > ORDER BY part_name > LIMIT 50 > Postgres seems to compute all possible rows and then sorts the > results, which nearly renders the paging meaningless. Yeah. The general rule is that sorting happens after computing the SELECT values --- this is more or less required for cases where the ORDER BY refers to a SELECT-list item. You'd probably have better results by writing a sub-select: SELECT part_id, part_name, (SELECT SUM(amount) FROM part_movements M WHERE P.part_id = M.part_id ) as part_amount FROM (SELECT part_id, part_name FROM parts P WHERE whatever ORDER BY whatever LIMIT n) as P; This will do the part_movements stuff only for rows that make it out of the sub-select. Another approach is to make sure the ORDER BY is always on an indexed column; in cases where the ORDER BY is done by an indexscan instead of a sort, calculation of the unwanted SELECT-list items does not happen. However, this only works as long as LIMIT+OFFSET is fairly small. Lastly, are you on a reasonably current Postgres version (performance complaints about anything older than 8.0 will no longer be accepted with much grace), and are your statistics up to date? The ANALYZE shows rowcount estimates that seem suspiciously far off: -> Seq Scan on scf_stokkart stok (cost=0.00..142622.54 rows=25 width=503) (actual time=4.726..19324.633 rows=4947 loops=1) Filter: (upper((durum)::text) = 'A'::text) This is important because, if the planner realized that the SELECT-list items were going to be evaluated 5000 times not 25, it might well choose a different plan. regards, tom lane