Skip to content

Conversation

@racerand
Copy link

@racerand racerand commented Dec 2, 2025

Summary

This fixes #1239 by rewriting the sqlalchemy code that fetches non-hidden entries. Such that references to the main query are no longer used in the sub-query. This is done in 2 steps:

  1. A sub-query that selects all the entry ids with hidden tags by joining TagEntry and Tag.
  2. Select all entries where they have that id
    This is done by first querying all tags that are hidden and joining with the TagEntry binding table and selecting only the entry ids.

Previous generated SQL for simple query:

SELECT DISTINCT entries.id
FROM entries
WHERE NOT (EXISTS (SELECT 1 
    FROM tags, tag_entries 
    WHERE entries.id = tag_entries.entry_id AND tags.id = tag_entries.tag_id AND tags.is_hidden)) 
ORDER BY entries.id DESC

New SQL:

SELECT DISTINCT entries.id 
FROM entries 
WHERE NOT 
    (entries.id IN 
        (SELECT tag_entries.entry_id 
        FROM tags JOIN tag_entries 
        WHERE tags.is_hidden AND tags.id = tag_entries.tag_id)) 
ORDER BY entries.id DESC

Tasks Completed

  • Platforms Tested:
    • Windows x86
    • Windows ARM
    • macOS x86
    • macOS ARM
    • Linux x86
    • Linux ARM
  • Tested For:
    • Basic functionality
    • PyInstaller executable

Reused existing tests, as this should be a "do the same but faster"

@racerand racerand changed the title Fix #1239 SQL performance issue Fix #1239 non-hidden entry query performance issue Dec 2, 2025
@racerand
Copy link
Author

racerand commented Dec 2, 2025

After researching the problem, I've come to the conclusion, that while this works well for me, it's not generally true, and ensuring Indices exist for tag_entries might be a more reasonable approach.
Though using explicit joins over implicit ones should still be the preferred approach

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Doesn't load library

1 participant