Query Engine
Overview
The query engine minimizes the number of queries issued to data sources. If a request can be satisfied with a single query, Monospace does not query the data source more than once.
Query Planning
When converting a Monospace query into raw database queries, the engine first produces a pessimistic plan. This initial plan prioritizes correctness over performance and makes no assumptions about data source capabilities — it works on any data source.
The pessimistic plan is never executed directly. Once the correct plan exists, a series of optimization passes run against it. The goal is the best-performing version of the plan while preserving correctness.
Optimizations
Optimizations fall into two categories.
Optimizations Based on Native Data Source Capabilities
If a data source supports executing multiple operations in a single query, the engine collapses them together. For example, if a SQL database supports UPDATE ... RETURNING, it is used instead of consecutive UPDATE + SELECT statements.
Optimizations Based on Other Operations in the Same Plan
If one or more operations can be made redundant by another operation in the same plan, the engine eliminates them. Examples:
- Consecutive
UPDATEstatements with identical filters are collapsed into one. SELECTstatements with identical filters and no intervening writes are merged into one.
Optimization passes repeat until no further improvements are possible. The result is a plan that is both correct and performant.
Execution
The optimized plan passes to the execution layer. Any data source queries with no inter-dependencies execute concurrently. Operations not natively supported by the data source, or that span multiple data sources, are performed in memory.