X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (neptune.hub.org [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id C8554D1D7F9 for ; Fri, 21 Nov 2003 16:51:01 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 09420-10 for ; Fri, 21 Nov 2003 12:50:31 -0400 (AST) Received: from news.hub.org (news.hub.org [200.46.204.72]) by svr1.postgresql.org (Postfix) with ESMTP id 51CADD1D7E6 for ; Fri, 21 Nov 2003 12:50:31 -0400 (AST) Received: from news.hub.org (news.hub.org [200.46.204.72]) by news.hub.org (8.12.9/8.12.9) with ESMTP id hALGoUNu090675 for ; Fri, 21 Nov 2003 16:50:30 GMT (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.12.9/8.12.9/Submit) id hALGcHkO088132 for pgsql-sql@postgresql.org; Fri, 21 Nov 2003 16:38:17 GMT From: dbichko@genpathpharma.com (Dmitri Bichko) X-Newsgroups: comp.databases.postgresql.sql Subject: OFFSET and subselects Date: 21 Nov 2003 08:38:16 -0800 Organization: http://groups.google.com Lines: 22 Message-ID: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Complaints-To: groups-abuse@google.com To: pgsql-sql@postgresql.org X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200311/322 X-Sequence-Number: 16137 I recently noticed a behaviour which seems quite suboptimal - I am working on a "mart" type application, which in practice means I end up with queries which have a few filters on several central tables, and then a few dozen subselects for other info (which seems to perform better than several dozen equivalent LEFT JOINs do). I am running in trouble with pagination here, somehow (rather naively) I assumed that when doing a LIMIT and OFFSET, the subselects on the records before the OFFSET would not be performed, which quite apparently is not the case. So, LIMIT 50 OFFSET 0 takes 100ms to run, LIMIT 50 OFFSET 50 takes 200ms, LIMIT 50 OFFSET 100 takes 300ms; and so forth, this really becomes unacceptable after a few pages. I was wondering how I migh improve the situation? Two possibilities come to mind: a) run the query without the subselects and store the LIMIT 50 in a temp table, doing a select all from that with the subselects as a separate query or b) similar, but run the queries from the subselects as separate queries, assembling everything in the app. Is one better than the other in any way? Are there better ways to deal with this? Thanks