Docs
Buscar⌘ K
  • Início
  • Sobre o The Graph
  • Redes Apoiadas
  • Contratos de Protocolo
  • Subgraphs
    • Substreams
      • Token API
        • AI Suite
          • Indexação
            • Recursos
              Subgraphs > Boas práticas

              3 minutos

              Boas Práticas de Subgraph 2 - Melhorar a Indexação e a Capacidade de Resposta de Queries com @derivedFrom

              TLDR

              Arrays in your schema can really slow down a Subgraph’s performance as they grow beyond thousands of entries. If possible, the @derivedFrom directive should be used when using arrays as it prevents large arrays from forming, simplifies handlers, and reduces the size of individual entities, improving indexing speed and query performance significantly.

              Como Usar a Diretiva @derivedFrom

              Você só precisa adicionar uma diretiva @derivedFrom após o seu arranjo no seu schema. Assim:

              1comments: [Comment!]! @derivedFrom(field: "post")

              @derivedFrom creates efficient one-to-many relationships, enabling an entity to dynamically associate with multiple related entities based on a field in the related entity. This approach removes the need for both sides of the relationship to store duplicate data, making the Subgraph more efficient.

              Exemplo de Caso de Uso para @derivedFrom

              Um exemplo de um arranjo que cresce dinamicamente é uma plataforma de blogs onde um “Post” pode ter vários “Comments” (comentários).

              Vamos começar com as nossas duas entidades, Post e Comment

              Sem otimização, seria possível implementá-la assim com um arranjo:

              1type Post @entity {2  id: Bytes!3  title: String!4  content: String!5  comments: [Comment!]!6}78type Comment @entity {9  id: Bytes!10  content: String!11}

              Arranjos como este, efetivamente, armazenarão dados extras de Comments no lado do Post no relacionamento.

              Aqui está uma versão otimizada que usa o @derivedFrom:

              1type Post @entity {2  id: Bytes!3  title: String!4  content: String!5  comments: [Comment!]! @derivedFrom(field: "post")6}78type Comment @entity {9  id: Bytes!10  content: String!11  post: Post!12}

              Ao adicionar a diretiva @derivedFrom, este schema só armazenará os “Comentários” no lado “Comments” do relacionamento, e não no lado “Post”. Os arranjos são armazenados em fileiras individuais, o que os faz crescer significativamente. Se o seu crescimento não for contido, isto pode permitir que o tamanho fique excessivamente grande.

              This will not only make our Subgraph more efficient, but it will also unlock three features:

              1. Podemos fazer um query sobre o Post e ver todos os seus comentários.

              2. Podemos fazer uma pesquisa reversa e um query sobre qualquer Comment, para ver de qual post ele vem.

              3. We can use Derived Field Loaders to unlock the ability to directly access and manipulate data from virtual relationships in our Subgraph mappings.

              Conclusão

              Use the @derivedFrom directive in Subgraphs to effectively manage dynamically growing arrays, enhancing indexing efficiency and data retrieval.

              Para aprender mais estratégias detalhadas sobre evitar arranjos grandes, leia este blog por Kevin Jones: Melhores Práticas no Desenvolvimento de Subgraphs: Como Evitar Grandes Arranjos.

              Melhores Práticas para um Subgraph 1 – 6

              1. Improve Query Speed with Subgraph Pruning

              2. Improve Indexing and Query Responsiveness by Using @derivedFrom

              3. Improve Indexing and Query Performance by Using Immutable Entities and Bytes as IDs

              4. Improve Indexing Speed by Avoiding eth_calls

              5. Simplify and Optimize with Timeseries and Aggregations

              6. Use Grafting for Quick Hotfix Deployment

              ⁠Editar no GitHub⁠

              Pruning with indexerHintsImmutable Entities and Bytes as IDs
              Nesta página
              • TLDR
              • Como Usar a Diretiva @derivedFrom
              • Exemplo de Caso de Uso para @derivedFrom
              • Conclusão
              • Melhores Práticas para um Subgraph 1 – 6
              The GraphStatusRede de TestesAtivos de MarcaFórumSegurançaPolítica de PrivacidadeAcordo de Serviço