Skip to content

Commit

Permalink
Add indexes to important columns
Browse files Browse the repository at this point in the history
* This is to make searches faster in the dashboard, now that we have a lot of rows
  • Loading branch information
Wason1797 committed Jan 20, 2025
1 parent 198cfc6 commit 3439af2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
6 changes: 3 additions & 3 deletions server/app/repositories/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ class StationData(BaseDBConnector.Base):
bme688_data_id: Mapped[int] = mapped_column(INTEGER(), ForeignKey("bme688_data.id"), nullable=True)

# Id of the individual sensor station submitting the data
station_id: Mapped[int] = mapped_column(INTEGER(), ForeignKey("stations.id"), nullable=False)
station_id: Mapped[int] = mapped_column(INTEGER(), ForeignKey("stations.id"), nullable=False, index=True)
# Id of the border router forwarding the data
border_router_id: Mapped[int] = mapped_column(INTEGER(), ForeignKey("border_routers.id"), nullable=True)
border_router_id: Mapped[int] = mapped_column(INTEGER(), ForeignKey("border_routers.id"), nullable=True, index=True)

timestamp: Mapped[str] = mapped_column(VARCHAR(20), nullable=False)
timestamp: Mapped[str] = mapped_column(VARCHAR(20), nullable=False, index=True)

scd41_data: Mapped[SCD41Data] = relationship("SCD41Data", lazy="joined")
ens160_data: Mapped[ENS160Data] = relationship("ENS160Data", lazy="joined")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""Add indexes to important columns
Revision ID: 8d2149a11367
Revises: 74b685228bd5
Create Date: 2025-01-20 17:31:16.781760
"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = "8d2149a11367"
down_revision: Union[str, None] = "74b685228bd5"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
with op.get_context().autocommit_block():
op.create_index(
op.f("ix_station_data_border_router_id"), "station_data", ["border_router_id"], unique=False, postgresql_concurrently=True
)
op.create_index(op.f("ix_station_data_station_id"), "station_data", ["station_id"], unique=False, postgresql_concurrently=True)
op.create_index(op.f("ix_station_data_timestamp"), "station_data", ["timestamp"], unique=False, postgresql_concurrently=True)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f("ix_station_data_timestamp"), table_name="station_data")
op.drop_index(op.f("ix_station_data_station_id"), table_name="station_data")
op.drop_index(op.f("ix_station_data_border_router_id"), table_name="station_data")
# ### end Alembic commands ###

0 comments on commit 3439af2

Please sign in to comment.