Skip to content

Add database indexes to commonly queried entity fields #216

@sameh-farouk

Description

@sameh-farouk

Split from #134. The processor DB has no indexes beyond auto-generated FK indexes from the Init migration. Adding @index directives in schema.graphql for commonly filtered/ordered fields would improve query performance with minimal effort.

Candidate fields

type Node @entity {
  nodeID: Int! @index
  farmID: Int! @index
  twinID: Int! @index
  # ...
}

type Farm @entity {
  farmID: Int! @index
  twinID: Int! @index
  # ...
}

type Twin @entity {
  twinID: Int! @index
  accountID: String! @index
  # ...
}

type NodeContract @entity {
  contractID: BigInt! @index
  twinID: Int! @index
  nodeID: Int! @index
  state: ContractState! @index
  # ...
}

type NameContract @entity {
  contractID: BigInt! @index
  twinID: Int! @index
  state: ContractState! @index
  # ...
}

type RentContract @entity {
  contractID: BigInt! @index
  twinID: Int! @index
  nodeID: Int! @index
  state: ContractState! @index
  # ...
}

type PublicIp @entity {
  ip: String! @index
  contractId: BigInt! @index
  # ...
}

type ContractBillReport @entity {
  contractID: BigInt! @index
  # ...
}

type NruConsumption @entity {
  contractID: BigInt! @index
  # ...
}

type UptimeEvent @entity {
  nodeID: Int! @index
  # ...
}

Steps

  1. Add @index directives to schema.graphql
  2. yarn codegen — regenerates src/model/ with @Index_() TypeORM decorators
  3. yarn build
  4. yarn db:create-migration — generates a migration with CREATE INDEX statements
  5. No processor reindex needed — indexes are added to existing data

Notes

  • Subsquid codegen v0.3.3 supports @index (single field) and @index(fields: [...], unique: true) (composite/unique) on OBJECT | FIELD_DEFINITION
  • Compatible with the typegen automation (PR feat: automate type generation with append-only version log #207) — codegen touches src/model/, typegen touches src/types/, no overlap
  • The candidate list above should be validated against actual query patterns from grid-proxy and dashboard before finalizing

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions