Rewrite Album.sum_bytes to use SQL sum in a single query.
This commit is contained in:
		
							parent
							
								
									f77ab51b14
								
							
						
					
					
						commit
						7d8b8daeba
					
				
					 1 changed files with 15 additions and 16 deletions
				
			
		|  | @ -504,23 +504,22 @@ class Album(ObjectBase, GroupableMixin): | |||
|             self.photodb.log.debug('Committing - remove photo from album') | ||||
|             self.photodb.commit() | ||||
| 
 | ||||
|     def sum_bytes(self, recurse=True, string=False): | ||||
|         if self._sum_bytes_local is None: | ||||
|             #print(self, 'sumbytes cache miss local') | ||||
|             photos = (photo for photo in self.get_photos() if photo.bytes is not None) | ||||
|             self._sum_bytes_local = sum(photo.bytes for photo in photos) | ||||
|         total = self._sum_bytes_local | ||||
| 
 | ||||
|     def sum_bytes(self, recurse=True): | ||||
|         query = ''' | ||||
|         SELECT SUM(bytes) FROM photos | ||||
|         WHERE photos.id IN ( | ||||
|             SELECT photoid FROM album_photo_rel WHERE | ||||
|             albumid IN {qmarks} | ||||
|         ) | ||||
|         ''' | ||||
|         if recurse: | ||||
|             if self._sum_bytes_recursive is None: | ||||
|                 #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) | ||||
|             albumids = [child.id for child in self.walk_children()] | ||||
|         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 | ||||
| 
 | ||||
|     def sum_photos(self): | ||||
|  |  | |||
		Loading…
	
		Reference in a new issue