mirror of
https://github.com/TagStudioDev/TagStudio.git
synced 2026-02-01 15:49:09 +00:00
fix: optimize queries
This commit is contained in:
@@ -434,7 +434,6 @@ class Library:
|
||||
selectinload(Entry.tags).options(
|
||||
selectinload(Tag.aliases),
|
||||
selectinload(Tag.subtags),
|
||||
selectinload(Tag.parent_tags),
|
||||
),
|
||||
)
|
||||
entry = session.scalar(statement)
|
||||
@@ -444,6 +443,32 @@ class Library:
|
||||
make_transient(entry)
|
||||
return entry
|
||||
|
||||
def get_entries_full(self, entry_ids: list[int] | set[int]) -> Iterator[Entry]:
|
||||
"""Load entry and join with all joins and all tags."""
|
||||
with Session(self.engine) as session:
|
||||
statement = select(Entry).where(Entry.id.in_(set(entry_ids)))
|
||||
statement = (
|
||||
statement.outerjoin(Entry.text_fields)
|
||||
.outerjoin(Entry.datetime_fields)
|
||||
.outerjoin(Entry.tags)
|
||||
)
|
||||
statement = statement.options(
|
||||
selectinload(Entry.text_fields),
|
||||
selectinload(Entry.datetime_fields),
|
||||
selectinload(Entry.tags).options(
|
||||
selectinload(Tag.aliases),
|
||||
selectinload(Tag.subtags),
|
||||
),
|
||||
)
|
||||
statement = statement.distinct()
|
||||
|
||||
entries = session.execute(statement).scalars()
|
||||
entries = entries.unique()
|
||||
|
||||
for entry in entries:
|
||||
yield entry
|
||||
session.expunge(entry)
|
||||
|
||||
@property
|
||||
def entries_count(self) -> int:
|
||||
with Session(self.engine) as session:
|
||||
|
||||
@@ -1116,8 +1116,7 @@ class QtDriver(DriverMixin, QObject):
|
||||
|
||||
is_grid_thumb = True
|
||||
logger.info("[QtDriver] Loading Entries...")
|
||||
# TODO: Grab all entries at once
|
||||
entries: list[Entry] = [self.lib.get_entry_full(e_id) for e_id in self.frame_content]
|
||||
entries: list[Entry] = list(self.lib.get_entries_full(self.frame_content))
|
||||
logger.info("[QtDriver] Building Filenames...")
|
||||
filenames: list[Path] = [self.lib.library_dir / e.path for e in entries]
|
||||
logger.info("[QtDriver] Done! Processing ItemThumbs...")
|
||||
|
||||
@@ -108,7 +108,6 @@ class FieldContainers(QWidget):
|
||||
|
||||
def update_from_entry(self, entry: Entry):
|
||||
"""Update tags and fields from a single Entry source."""
|
||||
self.cached_entries = [self.lib.get_entry_full(entry.id)]
|
||||
logger.info(
|
||||
"[FieldContainers] Updating Selection",
|
||||
path=entry.path,
|
||||
@@ -116,6 +115,7 @@ class FieldContainers(QWidget):
|
||||
tags=entry.tags,
|
||||
)
|
||||
|
||||
self.cached_entries = [self.lib.get_entry_full(entry.id)]
|
||||
entry_ = self.cached_entries[0]
|
||||
container_len: int = len(entry_.fields)
|
||||
container_index = 0
|
||||
|
||||
@@ -143,7 +143,7 @@ class PreviewPanel(QWidget):
|
||||
|
||||
# One Item Selected
|
||||
elif len(self.driver.selected) == 1:
|
||||
entry: Entry = self.lib.get_entry_full(self.driver.selected[0])
|
||||
entry: Entry = self.lib.get_entry(self.driver.selected[0])
|
||||
filepath: Path = self.lib.library_dir / entry.path
|
||||
ext: str = filepath.suffix.lower()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user