Re: Speed up this query

From: Mark Stosberg <mark(at)summersault(dot)com>
To: pgsql-performance(at)postgresql(dot)org
Subject: Re: Speed up this query
Date: 2007-02-08 17:11:25
Message-ID: eqflkm$g1t$1@news.hub.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

jeff(dot)ward(at)gmail(dot)com wrote:
> Hi all,
>
> I'm fairly new to SQL, so this is probably a dumb way to form this
> query, but I don't know another.
>
> I want to see the usernames of all the users who have logged on
> today. "users" is my table of users with id's and username's.
> "session_stats" is my table of user sessions where I store site
> activity, and it has a user_id column.
>
> SELECT username FROM users WHERE id IN (SELECT DISTINCT user_id FROM
> session_stats $dateClause AND user_id!=0)

Jeff,

It looks like you need a JOIN instead:

SELECT username from users
JOIN session_stats ON (users.id = session_stats.user_id)
WHERE $dateClause AND user_id != 0);

Check that you also have indexes on both of those columns (check the
docs for "CREATE INDEX" for details. )

Mark

Browse pgsql-performance by date

  From Date Subject
Next Message Tom Lane 2007-02-08 17:17:56 Re: stats collector process high CPU utilization
Previous Message Benjamin Minshall 2007-02-08 16:48:01 stats collector process high CPU utilization