[GENERAL] Need help optimizing this query

2007-07-18 Thread Pat Maddox
I've got a query that's taking forever (as will be obvious when you see it and the explain output). I can't figure out what indexes to add to make this run faster. I'd appreciate any help. Pat SELECT SUM(CASE WHEN (hit IS TRUE AND logged_in IS TRUE) THEN 1 ELSE 0 END) AS count_hits_console,

Re: [GENERAL] Need help optimizing this query

2007-07-18 Thread Pat Maddox
On 7/18/07, Pat Maddox <[EMAIL PROTECTED]> wrote: I've got a query that's taking forever (as will be obvious when you see it and the explain output). I can't figure out what indexes to add to make this run faster. I'd appreciate any help. Pat SELECT SUM(CASE WHEN (hit IS TRUE AND logged_in

Re: [GENERAL] Need help optimizing this query

2007-07-18 Thread Michael Glaesemann
On 7/18/07, Pat Maddox <[EMAIL PROTECTED]> wrote: I've got a query that's taking forever (as will be obvious when you see it and the explain output). I can't figure out what indexes to add to make this run faster. I'd appreciate any help. I'm curious why it's choosing to use hash joins rath

Re: [GENERAL] Need help optimizing this query

2007-07-18 Thread Pat Maddox
On 7/18/07, Michael Glaesemann <[EMAIL PROTECTED]> wrote: On 7/18/07, Pat Maddox <[EMAIL PROTECTED]> wrote: > I've got a query that's taking forever (as will be obvious when you > see it and the explain output). I can't figure out what indexes to > add to make this run faster. I'd appreciate a

Re: [GENERAL] Need help optimizing this query

2007-07-18 Thread Michael Glaesemann
On Jul 18, 2007, at 16:01 , Michael Glaesemann wrote: CREATE OR REPLACE FUNCTION one_when(BOOLEAN) RETURNS INTEGER LANGUAGE SQL as $_$SELECT value_when($1,1)$_$; I forgot to add that you can cast booleans to integers, so one_when (expr) is equivalent to expr::int: # SELECT (true and false)

Re: [GENERAL] Need help optimizing this query

2007-07-18 Thread Michael Glaesemann
On Jul 18, 2007, at 16:12 , Pat Maddox wrote: ERROR: invalid reference to FROM-clause entry for table "video_views" LINE 20: JOIN assets ON (video_views.video_id=videos.id) ^ HINT: There is an entry for table "video_views", but it cannot be referenced from this part of

Re: [GENERAL] Need help optimizing this query

2007-07-18 Thread Pat Maddox
On 7/18/07, Michael Glaesemann <[EMAIL PROTECTED]> wrote: On Jul 18, 2007, at 16:12 , Pat Maddox wrote: > ERROR: invalid reference to FROM-clause entry for table "video_views" > LINE 20: JOIN assets ON (video_views.video_id=videos.id) > ^ > HINT: There is an entry for

Re: [GENERAL] Need help optimizing this query

2007-07-18 Thread Pat Maddox
On 7/18/07, Pat Maddox <[EMAIL PROTECTED]> wrote: On 7/18/07, Michael Glaesemann <[EMAIL PROTECTED]> wrote: > > On Jul 18, 2007, at 16:12 , Pat Maddox wrote: > > > ERROR: invalid reference to FROM-clause entry for table "video_views" > > LINE 20: JOIN assets ON (video_views.video_id=videos.id) >

Re: [GENERAL] Need help optimizing this query

2007-07-18 Thread Michael Glaesemann
On Jul 18, 2007, at 16:48 , Pat Maddox wrote: For some reason the functions you wrote are giving me trouble (there's a BIGINT involved, I tried changing the functions around but kept having issues). You might try these, if you're interested. CREATE OR REPLACE FUNCTION ternary(BOOLEAN, BIGINT

Re: [GENERAL] Need help optimizing this query

2007-07-18 Thread Pat Maddox
On 7/18/07, Michael Glaesemann <[EMAIL PROTECTED]> wrote: On Jul 18, 2007, at 16:48 , Pat Maddox wrote: > For some reason the functions you wrote are giving me trouble (there's > a BIGINT involved, I tried changing the functions around but kept > having issues). You might try these, if you're

Re: [GENERAL] Need help optimizing this query

2007-07-18 Thread Michael Glaesemann
On Jul 18, 2007, at 17:34 , Pat Maddox wrote: -> Sort (cost=803054.95..809363.98 rows=2523610 width=64) (actual time=67076.407..75441.274 rows=5799447 loops=1) Sort Key: videos.id, videos.title, videos.guid If I'm reading this right, it looks like a majori