But I am confused here. How do iterate through the data another time? Do I call the select_related in my views.py code like:
manager_info = found_entries.select_related() then how do i iterate through manager_info in my template? On Tue, Nov 3, 2009 at 3:46 PM, Jani Tiainen <rede...@gmail.com> wrote: > > Your model was not-so-good Python/Django code style so it was bit hard > to read but... > > Low Kian Seong kirjoitti: >> On Tue, Nov 3, 2009 at 3:07 PM, Jani Tiainen <rede...@gmail.com> wrote: >>> Low Kian Seong kirjoitti: >>>> On Tue, Nov 3, 2009 at 1:33 PM, Jani Tiainen <rede...@gmail.com> wrote: >>>>> Low Kian Seong kirjoitti: >>>>>> I have about 3k plus record in my db table and am trying to do a >>>>>> simple render_to_response of a template sending it the results of a >>>>>> query. I debugged this and found that the total time taken for an >>>>>> operation like this is 50 seconds! Is there anyway to speed this up? >>>>>> >>>>> Answer is maybe. >>>> Model: >>>> >>> [clipped model code] >>> >>>> I isolated the code and ran the render_to_response by itself and >>>> measured the time. >>>> >>>>> First you have to provide model, view and template code you're using to >>>>> render your code, second the way you measured this 50 seconds. >>> How about view and template..? It seems that you have few foreign keys >>> at least that may cause extra roundtrips to db. > > > >> >> Template: >> >> <html> >> <head> >> </head> >> {% if query %} >> {% load utils %} >> <table border=1> >> <thead> >> <tr> >> >> <th bgcolor="#FFFF33" scope="col">Requestor</th> >> <th bgcolor="#FFFF33" scope="col">Country (User)</th> >> <th bgcolor="#FFFF33" scope="col">AD Domain</th> >> <th bgcolor="#FFFF33" scope="col">Company Name</th> >> <th bgcolor="#FFFF33" scope="col">Defender ID</th> >> <th bgcolor="#FFFF33" scope="col">First Name</th> >> <th bgcolor="#FFFF33" scope="col">Last Name</th> >> <th bgcolor="#FFFF33" scope="col">Token S/N</th> >> <th bgcolor="#FFFF33" scope="col">Token Pin</th> >> <th bgcolor="#FFFF33" scope="col">IT Mgr</th> >> <th bgcolor="#FFFF33" scope="col">HD Mgr</th> >> <th bgcolor="#FFFF33" scope="col">SR Approver</th> >> <th bgcolor="#FFFF33" scope="col">SR No. (Setup Cost)</th> >> <th bgcolor="#FFFF33" scope="col">SR No. (On Going Cost)</th> >> <th bgcolor="#FFFF33" scope="col">SR No. (Orange Cost)</th> >> <th bgcolor="#FFFF33" scope="col">Creation Date</th> >> <th bgcolor="#FFFF33" scope="col">Acct. Status</th> >> </tr> >> </thead> >> {% for record in query %} >> <tr> >> <th>{{record.requestor}}</th> >> <th>{{record.country}}</th> >> <th>{{record.ad_domain_group}}</th> >> <th>{{record.company}}</th> >> <th align="left">{{record.defender_id}}</th> >> <th align="left">{{record.first_name}}</th> >> <th align="left">{{record.last_name}}</th> >> <th>{{record.rsa_token_serial_number}}</th> >> <th>{{record.token_initial_pin}}</th> > >> <th>{{record.it_manager_id.name}}</th> >> <th>{{record.sr_manager_id.name}}</th> >> <th>{{record.hd_manager_id.name}}</th> > > All these three might hit another roundtrip to database. Since you're > using them, you might want to use select_related() method. > >> <th>{{ record.ad_domain_group|defender_setup }}</th> >> <th>{{ record.ad_domain_group|defender_on_going }}</th> >> <th>{{ record.ad_domain_group|defender_orange }}</th> >> <th>{{record.creation_date}}</th> >> <th>{{ record.disable_date|show_status:end_date }}</th> >> </tr> >> {% endfor %} >> </table> >> {% endif %} >> </html> >> >> >> views.py: >> >> def do_defender_advanced(request): >> query = request.POST.get('query_text','') >> entries = defender.objects.all() >> query_text_string = request.POST['query_text'] >> start_date_string = request.POST.get('start_date','') >> end_date_string = request.POST.get('end_date',str_today) >> >> if ('query_text' in request.POST) and >> request.POST['query_text'].strip(): >> >> query_text_string = request.POST['query_text'] >> start_date_string = request.POST.get('start_date','') >> end_date_string = request.POST.get('end_date',str_today) >> if len(start_date_string) == 0: >> start_date_string = str_today >> if len(end_date_string) == 0: >> end_date_string = str_today >> query_text_string = request.POST['query_text'] >> qset=( >> Q(first_name__icontains=query_text_string)| >> Q(last_name__icontains=query_text_string)| >> Q(country__icontains=query_text_string)| >> Q(existing_defender_id__icontains=query_text_string) >> ) >> >> if query_text_string.lower() == "all": >> found_entries = defender.objects.all() >> elif query_text_string.lower() != "all": >> found_entries = defender.objects.filter(qset) >> found_entries = found_entries.filter( >> # status = 'active', >> >> creation_date__range=[start_date_string, >> >> end_date_string]).values() >> else: >> found_entries = '' >> if len(found_entries) > 0: > > Note that this will fetch all entries in queryset from DB to memory. If > you want just to know is there anything use .count(), not len(). > > Both features (len, count and select_related) are documented in : > <http://docs.djangoproject.com/en/dev/ref/models/querysets/> > >> response = render_to_response("defender_advanced_report.html", >> {"query":found_entries}, >> Context( {'end_date':end_date_string } ) >> ) > > You might want to check how much data you actually get (in terms of bytes). > > -- > Jani Tiainen > > > > -- Low Kian Seong blog: http://lowkster.blogspot.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---