In the presentation Replace Your ORM With Relational Algebra, Christoffer Ekeroth humorously explores the idea of substituting Object Relational Mappers (ORMs) with a Domain-Specific Language (DSL) rooted in relational algebra. He outlines the limitations of ORMs, such as their failure to fully utilize SQL’s features, and presents relational algebra as a flexible alternative for database queries that allows for composability and string manipulation without the drawbacks of traditional SQL string-building methods. Christoffer discusses the foundational principles of relational algebra, including operations like selection and projection, while emphasizing its potential for efficient query structuring and optimization. He showcases a proof of concept by retrieving movies from IMDb using Python, illustrates the organic process of constructing queries, and engages the audience in discussions about the trade-offs between language complexity and expressive power. The presentation culminates in a dialogue about developing tailored DSLs for specific programming challenges, highlighting the community’s role in fostering innovative development solutions.
Replace Your ORM With Relational Algebra: A comprehensive overview
The problem with ORMs
Christoffer begins by highlighting the limitations of ORMs. While they offer composability and abstraction, they often fail to leverage the full power of database systems, limiting developers to a constrained subset of SQL functionality. Additionally, ORMs introduce complexity and inefficiencies when dealing with vendor-specific database optimizations. The alternative? A domain-specific language (DSL) built on relational algebra.
What is relational algebra?
Relational algebra serves as the theoretical foundation for query processing. Unlike SQL, which includes additional conveniences like aggregation and outer joins, relational algebra strictly defines operations like selection (filtering rows), projection (selecting columns), and joins (combining tables). By structuring queries as data rather than opaque SQL strings, developers gain composability and predictability.
Building queries with a DSL
Christoffer demonstrates how a DSL can act as a middle ground between raw SQL and ORMs. Using Python, he builds a proof of concept that retrieves movies directed by Ingmar Bergman from the IMDb top 250. His approach showcases how queries can be expressed naturally while preserving the efficiency benefits of relational algebra. By modeling queries as data structures, developers can manipulate and optimize them programmatically, reducing errors and improving maintainability.
Query optimization through algebraic properties
A key advantage of relational algebra lies in its mathematical properties, such as commutativity and associativity, which allow for powerful query optimizations. Christoffer illustrates this with a practical example of joining movies and genres, avoiding inefficient cross joins by applying logical filtering. These algebraic properties not only enhance performance but also enable query planning that adapts to different execution environments.
Moving beyond ORMs
While ORMs simplify database interactions, they introduce an additional layer of abstraction that isn’t always necessary. Christoffer suggests that developers rethink their approach to querying, exploring DSLs and in-memory interpreters as viable alternatives. He contrasts his method with SQLAlchemy, describing the latter as a hybrid between ORM and raw SQL that, while flexible, still carries ORM-related overhead.
The future of functional querying
As the presentation concludes, Christoffer discusses the broader implications of relational algebra-based DSLs. The rise of specialized programming languages and DSLs suggests a shift away from one-size-fits-all solutions like ORMs. Instead, developers may begin crafting lightweight, domain-specific languages tailored to their needs, balancing expressiveness with efficiency.
Final thoughts
Christoffer ’s talk challenges developers to reconsider their reliance on ORMs, advocating for a return to relational algebra as a foundational query language. While ORMs will likely remain a key tool for many applications, exploring alternative query representation methods could lead to more efficient, maintainable, and scalable database interactions. Whether you’re a database enthusiast or a functional programming advocate, this thought-provoking discussion opens new possibilities for data access and manipulation.