Saturday, December 30, 2006

DISTINT 300ms grouby 1300ms for 10 entries

recentlycommentedposts = web.query('SELECT DISTINCT ON(comments.postid) comments.postid, comments.comment, posts.title, posts.id FROM comments JOIN posts on posts.id=comments.postid order by comments.postid DESC limit 10')
recentlycommentedposts = web.query('''select postid, max(created) as created
from comments
group by postid
order by created desc
limit 10;''')

select p.id, p.title, p.created as post_date, c.comment, a.created as comment_date
from posts p,
comments c,
(select postid, max(created) as created
from comments
group by postid
order by created desc
limit 10) a
where
p.id = a.postid
and c.postid = a.postid
and c.created = a.created
order by post_date desc;

No comments: