1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-11-14 22:22:18 +00:00

speed up the getArtists function with subquery join

This commit is contained in:
Emory P 2014-02-02 03:42:45 -05:00
parent 1aa2a2c62b
commit 8570884e6e

View File

@ -2,7 +2,7 @@
from flask import request from flask import request
from web import app from web import app
from db import Folder, Artist, Album, Track, func from db import Folder, Artist, Album, Track, func, session
from api import get_entity from api import get_entity
import uuid, time, string import uuid, time, string
import os.path import os.path
@ -110,8 +110,13 @@ def show_directory():
def list_artists(): def list_artists():
# According to the API page, there are no parameters? # According to the API page, there are no parameters?
indexes = {} indexes = {}
for artist in Artist.query.all():
# Optimized query instead of using backrefs, is there a way to speed up the backref?
c = session.query(Album.artist_id, func.count(Album.artist_id).label('c')).group_by(Album.artist_id).subquery(name='c')
for artist in session.query(Artist.name, Artist.id, c.c.c.label('albums')).join(c).order_by(Artist.name).all():
index = artist.name[0].upper() if artist.name else '?' index = artist.name[0].upper() if artist.name else '?'
if index in map(str, xrange(10)): if index in map(str, xrange(10)):
index = '#' index = '#'
elif index not in string.letters: elif index not in string.letters:
@ -126,10 +131,14 @@ def list_artists():
'artists': { 'artists': {
'index': [ { 'index': [ {
'name': k, 'name': k,
'artist': [ a.as_subsonic_artist(request.user) for a in sorted(v, key = lambda a: a.name.lower()) ] 'artist': [ {
} for k, v in sorted(indexes.iteritems()) ] 'id': str(a.id),
} 'name': a.name,
}) 'albumCount': a.albums
} for a in v ]
} for k, v in indexes.iteritems() ]
}
})
@app.route('/rest/getArtist.view', methods = [ 'GET', 'POST' ]) @app.route('/rest/getArtist.view', methods = [ 'GET', 'POST' ])
def artist_info(): def artist_info():