Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
Index on transcript chunks by recording; recording count without ordering (D#441)
Murtaza Nasirec26a5f81922
diff --git a/src/api/recordings.py b/src/api/recordings.pyindex cc81ed3e..2db03876 100644--- a/src/api/recordings.py+++ b/src/api/recordings.py@@ -1746,8 +1746,9 @@ def get_recordings_paginated():# Default: sort by created_at (upload/processing date)stmt = stmt.order_by(Recording.created_at.desc())- # Get total count for pagination info- count_stmt = select(db.func.count()).select_from(stmt.subquery())+ # Get total count for pagination info; the order does not change the+ # count, so it is left out of the counting query (D#441)+ count_stmt = select(db.func.count()).select_from(stmt.order_by(None).subquery())total_count = db.session.execute(count_stmt).scalar()# Apply paginationdiff --git a/src/init_db.py b/src/init_db.pyindex 186f24d5..5fa6e5e2 100644--- a/src/init_db.py+++ b/src/init_db.py@@ -273,6 +273,9 @@ def _run_migrations(app, engine):if add_column_if_not_exists(engine, 'recording', 'upload_idempotency_key', 'VARCHAR(100)'):app.logger.info("Added upload_idempotency_key column to recording table")create_index_if_not_exists(engine, 'ix_recording_user_idem', 'recording', 'user_id, upload_idempotency_key')+ # Chunks are read and deleted by recording, in order (D#441)+ create_index_if_not_exists(engine, 'ix_transcript_chunk_recording', 'transcript_chunk',+ 'recording_id, chunk_index')if add_column_if_not_exists(engine, 'recording', 'processing_time_seconds', 'INTEGER'):app.logger.info("Added processing_time_seconds column to recording table")if add_column_if_not_exists(engine, 'recording', 'transcription_duration_seconds', 'INTEGER'):diff --git a/src/models/recording.py b/src/models/recording.pyindex 48eeeb97..1583c70a 100644--- a/src/models/recording.py+++ b/src/models/recording.py@@ -520,6 +520,8 @@ class TranscriptChunk(db.Model):# Composite index for efficient speaker name lookups scoped to user__table_args__ = (db.Index('idx_user_speaker_name', 'user_id', 'speaker_name'),+ # Chunks are read and deleted by recording, in order (D#441)+ db.Index('ix_transcript_chunk_recording', 'recording_id', 'chunk_index'),)# Relationshipsdiff --git a/tests/test_chunk_index.py b/tests/test_chunk_index.pynew file mode 100644index 00000000..7fc3db40--- /dev/null+++ b/tests/test_chunk_index.py@@ -0,0 +1,25 @@+"""Index on transcript chunks by recording (D#441).++Chunks are read and deleted by recording, in order, and recording_id had no+index, so every such query scanned the whole table.+"""++from sqlalchemy import inspect, text++from src.app import app, db+++def test_chunks_are_indexed_by_recording():+ with app.app_context():+ indexes = {i['name']: i['column_names'] for i in inspect(db.engine).get_indexes('transcript_chunk')}+ assert indexes.get('ix_transcript_chunk_recording') == ['recording_id', 'chunk_index']+++def test_chunk_lookup_by_recording_uses_the_index():+ with app.app_context():+ if db.engine.dialect.name != 'sqlite':+ return+ plan = db.session.execute(text(+ 'EXPLAIN QUERY PLAN SELECT * FROM transcript_chunk WHERE recording_id = 1 ORDER BY chunk_index'+ )).fetchall()+ assert any('ix_transcript_chunk_recording' in str(row) for row in plan)