1. Chris Moffitt
  2. satchmo
  3. Issues
Issue #1446 resolved

optimizing db-hits

Andrea de la Huerta
created an issue

I spent some time optimizing the unnecessary db-hits on the shop. Some of the improvements need the new prefetch_related function (=> Django 1.4) though. So maybe only some things could get integrated into trunk. I made a lot of tests with the debug-toolbar, and i'm getting a good amount of hits saved pro product on the category listings (on my project, about 9 hits saved pro product with/without the patch), so it does brings a good performance impact:

{{{ diff -r c143e491e4ae satchmo/apps/product/models.py --- a/satchmo/apps/product/models.py Sat Aug 18 20:02:30 2012 +0000 +++ b/satchmo/apps/product/models.py Fri Sep 21 11:28:46 2012 +0000 @@ -134,8 +134,9 @@

 def _get_mainImage(self):
     img = False
  • if self.images.count() > 0:
  • img = self.images.order_by('sort')[0]
  • images = self.images.order_by('sort')
  • if images:
  • img = images[0] else: if self.parent_id and self.parent != self: img = self.parent.main_image @@ -163,9 +164,9 @@

     if variations:
         slugs = qry.filter(site=self.site, active=True, **kwargs).values_list('slug',flat=True)
    
    • return Product.objects.filter(Q(productvariationparentproductslugin = slugs)|Q(slug__in = slugs))
    • return Product.objects.filter(Q(productvariationparentproductslugin = slugs)|Q(slug__in = slugs)).prefetch_related('productimage_set') else:
    • return qry.filter(site=self.site, active=True, productvariationparentisnull=True, **kwargs)
    • return qry.filter(site=self.site, active=True, productvariationparentisnull=True, **kwargs).prefetch_related('productimage_set')

    def translated_attributes(self, language_code=None): if not language_code: diff -r c143e491e4ae satchmo/apps/satchmo_ext/recentlist/context_processors.py --- a/satchmo/apps/satchmo_ext/recentlist/context_processors.py Sat Aug 18 20:02:30 2012 +0000 +++ b/satchmo/apps/satchmo_ext/recentlist/context_processors.py Fri Sep 21 11:28:46 2012 +0000 @@ -5,17 +5,7 @@ """Puts the recently-viewed products in the page variables""" recent = request.session.get('RECENTLIST',[]) maxrecent = config_value('PRODUCT','RECENT_MAX') -
    - products = [] - for slug in recent: - if len(products) > maxrecent: - break -
    - try: - p = Product.objects.get_by_site(slugexact = slug) - products.append(p) - except Product.DoesNotExist: - pass + products = Product.objects.active_by_site().filter( + slugin = recent[:maxrecent]).prefetch_related('productimage_set') + return {'recent_products' : products}

- return {'recent_products' : products}

}}}

Comments (1)

  1. Log in to comment