Re: Slow Query Help - Mailing list pgsql-performance

From Heikki Linnakangas
Subject Re: Slow Query Help
Date
Msg-id 51111000.1010200@vmware.com
Whole thread Raw
In response to Slow Query Help  (Will Platnick <wplatnick@gmail.com>)
List pgsql-performance
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
duringour peak time where lots of users are logging in.  According to New Relic, this query is now taking up the most
amountof time during peak activity and my pg_stat_activity and slow log sampling agrees. We have 3 DB servers
referencedhere, production running 9.2.2, semi-idle (idle except for replication when I ran the test) running 9.2.2,
and9.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


pgsql-performance by date:

Previous
From: Will Platnick
Date:
Subject: Slow Query Help
Next
From: Johnny Tan
Date:
Subject: postgresql.conf recommendations