Free 30-min Web3 Consultation
Book Consultation
Smart Contract Security Audits
View Audit Services
Custom DeFi Protocol Development
Explore DeFi
Full-Stack Web3 dApp Development
View App Services
Free 30-min Web3 Consultation
Book Consultation
Smart Contract Security Audits
View Audit Services
Custom DeFi Protocol Development
Explore DeFi
Full-Stack Web3 dApp Development
View App Services
Free 30-min Web3 Consultation
Book Consultation
Smart Contract Security Audits
View Audit Services
Custom DeFi Protocol Development
Explore DeFi
Full-Stack Web3 dApp Development
View App Services
Free 30-min Web3 Consultation
Book Consultation
Smart Contract Security Audits
View Audit Services
Custom DeFi Protocol Development
Explore DeFi
Full-Stack Web3 dApp Development
View App Services
LABS
Comparisons

Subgraph Schema Design: Entity-Relationship vs Flat Structure

A technical comparison of normalized (Entity-Relationship) and denormalized (Flat) schema designs for The Graph subgraphs. Analyzes trade-offs in query performance, indexing speed, developer experience, and hosting costs for protocol architects and engineering leads.
Chainscore © 2026
introduction
THE ANALYSIS

Introduction: The Core Schema Decision for Subgraph Performance

Choosing between Entity-Relationship and Flat schema designs fundamentally dictates your subgraph's query performance, indexing speed, and long-term maintainability.

Entity-Relationship (ER) Schema excels at modeling complex, interconnected on-chain data because it mirrors relational database principles. This structure allows for powerful GraphQL queries with nested relationships, enabling efficient data retrieval for applications like NFT marketplaces (e.g., querying a collection, its tokens, and owner histories in one request). However, this power comes at a cost: indexing can be slower due to multiple entity lookups, and query performance may degrade with deeply nested relationships on high-throughput chains like Arbitrum or Polygon.

Flat Schema takes a different approach by denormalizing data into fewer, wider entities. This strategy results in significantly faster indexing speeds—often 20-40% quicker for initial syncs—and simpler, more predictable queries. The trade-off is data duplication and potential update complexity; a change to a referenced piece of data (like a user's address) may require updates across multiple flat records, increasing write overhead and storage costs.

The key trade-off: If your priority is complex query flexibility and data integrity for a dApp like Uniswap or Aave, choose Entity-Relationship. If you prioritize raw indexing speed and simple queries for a high-volume analytics dashboard or a simple token tracker, choose Flat Structure. Your decision should be guided by your primary access patterns and whether you optimize for read complexity or write/initial sync performance.

tldr-summary
Entity-Relationship vs Flat Schema Design

TL;DR: Key Differentiators at a Glance

A quick scan of the core architectural trade-offs for designing Subgraph schemas on The Graph.

03

ER Trade-off: Complexity & Cost

Higher development and query complexity. Requires careful planning of @derivedFrom fields and relationships. Can lead to more expensive GraphQL queries (deeper nesting = more compute) and potentially slower indexing if relationships are not optimized.

04

Flat Trade-off: Data Redundancy

Potential for data duplication and update anomalies. If a piece of data (like a token.name) changes, every record containing it must be updated. This denormalization simplifies reads at the cost of more complex, state-aware event handling in your mapping logic.

HEAD-TO-HEAD COMPARISON

Entity-Relationship vs Flat Schema Design

Direct comparison of Subgraph schema design patterns for blockchain indexing.

Metric / FeatureEntity-Relationship (ER) SchemaFlat Schema

Complex Data Modeling

Query Performance (Nested Data)

Optimized (1 Query)

Multiple Queries Required

Schema Complexity

High (GraphQL relations)

Low (Simple fields)

Gas Event Handling

Efficient (Single handler)

Inefficient (Multiple handlers)

Use Case Fit

DeFi, NFTs, Social

Token Transfers, Simple Logs

Development Overhead

Higher initial setup

Lower initial setup

Example Protocols

Uniswap, Aave, Lido

ERC-20 Transfers, WETH

pros-cons-a
Subgraph Schema Design

Entity-Relationship Schema: Pros and Cons

Choosing between a normalized Entity-Relationship (ER) model and a denormalized Flat Structure is a foundational decision. This comparison highlights the key trade-offs for indexing performance, developer experience, and query complexity.

01

ER Schema: Query Flexibility

Normalized data relationships: Enables complex, multi-hop queries (e.g., User -> Pools -> Swaps -> Tokens) without data duplication. This is critical for analytics dashboards like Dune Analytics or for protocols like Uniswap V3 that require deep relationship traversal. Queries are more expressive and maintainable.

02

ER Schema: Data Integrity

Single source of truth: Updates to a core entity (e.g., a Token's total supply) propagate automatically to all related records. This prevents inconsistencies, which is essential for accurate financial reporting in DeFi protocols like Aave or Compound. Schema changes are localized, reducing migration risk.

03

Flat Schema: Query Speed

Denormalized for performance: All required data for a common query pattern is stored in a single entity. This results in < 100ms p95 query latency for simple reads, as there are no expensive joins. Ideal for high-throughput applications like NFT marketplace activity feeds or token transfer histories.

04

Flat Schema: Simplicity & Cost

Reduced indexing overhead: Simpler mappings with fewer entities lead to ~30-50% faster initial sync times and lower hosting costs on services like The Graph's hosted service. The learning curve is lower for developers, making it a strong choice for MVPs or simple event-logging subgraphs.

05

ER Schema: Complexity Cost

Higher indexing latency: Complex relationships increase the time to index new blocks and can lead to > 2s p95 query times for deep traversals on large datasets. This requires careful optimization (e.g., derived fields) and more expensive infrastructure to maintain performance.

06

Flat Schema: Update Rigidity

Data duplication risks: If a piece of data (e.g., a user's ENS name) changes, you must update it in every flattened record. This makes schema migrations costly and error-prone. It's a poor fit for data that has many-to-many relationships or frequently updated attributes.

pros-cons-b
Subgraph Schema Design Comparison

Flat Schema: Pros and Cons

Choosing between Entity-Relationship (ER) and Flat schemas is a foundational decision impacting query performance, development speed, and data integrity. Here are the key trade-offs.

01

Flat Schema: Key Strength

Simpler Queries & Faster Development: No complex joins or nested relationships. Fetching all data for a transaction or user is a single, fast query. This matters for rapid prototyping and applications where read latency is critical, like dashboards or simple analytics.

02

Flat Schema: Key Weakness

Data Redundancy & Update Complexity: The same data (e.g., token name, pool address) is duplicated across many records. If a referenced entity changes, you must update every related record. This matters for data-heavy protocols like Uniswap v3 or Compound, where updates are frequent and storage costs scale poorly.

03

Entity-Relationship Schema: Key Strength

Data Integrity & Normalized Storage: Entities (e.g., User, Pool, Token) are defined once and referenced via ID. Updates are atomic and propagate automatically. This matters for complex DeFi applications (e.g., tracking user positions across multiple vaults) where consistency is non-negotiable.

04

Entity-Relationship Schema: Key Weakness

Complex Queries & Potential Performance Cost: Assembling a complete view requires GraphQL relationships (@derivedFrom) which can lead to the N+1 query problem. This matters for high-throughput applications requiring sub-second response times, as deep nested queries can become slow without careful indexing.

CHOOSE YOUR PRIORITY

When to Use Each Schema: A Decision Framework

Entity-Relationship Schema for DeFi

Verdict: The Standard Choice. Strengths: Models complex financial relationships natively. A Uniswap V3 Subgraph uses entities like Pool, Token, Position, and Swap with defined relationships (Pool.tokens, Position.pool). This enables efficient, multi-hop queries for analytics dashboards (e.g., "Show all positions for user X across pools containing token Y"). It's essential for protocols like Aave (Users, Reserves, Deposits) or Compound, where understanding nested relationships is core to the data model. Trade-off: Schema design is more upfront work, and deep nested queries can be slower if not indexed properly.

Flat Structure for DeFi

Verdict: Niche Use for High-Volume Logs. Strengths: Exceptional for high-throughput, event-sourced data where relationships are secondary. Ideal for indexing every Transfer event from an ERC-20 token like USDC into a single, massive TransferEvent entity with fields from, to, value, blockNumber. Queries filtering by a single address are blazing fast. Use for simple dashboards tracking raw volume or token holder counts. Trade-off: Calculating aggregate relationships (e.g., "net flow between two protocols") requires post-processing and is inefficient.

SUBGRAPH SCHEMA DESIGN

Technical Deep Dive: Performance and Cost Implications

Choosing between Entity-Relationship (ER) and Flat schemas fundamentally impacts query performance, indexing speed, and hosting costs. This analysis breaks down the trade-offs for protocol architects and data engineers.

Entity-Relationship (ER) schemas are typically faster for complex, multi-entity queries. By leveraging GraphQL's relationship traversal, you avoid expensive @derivedFrom field lookups in a flat structure. For example, querying a User with their Transactions and related Pools is a single, optimized query in an ER model. A flat schema would require multiple separate queries or complex joins, increasing latency significantly as data scales.

verdict
THE ANALYSIS

Final Verdict and Decision Framework

Choosing between Entity-Relationship and Flat Schema designs is a foundational decision that dictates your Subgraph's performance, maintainability, and scalability.

Entity-Relationship (ER) Schema excels at modeling complex, interconnected on-chain data because it mirrors relational database principles. This structure is ideal for protocols like Uniswap or Aave, where you must track relationships between User, Pool, Token, and Transaction entities. It enables powerful GraphQL queries with nested relationships (e.g., fetching a user's deposits across all pools) and enforces data integrity through defined relationships. However, this comes at a cost: query performance can degrade with deep nesting, and indexing times increase as the relationship graph grows, impacting sync speed for new chains.

Flat Schema takes a different approach by denormalizing data into fewer, wider entities. This strategy results in significantly faster read performance and simpler queries, as all necessary data for a common view (e.g., a dashboard showing pool statistics) is stored in a single entity. It's highly effective for high-throughput applications like NFT marketplaces tracking simple Transfer events, where Blast or Arbitrum might see 50+ TPS. The trade-off is data duplication and potential update complexity—if a referenced field (like a token's symbol) changes, you must update every related record, increasing write overhead.

The key trade-off is complexity for performance. If your priority is rich query flexibility, data integrity, and modeling intricate business logic (e.g., a DeFi analytics platform), choose the Entity-Relationship model. It future-proofs your subgraph for evolving protocol features. If you prioritize maximizing query speed, minimizing indexing latency, and serving simple, high-volume data (e.g., a real-time transaction feed or a leaderboard), choose the Flat Structure. For many projects, a pragmatic hybrid—using a flat structure for hot data paths and ER for core relationships—strikes the optimal balance.

ENQUIRY

Get In Touch
today.

Our experts will offer a free quote and a 30min call to discuss your project.

NDA Protected
24h Response
Directly to Engineering Team
10+
Protocols Shipped
$20M+
TVL Overall
NDA Protected Directly to Engineering Team