added basic indexing and a migration plan

This commit is contained in:
decent-engineer-decent-datascientist 2025-06-19 17:56:25 -05:00
parent 81b8267e85
commit 23159c2ab9
3 changed files with 24 additions and 5 deletions

View File

@ -9,7 +9,7 @@ from open_webui.models.tags import TagModel, Tag, Tags
from open_webui.env import SRC_LOG_LEVELS from open_webui.env import SRC_LOG_LEVELS
from pydantic import BaseModel, ConfigDict 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 import or_, func, select, and_, text
from sqlalchemy.sql import exists from sqlalchemy.sql import exists
@ -39,6 +39,20 @@ class Chat(Base):
meta = Column(JSON, server_default="{}") meta = Column(JSON, server_default="{}")
folder_id = Column(Text, nullable=True) 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): class ChatModel(BaseModel):
model_config = ConfigDict(from_attributes=True) model_config = ConfigDict(from_attributes=True)

View File

@ -6,7 +6,7 @@ from open_webui.internal.db import Base, JSONField, get_db
from open_webui.models.users import Users from open_webui.models.users import Users
from open_webui.env import SRC_LOG_LEVELS from open_webui.env import SRC_LOG_LEVELS
from pydantic import BaseModel, ConfigDict 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 = logging.getLogger(__name__)
log.setLevel(SRC_LOG_LEVELS["MODELS"]) log.setLevel(SRC_LOG_LEVELS["MODELS"])
@ -31,6 +31,10 @@ class Function(Base):
updated_at = Column(BigInteger) updated_at = Column(BigInteger)
created_at = Column(BigInteger) created_at = Column(BigInteger)
__table_args__ = (
Index('is_global_idx', 'is_global'),
)
class FunctionMeta(BaseModel): class FunctionMeta(BaseModel):
description: Optional[str] = None description: Optional[str] = None

View File

@ -8,7 +8,7 @@ from open_webui.internal.db import Base, get_db
from open_webui.env import SRC_LOG_LEVELS from open_webui.env import SRC_LOG_LEVELS
from pydantic import BaseModel, ConfigDict 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 = logging.getLogger(__name__)
log.setLevel(SRC_LOG_LEVELS["MODELS"]) log.setLevel(SRC_LOG_LEVELS["MODELS"])
@ -24,8 +24,9 @@ class Tag(Base):
user_id = Column(String) user_id = Column(String)
meta = Column(JSON, nullable=True) meta = Column(JSON, nullable=True)
# Unique constraint ensuring (id, user_id) is unique, not just the `id` column __table_args__ = (
__table_args__ = (PrimaryKeyConstraint("id", "user_id", name="pk_id_user_id"),) Index('user_id_idx', 'user_id'),
)
class TagModel(BaseModel): class TagModel(BaseModel):