Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 2C22B9FB3FD for ; Thu, 8 Feb 2007 13:23:17 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-new, port 10024) with ESMTP id 75661-01-2 for ; Thu, 8 Feb 2007 13:23:09 -0400 (AST) Received: from news.hub.org (news.hub.org [200.46.204.72]) by postgresql.org (Postfix) with ESMTP id D106C9FB398 for ; Thu, 8 Feb 2007 13:15:40 -0400 (AST) Received: from news.hub.org (news.hub.org [200.46.204.72]) by news.hub.org (8.13.8/8.13.8) with ESMTP id l18HFdRd017402 for ; Thu, 8 Feb 2007 13:15:39 -0400 (AST) (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.13.8/8.13.8/Submit) id l18HBqZt017025 for pgsql-performance@postgresql.org; Thu, 8 Feb 2007 13:11:52 -0400 (AST) (envelope-from news) From: Mark Stosberg X-Newsgroups: pgsql.performance Subject: Re: Speed up this query Date: Thu, 08 Feb 2007 12:11:25 -0500 Organization: Hub.Org Networking Services Lines: 26 Message-ID: References: <1170945011.442790.324760@a75g2000cwd.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@news.hub.org User-Agent: Thunderbird 1.5.0.8 (X11/20061109) In-Reply-To: <1170945011.442790.324760@a75g2000cwd.googlegroups.com> To: pgsql-performance@postgresql.org X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200702/86 X-Sequence-Number: 23140 jeff.ward@gmail.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