Re: Slow Query Help

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance


[Postgresql General]     [Postgresql PHP]     [PHP Users]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Yosemite]

  Powered by Linux