Add index to a column that did not have an index when created #551
-
First Check
Commit to Help
Example Codefrom datetime import datetime
from typing import Dict, Optional
from sqlalchemy import BLOB, Column, JSON
from sqlmodel import Field, SQLModel, create_engine
# Table schema when table was created
class ExperimentConfig(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
variant_name: str
owner: str
description: bytes = Field(default=None, sa_column=Column(BLOB))
timestamp: datetime
# Updated schema when i found out many queries using timestamp and variant_name column as filters.
class ExperimentConfig(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
variant_name: str = Field(index=True)
owner: str
description: bytes = Field(default=None, sa_column=Column(BLOB))
timestamp: datetime = Field(index=True)
# Then I tried to run the following code with my local MySQL backend again
db_url = f"mysql+pymysql://user:password@localhost:3306/mydb"
engine = create_engine(db_url, echo=False, future=False)
SQLModel.metadata.create_all(engine)
# However, the index wasn't created at this point. I verified that the index did not exist through the following SQL query.
# `SHOW INDEX IN experimentconfig` DescriptionI have had a table that contains ~10K rows of data. Then i decided to add indexes to two columns in the table by simply updating From my perspective, addressing this question could benefit many users because it is very common that we add indexes later after analyzing a database read patterns. In addition, I am not sure this should be asked here or in SQLalchemy. If this is a wrong place to ask such a question, please let me know! Operating SystemmacOS Operating System DetailsIt is a macbook pro 2019 with intel x86_64 i7 processor. Thus I don't think my platform could be the issue. SQLModel Version0.0.8 Python VersionPython 3.10.9 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Following this stackoverflow answer, you are able to create a new index with;
|
Beta Was this translation helpful? Give feedback.
Following this stackoverflow answer, you are able to create a new index with;