Faster track selection diaglog

Use better query to load last_played times along with tracks
This commit is contained in:
Keith Edmunds 2023-10-04 01:50:51 +01:00
parent b2f826dfcc
commit 9847252038
2 changed files with 20 additions and 11 deletions

View File

@ -25,6 +25,7 @@ from sqlalchemy import (
from sqlalchemy.orm import (
declarative_base,
joinedload,
relationship,
)
from sqlalchemy.orm.exc import (
@ -638,9 +639,9 @@ class Tracks(Base):
path: str = Column(String(2048), index=False, nullable=False, unique=True)
mtime = Column(Float, index=True)
bitrate = Column(Integer, nullable=True, default=None)
playlistrows: PlaylistRows = relationship("PlaylistRows", back_populates="track")
playlistrows: List[PlaylistRows] = relationship("PlaylistRows", back_populates="track")
playlists = association_proxy("playlistrows", "playlist")
playdates: Playdates = relationship("Playdates", back_populates="track")
playdates: List[Playdates] = relationship("Playdates", back_populates="track")
def __repr__(self) -> str:
return (
@ -715,15 +716,22 @@ class Tracks(Base):
@classmethod
def search_titles(cls, session: scoped_session, text: str) -> List["Tracks"]:
"""Search case-insenstively for titles containing str"""
"""
Search case-insenstively for titles containing str
The query performs an outer join with 'joinedload' to populate the results
from the Playdates table at the same time. unique() needed; see
https://docs.sqlalchemy.org/en/20/orm/queryguide/relationships.html#joined-eager-loading
"""
return (
session.execute(
select(cls)
.join(Playdates, isouter=True)
.options(joinedload(Tracks.playdates))
.where(cls.title.like(f"{text}%"))
.group_by(cls.title)
.order_by(cls.title)
)
.scalars()
.unique()
.all()
)

View File

@ -1937,7 +1937,9 @@ class DbDialog(QDialog):
return
if track:
self.musicmuster.visible_playlist_tab().insert_track(self.session, track, note)
self.musicmuster.visible_playlist_tab().insert_track(
self.session, track, note
)
else:
self.musicmuster.visible_playlist_tab().insert_header(self.session, note)
@ -1956,12 +1958,11 @@ class DbDialog(QDialog):
if matches:
for track in matches:
last_played = None
show_last_played = False
if len(matches) < 20:
show_last_played = True
last_playdate = max(track.playdates, key=lambda p: p.lastplayed, default=None)
if last_playdate:
last_played = last_playdate.lastplayed
last_playdate = max(
track.playdates, key=lambda p: p.lastplayed, default=None
)
if last_playdate:
last_played = last_playdate.lastplayed
t = QListWidgetItem()
track_text = (
f"{track.title} - {track.artist} "