Skip to content

armgit / letta

Public Imported from GitHub
0

Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.

chore: index suggestions from planetscale suggestions (#6111)

Kian Jones78600fac23fa
diff --git a/alembic/versions/b1c2d3e4f5a6_drop_unused_and_redundant_indexes.py b/alembic/versions/b1c2d3e4f5a6_drop_unused_and_redundant_indexes.py
new file mode 100644
index 000000000..30d194a98
--- /dev/null
+++ b/alembic/versions/b1c2d3e4f5a6_drop_unused_and_redundant_indexes.py
@@ -0,0 +1,73 @@
+"""drop unused and redundant indexes
+
+Revision ID: b1c2d3e4f5a6
+Revises: a1b2c3d4e5f6
+Create Date: 2025-11-11 21:16:00.000000
+
+"""
+
+from typing import Sequence, Union
+
+import sqlalchemy as sa
+
+from alembic import op
+from letta.settings import settings
+
+# revision identifiers, used by Alembic.
+revision: str = "b1c2d3e4f5a6"
+down_revision: Union[str, None] = "a1b2c3d4e5f6"
+branch_labels: Union[str, Sequence[str], None] = None
+depends_on: Union[str, Sequence[str], None] = None
+
+
+def upgrade() -> None:
+ # Skip this migration for SQLite
+ if not settings.letta_pg_uri_no_default:
+ return
+
+ # Drop unused indexes
+ op.drop_index("ix_passage_tags_archive_tag", table_name="passage_tags", if_exists=True)
+ op.drop_index("ix_jobs_created_at", table_name="jobs", if_exists=True)
+ op.drop_index("ix_block_project_id", table_name="block", if_exists=True)
+ op.drop_index("ix_block_label", table_name="block", if_exists=True)
+
+ # Drop redundant indexes (covered by other composite indexes or FKs)
+ op.drop_index("ix_messages_run_id", table_name="messages", if_exists=True) # Redundant with ix_messages_run_sequence
+ op.drop_index("ix_files_agents_agent_id", table_name="files_agents", if_exists=True) # Redundant with FK index
+ op.drop_index(
+ "ix_agents_organization_id", table_name="agents", if_exists=True
+ ) # Redundant with ix_agents_organization_id_deployment_id
+ op.drop_index(
+ "ix_passage_tags_archive_id", table_name="passage_tags", if_exists=True
+ ) # Redundant with ix_passage_tags_archive_tag and ix_passage_tags_org_archive
+ op.drop_index(
+ "ix_blocks_block_label", table_name="blocks_agents", if_exists=True
+ ) # Redundant with ix_blocks_agents_block_label_agent_id
+ op.drop_index("ix_block_organization_id", table_name="block", if_exists=True) # Redundant with ix_block_org_project_template
+ op.drop_index(
+ "archival_passages_org_idx", table_name="archival_passages", if_exists=True
+ ) # Redundant with ix_archival_passages_org_archive
+
+ # Drop unused table (leftover from PlanetScale migration)
+ op.drop_table("_planetscale_import", if_exists=True)
+
+
+def downgrade() -> None:
+ # Skip this migration for SQLite
+ if not settings.letta_pg_uri_no_default:
+ return
+
+ # Re-create indexes in reverse order
+ op.create_index("archival_passages_org_idx", "archival_passages", ["organization_id"], unique=False, if_not_exists=True)
+ op.create_index("ix_block_organization_id", "block", ["organization_id"], unique=False, if_not_exists=True)
+ op.create_index("ix_blocks_block_label", "blocks_agents", ["block_label"], unique=False, if_not_exists=True)
+ op.create_index("ix_passage_tags_archive_id", "passage_tags", ["archive_id"], unique=False, if_not_exists=True)
+ op.create_index("ix_agents_organization_id", "agents", ["organization_id"], unique=False, if_not_exists=True)
+ op.create_index("ix_files_agents_agent_id", "files_agents", ["agent_id"], unique=False, if_not_exists=True)
+ op.create_index("ix_messages_run_id", "messages", ["run_id"], unique=False, if_not_exists=True)
+ op.create_index("ix_block_label", "block", ["label"], unique=False, if_not_exists=True)
+ op.create_index("ix_block_project_id", "block", ["project_id"], unique=False, if_not_exists=True)
+ op.create_index("ix_jobs_created_at", "jobs", ["created_at", "id"], unique=False, if_not_exists=True)
+ op.create_index("ix_passage_tags_archive_tag", "passage_tags", ["archive_id", "tag"], unique=False, if_not_exists=True)
+
+ # Note: Not recreating _planetscale_import table in downgrade as it's application-specific
diff --git a/letta/orm/agent.py b/letta/orm/agent.py
index 01b3cbcaa..83690c75a 100644
--- a/letta/orm/agent.py
+++ b/letta/orm/agent.py
@@ -39,7 +39,6 @@ class Agent(SqlalchemyBase, OrganizationMixin, ProjectMixin, TemplateEntityMixin
__pydantic_model__ = PydanticAgentState
__table_args__ = (
Index("ix_agents_created_at", "created_at", "id"),
- Index("ix_agents_organization_id", "organization_id"),
Index("ix_agents_organization_id_deployment_id", "organization_id", "deployment_id"),
Index("ix_agents_project_id", "project_id"),
)
diff --git a/letta/orm/block.py b/letta/orm/block.py
index 4ae5e5763..6ac486556 100644
--- a/letta/orm/block.py
+++ b/letta/orm/block.py
@@ -24,9 +24,6 @@ class Block(OrganizationMixin, SqlalchemyBase, ProjectMixin, TemplateEntityMixin
__table_args__ = (
UniqueConstraint("id", "label", name="unique_block_id_label"),
Index("created_at_label_idx", "created_at", "label"),
- Index("ix_block_label", "label"),
- Index("ix_block_organization_id", "organization_id"),
- Index("ix_block_project_id", "project_id"),
Index("ix_block_is_template", "is_template"),
Index("ix_block_hidden", "hidden"),
Index("ix_block_org_project_template", "organization_id", "project_id", "is_template"),
diff --git a/letta/orm/blocks_agents.py b/letta/orm/blocks_agents.py
index 6c9374282..f55ac5484 100644
--- a/letta/orm/blocks_agents.py
+++ b/letta/orm/blocks_agents.py
@@ -25,7 +25,6 @@ class BlocksAgents(Base):
),
UniqueConstraint("agent_id", "block_id", name="unique_agent_block"),
Index("ix_blocks_agents_block_label_agent_id", "block_label", "agent_id"),
- Index("ix_blocks_block_label", "block_label"),
Index("ix_blocks_agents_block_id", "block_id"),
)
diff --git a/letta/orm/files_agents.py b/letta/orm/files_agents.py
index 0202b34f5..1c768711d 100644
--- a/letta/orm/files_agents.py
+++ b/letta/orm/files_agents.py
@@ -32,8 +32,6 @@ class FileAgent(SqlalchemyBase, OrganizationMixin):
# helpful indexes for look-ups
Index("ix_file_agent", "file_id", "agent_id"),
Index("ix_agent_filename", "agent_id", "file_name"),
- # improve lookups by agent alone (e.g., WHERE agent_id IN (...))
- Index("ix_files_agents_agent_id", "agent_id"),
)
__pydantic_model__ = PydanticFileAgent
diff --git a/letta/orm/job.py b/letta/orm/job.py
index 9d46a01a3..f8b15aee8 100644
--- a/letta/orm/job.py
+++ b/letta/orm/job.py
@@ -23,10 +23,7 @@ class Job(SqlalchemyBase, UserMixin):
__tablename__ = "jobs"
__pydantic_model__ = PydanticJob
- __table_args__ = (
- Index("ix_jobs_created_at", "created_at", "id"),
- Index("ix_jobs_user_id", "user_id"),
- )
+ __table_args__ = (Index("ix_jobs_user_id", "user_id"),)
status: Mapped[JobStatus] = mapped_column(String, default=JobStatus.created, doc="The current status of the job.")
completed_at: Mapped[Optional[datetime]] = mapped_column(nullable=True, doc="The unix timestamp of when the job was completed.")
diff --git a/letta/orm/message.py b/letta/orm/message.py
index d98085478..3a6072893 100644
--- a/letta/orm/message.py
+++ b/letta/orm/message.py
@@ -23,7 +23,6 @@ class Message(SqlalchemyBase, OrganizationMixin, AgentMixin):
Index("ix_messages_created_at", "created_at", "id"),
Index("ix_messages_agent_sequence", "agent_id", "sequence_id"),
Index("ix_messages_org_agent", "organization_id", "agent_id"),
- Index("ix_messages_run_id", "run_id"),
# Composite index for optimizing the frequently-run query:
Index("ix_messages_run_sequence", "run_id", "sequence_id"),
)
diff --git a/letta/orm/passage.py b/letta/orm/passage.py
index cf17bc835..caca2764e 100644
--- a/letta/orm/passage.py
+++ b/letta/orm/passage.py
@@ -90,7 +90,6 @@ class ArchivalPassage(BasePassage, ArchiveMixin):
def __table_args__(cls):
if settings.database_engine is DatabaseChoice.POSTGRES:
return (
- Index("archival_passages_org_idx", "organization_id"),
Index("ix_archival_passages_org_archive", "organization_id", "archive_id"),
Index("archival_passages_created_at_id_idx", "created_at", "id"),
Index("ix_archival_passages_archive_id", "archive_id"),
diff --git a/letta/orm/passage_tag.py b/letta/orm/passage_tag.py
index 45f24f0af..debbd650b 100644
--- a/letta/orm/passage_tag.py
+++ b/letta/orm/passage_tag.py
@@ -25,9 +25,7 @@ class PassageTag(SqlalchemyBase, OrganizationMixin):
# ensure uniqueness of tag per passage
UniqueConstraint("passage_id", "tag", name="uq_passage_tag"),
# indexes for efficient queries
- Index("ix_passage_tags_archive_id", "archive_id"),
Index("ix_passage_tags_tag", "tag"),
- Index("ix_passage_tags_archive_tag", "archive_id", "tag"),
Index("ix_passage_tags_org_archive", "organization_id", "archive_id"),
)