Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 977DD632DCB for ; Mon, 19 Jul 2010 12:35:55 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 39029-04 for ; Mon, 19 Jul 2010 15:35:48 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-bw0-f46.google.com (mail-bw0-f46.google.com [209.85.214.46]) by mail.postgresql.org (Postfix) with ESMTP id DE2266328DA for ; Mon, 19 Jul 2010 12:35:47 -0300 (ADT) Received: by bwz1 with SMTP id 1so2356953bwz.19 for ; Mon, 19 Jul 2010 08:35:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from :user-agent:mime-version:to:subject:references:in-reply-to :content-type:content-transfer-encoding; bh=w+sBcYqBX7Wjxt8c+5WdzMJWl2v+cJI3vppjI4r7W/s=; b=SpjECJ12lT/1VhDra0PCmumnaJKASFr8baBnhPhZ07ha9gG13v8glA4NcKrNfGM7oT 3TMZ17pgGrtazbuUPU0yjT7Y4uPoC51q9rks/WDCpx5kpPNr/bXYF8asMt1t6H8YpdaT s7wEhuIzBU3Qk2sb+wbeWAqGhtObniCA7lD3M= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; b=pNOkUzitFMtzKg4RNnuJYD5ux0IlSPoTOmobuv7ZGCVEzLwGn+StjAAE7JDt7f9t2e Ju8Hxlc20EzRv9Qe5MXJE6orducyKc++VT8V6zw638Afk/a+1SoSDhlvD69py3V+sG4z 6WHgJ1/giTa0Td+qCPly9L0j/BwKPhi+EO+Sg= Received: by 10.204.175.73 with SMTP id w9mr3120711bkz.22.1279553736862; Mon, 19 Jul 2010 08:35:36 -0700 (PDT) Received: from [10.1.1.57] (skuns.zoral.com.ua [91.193.166.194]) by mx.google.com with ESMTPS id a9sm26243360bky.23.2010.07.19.08.35.35 (version=TLSv1/SSLv3 cipher=RC4-MD5); Mon, 19 Jul 2010 08:35:36 -0700 (PDT) Message-ID: <4C4470CE.9060902@gmail.com> Date: Mon, 19 Jul 2010 18:35:42 +0300 From: Vitalii Tymchyshyn User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.10) Gecko/20100528 Thunderbird/3.0.5 MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Re: Big field, limiting and ordering References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.5 tagged_above=-10 required=5 tests=BAYES_05=-0.5 X-Spam-Level: X-Archive-Number: 201007/286 X-Sequence-Number: 39675 19.07.10 18:09, Ivan Voras написав(ла): > Hello, > > I don't think this is generally solvable but maybe it is so here goes. > The original situation was this: > > SELECT something, big_field, complex_function(big_field), rank FROM t1 > UNION ALL SELECT something, big_field, complex_function(big_field), rank > from t2 ORDER BY rank LIMIT small_number; > > This query first fetches all big_field datums and does all > complex_function() calculations on them, then orders then by rank, even > though I actually need only small_number of records. There are two > problems here: first, selecting for all big_field values requires a lot > of memory, which is unacceptable, and then, running complex_function() > on all of them takes too long. > > I did get rid of unnecessary complex_function() calculations by nesting > queries like: > > SELECT something, big_field, complex_function(big_field), rank FROM > (SELECT original_query_without_complex_function_but_with_big_field ORDER > BY rank LIMIT small_number); > > but this still leaves gathering all the big_field datum from the > original query. I cannot pull big_field out from this subquery because > it comes from UNION of tables. > > Any suggestions? > You can do the next: SELECT something, big_field, complex_function(big_field), rank FROM (SELECT * from ( (SELECT something, big_field, complex_function(big_field), rank FROM t1 order by rank limit small_number) UNION ALL (SELECT something, big_field, complex_function(big_field), rank from t2 ORDER BY rank LIMIT small_number) ) a ORDER BY rank LIMIT small_number) b; So, you take small_number records from each table, then select small_number best records from resulting set, then do the calculation. Best regards, Vitalii Tymchyshyn