Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance issues when updating #1850

Open
rafalnawojczyk opened this issue Nov 4, 2024 · 1 comment
Open

Performance issues when updating #1850

rafalnawojczyk opened this issue Nov 4, 2024 · 1 comment

Comments

@rafalnawojczyk
Copy link

I've encountered performance issue while updating a record in the database.

My model looks like that for Session:

export default class Session extends Model {
  static table = "sessions";

  static associations = {
    solves: { type: "has_many" as const, foreignKey: "session_id" },
  };

  @readonly @date("created_at") createdAt!: Date;

  @nochange @field("user_id") userId!: string;

  @date("used") used!: Date;
  @text("name") name!: string;
  @field("cube") cube!: CubeType;
  @field("amount") amount!: number;
  @field("full_amount") fullAmount!: number;
  @field("stdev") stdev!: number;
  @field("average") average!: number;
  @field("best") best!: number;
  @field("is_trainer") isTrainer!: boolean;
  @json("valid_times", jsonValue => jsonValue) validTimes!: number[];
  @field("threshold_1") threshold1!: number;
  @field("threshold_2") threshold2!: number;
  @field("threshold_3") threshold3!: number;
  @field("threshold_4") threshold4!: number;

  @children("solves") solves!: Query<Solve>;
}

and for solve:

export default class Solve extends Model {
  static table = "solves";
  static associations = {
    sessions: { type: "belongs_to" as const, key: "session_id" },
  };

  @field("time") time!: number;
  @field("scramble") scramble!: string;
  @text("note") note?: string;
  @field("flag") flag?: SolveFlagType;
  @field("inspection") inspection?: number;
  @field("star") star?: boolean;
  @field("is_trainer") isTrainer!: boolean;
  @field("training_alg") trainingAlg!: string;
  @date("created_at") createdAt!: number;
  @relation("sessions", "session_id") session!: Relation<Session>;
}

When I'm making an update on Session - it takes around 80ms, when there is only 5-6 solves in this session(one to many relation).
When there is around 600 solves that are related to the session - i takes around 500ms just to execute this empty update:

 await currentSession.update(session => {});

Is there anything wrong with the models that I've missed, and what is causing this?

@rafalnawojczyk
Copy link
Author

I've only partially resolved this problem by removing 'session' field from Solve. It sped up a lot the whole process.

Although, when data grows to 15k solves that are tied to specific session - it still takes a lot of time to update it(around 1.5s)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant