Skip to content

Commit

Permalink
perf: Optimize zorg query joins
Browse files Browse the repository at this point in the history
  • Loading branch information
bbugyi200 committed Jul 22, 2024
1 parent f2b57fa commit f1b1587
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/zorg/storage/sql/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class H4(_HasTitleBase, table=True):
class ZorgFile(_Base, table=True):
"""Model class for zorg (*.zo) files."""

path: str
path: str = Field(index=True)
has_errors: bool = False
h1s: List[H1] = Relationship(back_populates="page")

Expand Down Expand Up @@ -195,7 +195,7 @@ class ZorgNote(_Base, table=True):
todo_status: Optional[NoteType] = None
# TODO(bugyi): Make this field NOT optional
block_id: Optional[int] = Field(foreign_key="block.id")
page_path: str = Field(sa_type=String)
page_path: str = Field(index=True)

# relationships
block: Block = Relationship(back_populates="notes")
Expand Down
12 changes: 2 additions & 10 deletions src/zorg/storage/sql/_query_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,10 @@

_BASE_SELECTOR: Final = (
select(sql.ZorgNote)
.join(sql.H1, cast(ColumnElement, sql.ZorgFile.h1s))
.outerjoin(sql.H2, cast(ColumnElement, sql.H1.h2s))
.outerjoin(sql.H3, cast(ColumnElement, sql.H2.h3s))
.outerjoin(sql.H4, cast(ColumnElement, sql.H3.h4s))
.join(
sql.Block,
cast(ColumnElement, sql.Block.h1_id == sql.H1.id)
| cast(ColumnElement, sql.Block.h2_id == sql.H2.id)
| cast(ColumnElement, sql.Block.h3_id == sql.H3.id)
| cast(ColumnElement, sql.Block.h4_id == sql.H4.id),
sql.ZorgFile,
cast(ColumnElement, sql.ZorgFile.path == sql.ZorgNote.page_path),
)
.join(sql.ZorgNote, cast(ColumnElement, sql.Block.notes))
.distinct()
)
_LOGGER: Final = Logger(__name__)
Expand Down

0 comments on commit f1b1587

Please sign in to comment.