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> <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: response = render_to_response("defender_advanced_report.html", {"query":found_entries}, Context( {'end_date':end_date_string } ) ) response['Content-Type'] = 'application/vnd.ms-excel' response['Content-Disposition'] = 'attachment;filename=DEFENDER.xls' return response else: no_results=Context({'results':'No results'}) return render_to_response("user/defender_advanced.html", {"no_results" : no_results}) There is a query page where the start_date and end_date is being sent. Then the do_defender_advanced will process it and generate an Excel using the template. > > -- > 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 -~----------~----~----~----~------~----~------~--~---