Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U2j2j-0001PJ-Bu for pgsql-performance@arkaria.postgresql.org; Tue, 05 Feb 2013 13:58:37 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1U2j2i-0006Te-8r for pgsql-performance@arkaria.postgresql.org; Tue, 05 Feb 2013 13:58:36 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U2j2h-0006TY-9B for pgsql-performance@postgresql.org; Tue, 05 Feb 2013 13:58:35 +0000 Received: from smtp-outbound-2.vmware.com ([208.91.2.13]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U2j2e-0007M4-Fe for pgsql-performance@postgresql.org; Tue, 05 Feb 2013 13:58:34 +0000 Received: from sc9-mailhost3.vmware.com (sc9-mailhost3.vmware.com [10.113.161.73]) by smtp-outbound-2.vmware.com (Postfix) with ESMTP id D0CD2283CE; Tue, 5 Feb 2013 05:58:28 -0800 (PST) Received: from zcs-prod-mta-2.vmware.com (zcs-prod-mta-2.vmware.com [10.113.163.64]) by sc9-mailhost3.vmware.com (Postfix) with ESMTP id CE71B401B8; Tue, 5 Feb 2013 05:58:28 -0800 (PST) Received: from zcs-prod-mta-2.vmware.com (localhost.localdomain [127.0.0.1]) by zcs-prod-mta-2.vmware.com (Postfix) with ESMTP id B6640E000A; Tue, 5 Feb 2013 05:58:28 -0800 (PST) Received: from [10.113.225.145] (unknown [10.113.160.14]) by zcs-prod-mta-2.vmware.com (Postfix) with ESMTPSA; Tue, 5 Feb 2013 05:58:27 -0800 (PST) Message-ID: <51111000.1010200@vmware.com> Date: Tue, 05 Feb 2013 15:58:24 +0200 From: Heikki Linnakangas Organization: VMware User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.12) Gecko/20130116 Icedove/10.0.12 MIME-Version: 1.0 To: Will Platnick CC: pgsql-performance@postgresql.org Subject: Re: Slow Query Help References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -6.9 (------) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org On 05.02.2013 05:45, Will Platnick wrote: > We upgraded from PG 9.1 to 9.2. Since the upgrade, the # of active queries has raised significantly, especially during our peak time where lots of users are logging in. According to New Relic, this query is now taking up the most amount of time during peak activity and my pg_stat_activity and slow log sampling agrees. We have 3 DB servers referenced here, production running 9.2.2, semi-idle (idle except for replication when I ran the test) running 9.2.2, and 9.1.3 completely idle with an old dump restored. The only thing that stands out is that it always checks both indexes for matches. Since you only want a single row as a result, it seems like it would be better to first check one index, and only check the other one if there's no match. Rewriting the query with UNION should do that: SELECT id, username, password, email, verified, timezone FROM users WHERE lower(username) = 'randomuser' UNION ALL SELECT id, username, password, email, verified, timezone FROM users WHERE lower(email) = 'randomuser' LIMIT 1; Also, if you can assume that email addresses always contain the @-character, you could take advantage of that and only do the lower(email) = 'randomuser' search if there is one. - Heikki -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance