Re: Reducing the number of my view's database queries

2008-12-12 Thread Malcolm Tredinnick
On Fri, 2008-12-12 at 10:29 -0800, erikcw wrote: > Thanks for all of the responses. Just wanted to share where I'm at at > this point: [... *argh* my eyes!...ok, snipped...] > This seems to work. If you know of any performance tweaks I'd love to > hear about them (however it is incredibly fas

Re: Reducing the number of my view's database queries

2008-12-12 Thread erikcw
Thanks for all of the responses. Just wanted to share where I'm at at this point: self.profilekeyword_set.select_related().all().extra(select={ 'rank_url': 'SELECT url FROM rankreport_keywordrank WHERE rankreport_keywordrank.keyword_id = rankreport_profilekeyword.keyword_id AND rankrepor

Re: Reducing the number of my view's database queries

2008-12-08 Thread Malcolm Tredinnick
On Mon, 2008-12-08 at 07:43 -0800, DavidA wrote: > If I undestand the problem correctly, in MySQL you could do this in > one query as: > > select > m.*, > (select min(created) from model2 where id = m.model2_id) as > first_created, > (select max(created) from model2 where id = m.mode

Re: Reducing the number of my view's database queries

2008-12-08 Thread maeck
I think you are on something here. Below is what concerns me most in the example: data[o.name].append(o.model3_set.all().order_by('created')[0]) This will sort the entire model3 table just to get the min value. And if the db is doing this 6000 times, that is a bit useless. Have you tried using

Re: Reducing the number of my view's database queries

2008-12-08 Thread DavidA
If I undestand the problem correctly, in MySQL you could do this in one query as: select m.*, (select min(created) from model2 where id = m.model2_id) as first_created, (select max(created) from model2 where id = m.model2_id) as last_created from model1 m ; I don't know how that tran

Re: Reducing the number of my view's database queries

2008-12-07 Thread Malcolm Tredinnick
On Sat, 2008-12-06 at 20:56 -0800, erikcw wrote: > Hi all, > > I'm trying to write a model query that will return a queryset along > with the latest (and earliest) data from 2 related models. > > Right now I'm doing something like this: > > objects = Model1.objects.filter(user=3).select_relate

Re: Reducing the number of my view's database queries

2008-12-07 Thread Malcolm Tredinnick
On Sun, 2008-12-07 at 18:09 -0800, Dave Dash wrote: > Without knowing too much about your code, the only thing I can say, > having had a similar issue, was that select_related witha specified > depth generally made for more efficient queries. This is quite true. Also remember that if you know w

Re: Reducing the number of my view's database queries

2008-12-07 Thread Dave Dash
Without knowing too much about your code, the only thing I can say, having had a similar issue, was that select_related witha specified depth generally made for more efficient queries. For fun, I started on some code to output database queries on all my pages while I'm debugging: Queries