Rewrite Album.sum_bytes to use SQL sum in a single query.

This commit is contained in:
voussoir 2018-04-15 02:52:41 -07:00
parent f77ab51b14
commit 7d8b8daeba

View file

@ -504,23 +504,22 @@ class Album(ObjectBase, GroupableMixin):
self.photodb.log.debug('Committing - remove photo from album') self.photodb.log.debug('Committing - remove photo from album')
self.photodb.commit() self.photodb.commit()
def sum_bytes(self, recurse=True, string=False): def sum_bytes(self, recurse=True):
if self._sum_bytes_local is None: query = '''
#print(self, 'sumbytes cache miss local') SELECT SUM(bytes) FROM photos
photos = (photo for photo in self.get_photos() if photo.bytes is not None) WHERE photos.id IN (
self._sum_bytes_local = sum(photo.bytes for photo in photos) SELECT photoid FROM album_photo_rel WHERE
total = self._sum_bytes_local albumid IN {qmarks}
)
'''
if recurse: if recurse:
if self._sum_bytes_recursive is None: albumids = [child.id for child in self.walk_children()]
#print(self, 'sumbytes cache miss recursive')
child_bytes = sum(child.sum_bytes(recurse=True) for child in self.get_children())
self._sum_bytes_recursive = self._sum_bytes_local + child_bytes
total = self._sum_bytes_recursive
if string:
return bytestring.bytestring(total)
else: else:
albumids = [self.id]
query = query.format(qmarks='(%s)' % ','.join('?' * len(albumids)))
bindings = albumids
total = self.photodb.sql_select_one(query, bindings)[0]
return total return total
def sum_photos(self): def sum_photos(self):