Received: from maia.hub.org (maia-4.hub.org [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id AFDD9633C8B for ; Thu, 20 May 2010 05:20:50 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 34508-08 for ; Thu, 20 May 2010 08:20:43 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-ew0-f224.google.com (mail-ew0-f224.google.com [209.85.219.224]) by mail.postgresql.org (Postfix) with ESMTP id 501AE632FDC for ; Thu, 20 May 2010 05:20:43 -0300 (ADT) Received: by ewy24 with SMTP id 24so688794ewy.34 for ; Thu, 20 May 2010 01:20:41 -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:cc:subject:references:in-reply-to :content-type:content-transfer-encoding; bh=WIUThtslDZtDbfORmxqjvmHxcgXt5Kbbv1rQEsdccWE=; b=UU5cCqTRgYxAgWctupAiKbWQT/TrNm3HLA4Xv9365h70SJGGZHSQEuetDkin6Pcwns 3L8Q7hMopvOIicK0rFNMuZ/hpwM56XjRvAS+dYZgXs/jr/yJh08oR296iprqFsmaUJuh Wx+EL+oPxUmsjve9W9RF0Jg+8SHZmBkcD3XIc= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; b=NXTmANBZnn/6cSmY/o6Bh2rFKFKwKRKODyMogoyX+lYAQ/4b/kQclwu16L3lLJ/wrK wyYdD3xAp2xHZKuE+InKdhhqCTpSjpoge7OUE7r5j9NmOs65Zlqt5zILTxXA7A6Z5QPk hYif4CiscEFw0fPfwpuaiBsVJGytHeoHGV85k= Received: by 10.213.79.76 with SMTP id o12mr4164155ebk.19.1274343638875; Thu, 20 May 2010 01:20:38 -0700 (PDT) Received: from [0.0.0.0] (5357DA75.cable.casema.nl [83.87.218.117]) by mx.google.com with ESMTPS id 16sm4077317ewy.7.2010.05.20.01.20.37 (version=TLSv1/SSLv3 cipher=RC4-MD5); Thu, 20 May 2010 01:20:38 -0700 (PDT) Message-ID: <4BF4F0DA.7060208@gmail.com> Date: Thu, 20 May 2010 10:20:42 +0200 From: Yeb Havinga User-Agent: Thunderbird 2.0.0.24 (Windows/20100228) MIME-Version: 1.0 To: David Jarvis CC: pgsql-performance@postgresql.org Subject: Re: Optimize date query for large child tables: GiST or GIN? References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001 X-Spam-Level: X-Archive-Number: 201005/108 X-Sequence-Number: 38692 Hello David, > The table aggregates 237 million rows from its child tables. The > sluggishness comes from this part of the query: > > m.taken BETWEEN > /* Start date. */ > (extract( YEAR FROM m.taken )||'-01-01')::date AND > /* End date. Calculated by checking to see if the end date wraps > into the next year. If it does, then add 1 to the current year. > */ > (cast(extract( YEAR FROM m.taken ) + greatest( -1 * > sign( > (extract( YEAR FROM m.taken )||'-12-31')::date - > (extract( YEAR FROM m.taken )||'-01-01')::date ), 0 > ) AS text)||'-12-31')::date Either I had too less coffee and completely misunderstand this expression, or it is always true and can be omitted. Could you explain a bit what this part tries to do and maybe also show it's original counterpart in the source database? regards, Yeb Havinga