pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Mark Stosberg <mark@summersault.com>
To: pgsql-performance@postgresql.org
Subject: Re: Speed up this query
Date: Thu, 08 Feb 2007 12:11:25 -0500
Message-ID: <eqflkm$g1t$1@news.hub.org> (raw)
In-Reply-To: <1170945011.442790.324760@a75g2000cwd.googlegroups.com>
References: <1170945011.442790.324760@a75g2000cwd.googlegroups.com>

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



Message-ID: <eqflkm$g1t$1@news.hub.org>
Permalink:  ../eqflkm$g1t$1@news.hub.org/
Also on:    postgresql.org/message-id/eqflkm$g1t$1@news.hub.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: mark@summersault.com
  Subject: Re: Speed up this query
  In-Reply-To: <eqflkm$g1t$1@news.hub.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox