diff --git a/backend/open_webui/models/chats.py b/backend/open_webui/models/chats.py index 0ac53a023..71e7b34bf 100644 --- a/backend/open_webui/models/chats.py +++ b/backend/open_webui/models/chats.py @@ -9,7 +9,7 @@ from open_webui.models.tags import TagModel, Tag, Tags from open_webui.env import SRC_LOG_LEVELS from pydantic import BaseModel, ConfigDict -from sqlalchemy import BigInteger, Boolean, Column, String, Text, JSON +from sqlalchemy import BigInteger, Boolean, Column, String, Text, JSON, Index from sqlalchemy import or_, func, select, and_, text from sqlalchemy.sql import exists @@ -39,6 +39,20 @@ class Chat(Base): meta = Column(JSON, server_default="{}") folder_id = Column(Text, nullable=True) + __table_args__ = ( + # Performance indexes for common queries + # WHERE folder_id = ... + Index('folder_id_idx', 'folder_id'), + # WHERE user_id = ... AND pinned = ... + Index('user_id_pinned_idx', 'user_id', 'pinned'), + # WHERE user_id = ... AND archived = ... + Index('user_id_archived_idx', 'user_id', 'archived'), + # WHERE user_id = ... ORDER BY updated_at DESC + Index('updated_at_user_id_idx', 'updated_at', 'user_id'), + # WHERE folder_id = ... AND user_id = ... + Index('folder_id_user_id_idx', 'folder_id', 'user_id'), + ) + class ChatModel(BaseModel): model_config = ConfigDict(from_attributes=True) diff --git a/backend/open_webui/models/functions.py b/backend/open_webui/models/functions.py index e98771fa0..af2311319 100644 --- a/backend/open_webui/models/functions.py +++ b/backend/open_webui/models/functions.py @@ -6,7 +6,7 @@ from open_webui.internal.db import Base, JSONField, get_db from open_webui.models.users import Users from open_webui.env import SRC_LOG_LEVELS from pydantic import BaseModel, ConfigDict -from sqlalchemy import BigInteger, Boolean, Column, String, Text +from sqlalchemy import BigInteger, Boolean, Column, String, Text, Index log = logging.getLogger(__name__) log.setLevel(SRC_LOG_LEVELS["MODELS"]) @@ -31,6 +31,10 @@ class Function(Base): updated_at = Column(BigInteger) created_at = Column(BigInteger) + __table_args__ = ( + Index('is_global_idx', 'is_global'), + ) + class FunctionMeta(BaseModel): description: Optional[str] = None diff --git a/backend/open_webui/models/tags.py b/backend/open_webui/models/tags.py index 279dc624d..46e412d81 100644 --- a/backend/open_webui/models/tags.py +++ b/backend/open_webui/models/tags.py @@ -8,7 +8,7 @@ from open_webui.internal.db import Base, get_db from open_webui.env import SRC_LOG_LEVELS from pydantic import BaseModel, ConfigDict -from sqlalchemy import BigInteger, Column, String, JSON, PrimaryKeyConstraint +from sqlalchemy import BigInteger, Column, String, JSON, PrimaryKeyConstraint, Index log = logging.getLogger(__name__) log.setLevel(SRC_LOG_LEVELS["MODELS"]) @@ -24,8 +24,9 @@ class Tag(Base): user_id = Column(String) meta = Column(JSON, nullable=True) - # Unique constraint ensuring (id, user_id) is unique, not just the `id` column - __table_args__ = (PrimaryKeyConstraint("id", "user_id", name="pk_id_user_id"),) + __table_args__ = ( + Index('user_id_idx', 'user_id'), + ) class TagModel(BaseModel):