# HG changeset patch # User cavaliet # Date 1383836001 -3600 # Node ID aa4d01f2adb706a1482901899532ee332d3c4fc1 # Parent 7331ecc46cba041b6284a3acbe062db05dd19aaf main search request optimisation diff -r 7331ecc46cba -r aa4d01f2adb7 src/jocondelab/views/front_office.py --- a/src/jocondelab/views/front_office.py Wed Nov 06 17:56:19 2013 +0100 +++ b/src/jocondelab/views/front_office.py Thu Nov 07 15:53:21 2013 +0100 @@ -81,7 +81,8 @@ else: self.template_name = "jocondelab/front_search.html" - qs = Notice.objects.prefetch_related("images").filter(image=True) + # Get first image url with extra : avoid prefetch on all images + qs = Notice.objects.extra(select={'relative_url': '"core_noticeimage"."relative_url"'}).filter(image=True).filter(images__main=True) if emptysearch: if not cache.get('notice_count'): @@ -90,19 +91,18 @@ # Optimize random : order_by('?') is too slow # generate_series(1, 100) and not generate_series(1, 12) to be sure we have existing ids orm_request = """ -SELECT "core_notice"."id", "core_notice"."ref", "core_notice"."adpt", "core_notice"."appl", "core_notice"."aptn", "core_notice"."attr", "core_notice"."autr", "core_notice"."bibl", "core_notice"."comm", "core_notice"."contact", "core_notice"."coor", "core_notice"."copy", "core_notice"."dacq", "core_notice"."data", "core_notice"."dation", "core_notice"."ddpt", "core_notice"."decv", "core_notice"."deno", "core_notice"."depo", "core_notice"."desc", "core_notice"."desy", "core_notice"."dims", "core_notice"."dmaj", "core_notice"."dmis", "core_notice"."domn", "core_notice"."drep", "core_notice"."ecol", "core_notice"."epoq", "core_notice"."etat", "core_notice"."expo", "core_notice"."gene", "core_notice"."geohi", "core_notice"."hist", "core_notice"."image", "core_notice"."insc", "core_notice"."inv", "core_notice"."label", "core_notice"."labo", "core_notice"."lieux", "core_notice"."loca", "core_notice"."loca2", "core_notice"."mill", "core_notice"."milu", "core_notice"."mosa", "core_notice"."msgcom", "core_notice"."museo", "core_notice"."nsda", "core_notice"."onom", "core_notice"."paut", "core_notice"."pdat", "core_notice"."pdec", "core_notice"."peoc", "core_notice"."peri", "core_notice"."peru", "core_notice"."phot", "core_notice"."pins", "core_notice"."plieux", "core_notice"."prep", "core_notice"."puti", "core_notice"."reda", "core_notice"."refim", "core_notice"."repr", "core_notice"."srep", "core_notice"."stat", "core_notice"."tech", "core_notice"."tico", "core_notice"."titr", "core_notice"."util", "core_notice"."video", "core_notice"."www" +SELECT "core_notice"."id", "core_notice"."ref", "core_notice"."adpt", "core_notice"."appl", "core_notice"."aptn", "core_notice"."attr", "core_notice"."autr", "core_notice"."bibl", "core_notice"."comm", "core_notice"."contact", "core_notice"."coor", "core_notice"."copy", "core_notice"."dacq", "core_notice"."data", "core_notice"."dation", "core_notice"."ddpt", "core_notice"."decv", "core_notice"."deno", "core_notice"."depo", "core_notice"."desc", "core_notice"."desy", "core_notice"."dims", "core_notice"."dmaj", "core_notice"."dmis", "core_notice"."domn", "core_notice"."drep", "core_notice"."ecol", "core_notice"."epoq", "core_notice"."etat", "core_notice"."expo", "core_notice"."gene", "core_notice"."geohi", "core_notice"."hist", "core_notice"."image", "core_notice"."insc", "core_notice"."inv", "core_notice"."label", "core_notice"."labo", "core_notice"."lieux", "core_notice"."loca", "core_notice"."loca2", "core_notice"."mill", "core_notice"."milu", "core_notice"."mosa", "core_notice"."msgcom", "core_notice"."museo", "core_notice"."nsda", "core_notice"."onom", "core_notice"."paut", "core_notice"."pdat", "core_notice"."pdec", "core_notice"."peoc", "core_notice"."peri", "core_notice"."peru", "core_notice"."phot", "core_notice"."pins", "core_notice"."plieux", "core_notice"."prep", "core_notice"."puti", "core_notice"."reda", "core_notice"."refim", "core_notice"."repr", "core_notice"."srep", "core_notice"."stat", "core_notice"."tech", "core_notice"."tico", "core_notice"."titr", "core_notice"."util", "core_notice"."video", "core_notice"."www", "core_noticeimage"."relative_url" FROM ( SELECT 1 + floor(random() * %i)::integer AS id FROM generate_series(1, 100) g GROUP BY 1 ) r JOIN "core_notice" USING (id) -WHERE "core_notice"."image" = true +INNER JOIN "core_noticeimage" ON ("core_notice"."id" = "core_noticeimage"."notice_id") +WHERE "core_notice"."image" = true AND "core_noticeimage"."main" = true LIMIT 12; """ ns = list(Notice.objects.raw(orm_request % context["count"])) - # list because prefetch_related_objects needs list and not QuerySet or RawQuerySet - prefetch_related_objects(ns, ['images']) else: uri_cache = {} if dbpedia_uris: @@ -148,18 +148,11 @@ notices = [] termsbythesaurus = get_terms_by_thesaurus(ns, lang) for n in ns: - #termsbythesaurus = get_terms_by_thesaurus(n, lang) - # select first image with "_p.jpg" - selected_image = None - for i in n.images.all(): - if i.main: - selected_image = settings.JOCONDE_IMAGE_BASE_URL + i.relative_url - break noticedict = { "id": n.id, "title": n.titr, "designation": " | ".join([ v for v in [ n.deno, n.appl] if v ]), - "image": selected_image, + "image": settings.JOCONDE_IMAGE_BASE_URL + n.relative_url, "author": n.autr, "terms_by_thesaurus": termsbythesaurus[n.pk], "datation": termsbythesaurus[n.pk].get("PERI",{}).get("translated",[]) + termsbythesaurus[n.pk].get("EPOQ",{}).get("translated",[])