Spaces:
Running
Running
B tree indexing
Browse files
py_backend/alembic/versions/0022_add_filtering_indexes.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""add_filtering_indexes
|
| 2 |
+
|
| 3 |
+
Revision ID: 0022
|
| 4 |
+
Revises: 0021
|
| 5 |
+
Create Date: 2024-12-19 12:00:00.000000
|
| 6 |
+
|
| 7 |
+
"""
|
| 8 |
+
from alembic import op
|
| 9 |
+
import sqlalchemy as sa
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# revision identifiers, used by Alembic.
|
| 13 |
+
revision = '0022'
|
| 14 |
+
down_revision = '0021'
|
| 15 |
+
branch_labels = None
|
| 16 |
+
depends_on = None
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def upgrade():
|
| 20 |
+
# Indexes for images table - commonly filtered columns
|
| 21 |
+
# Note: ix_images_captured_at already exists from migration 0016, skip it here
|
| 22 |
+
op.execute("CREATE INDEX IF NOT EXISTS ix_images_source ON images(source)")
|
| 23 |
+
op.execute("CREATE INDEX IF NOT EXISTS ix_images_event_type ON images(event_type)")
|
| 24 |
+
op.execute("CREATE INDEX IF NOT EXISTS ix_images_image_type ON images(image_type)")
|
| 25 |
+
|
| 26 |
+
# Indexes for captions table - filtered and ordered columns
|
| 27 |
+
op.execute("CREATE INDEX IF NOT EXISTS ix_captions_starred ON captions(starred)")
|
| 28 |
+
op.execute("CREATE INDEX IF NOT EXISTS ix_captions_created_at ON captions(created_at)")
|
| 29 |
+
|
| 30 |
+
# Indexes for join tables - improve join performance
|
| 31 |
+
op.execute("CREATE INDEX IF NOT EXISTS ix_image_countries_image_id ON image_countries(image_id)")
|
| 32 |
+
op.execute("CREATE INDEX IF NOT EXISTS ix_image_countries_c_code ON image_countries(c_code)")
|
| 33 |
+
|
| 34 |
+
# Index on Country.r_code for region filtering
|
| 35 |
+
op.execute("CREATE INDEX IF NOT EXISTS ix_countries_r_code ON countries(r_code)")
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def downgrade():
|
| 39 |
+
op.drop_index('ix_countries_r_code', table_name='countries', if_exists=True)
|
| 40 |
+
op.drop_index('ix_image_countries_c_code', table_name='image_countries', if_exists=True)
|
| 41 |
+
op.drop_index('ix_image_countries_image_id', table_name='image_countries', if_exists=True)
|
| 42 |
+
op.drop_index('ix_captions_created_at', table_name='captions', if_exists=True)
|
| 43 |
+
op.drop_index('ix_captions_starred', table_name='captions', if_exists=True)
|
| 44 |
+
op.drop_index('ix_images_image_type', table_name='images', if_exists=True)
|
| 45 |
+
op.drop_index('ix_images_event_type', table_name='images', if_exists=True)
|
| 46 |
+
op.drop_index('ix_images_source', table_name='images', if_exists=True)
|
| 47 |
+
|
py_backend/app/models.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
from sqlalchemy import (
|
| 2 |
Column, String, DateTime, SmallInteger, Table, ForeignKey, Boolean,
|
| 3 |
-
CheckConstraint, UniqueConstraint, Text, Integer
|
| 4 |
)
|
| 5 |
from sqlalchemy.dialects.postgresql import UUID, TIMESTAMP, CHAR, JSONB
|
| 6 |
from sqlalchemy.orm import relationship
|
|
@@ -22,6 +22,8 @@ image_countries = Table(
|
|
| 22 |
ForeignKey("countries.c_code"),
|
| 23 |
primary_key=True,
|
| 24 |
),
|
|
|
|
|
|
|
| 25 |
)
|
| 26 |
|
| 27 |
images_captions = Table(
|
|
@@ -121,6 +123,10 @@ class Images(Base):
|
|
| 121 |
CheckConstraint('pitch_deg IS NULL OR (pitch_deg BETWEEN -90 AND 90)', name='chk_images_pitch_deg'),
|
| 122 |
CheckConstraint('yaw_deg IS NULL OR (yaw_deg BETWEEN -180 AND 180)', name='chk_images_yaw_deg'),
|
| 123 |
CheckConstraint('roll_deg IS NULL OR (roll_deg BETWEEN -180 AND 180)', name='chk_images_roll_deg'),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
)
|
| 125 |
|
| 126 |
image_id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
|
@@ -159,6 +165,8 @@ class Captions(Base):
|
|
| 159 |
CheckConstraint('accuracy IS NULL OR (accuracy BETWEEN 0 AND 100)', name='chk_captions_accuracy'),
|
| 160 |
CheckConstraint('context IS NULL OR (context BETWEEN 0 AND 100)', name='chk_captions_context'),
|
| 161 |
CheckConstraint('usability IS NULL OR (usability BETWEEN 0 AND 100)', name='chk_captions_usability'),
|
|
|
|
|
|
|
| 162 |
)
|
| 163 |
|
| 164 |
caption_id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
|
|
|
| 1 |
from sqlalchemy import (
|
| 2 |
Column, String, DateTime, SmallInteger, Table, ForeignKey, Boolean,
|
| 3 |
+
CheckConstraint, UniqueConstraint, Text, Integer, Index
|
| 4 |
)
|
| 5 |
from sqlalchemy.dialects.postgresql import UUID, TIMESTAMP, CHAR, JSONB
|
| 6 |
from sqlalchemy.orm import relationship
|
|
|
|
| 22 |
ForeignKey("countries.c_code"),
|
| 23 |
primary_key=True,
|
| 24 |
),
|
| 25 |
+
Index('ix_image_countries_image_id', 'image_id'),
|
| 26 |
+
Index('ix_image_countries_c_code', 'c_code'),
|
| 27 |
)
|
| 28 |
|
| 29 |
images_captions = Table(
|
|
|
|
| 123 |
CheckConstraint('pitch_deg IS NULL OR (pitch_deg BETWEEN -90 AND 90)', name='chk_images_pitch_deg'),
|
| 124 |
CheckConstraint('yaw_deg IS NULL OR (yaw_deg BETWEEN -180 AND 180)', name='chk_images_yaw_deg'),
|
| 125 |
CheckConstraint('roll_deg IS NULL OR (roll_deg BETWEEN -180 AND 180)', name='chk_images_roll_deg'),
|
| 126 |
+
Index('ix_images_source', 'source'),
|
| 127 |
+
Index('ix_images_event_type', 'event_type'),
|
| 128 |
+
Index('ix_images_image_type', 'image_type'),
|
| 129 |
+
Index('ix_images_captured_at', 'captured_at'),
|
| 130 |
)
|
| 131 |
|
| 132 |
image_id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
|
|
|
| 165 |
CheckConstraint('accuracy IS NULL OR (accuracy BETWEEN 0 AND 100)', name='chk_captions_accuracy'),
|
| 166 |
CheckConstraint('context IS NULL OR (context BETWEEN 0 AND 100)', name='chk_captions_context'),
|
| 167 |
CheckConstraint('usability IS NULL OR (usability BETWEEN 0 AND 100)', name='chk_captions_usability'),
|
| 168 |
+
Index('ix_captions_starred', 'starred'),
|
| 169 |
+
Index('ix_captions_created_at', 'created_at'),
|
| 170 |
)
|
| 171 |
|
| 172 |
caption_id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|