Paging Through a Parquet File in DuckDB: File_row_number or Offset?
Source Entity
Hacker News

DuckDB performance testing reveals that using file_row_number is significantly faster and more reliable than LIMIT/OFFSET for paging through large Parquet files. Relying on traditional OFFSET methods can lead to data integrity issues and slow query performance in high-volume API environments.
Optimizing Large-Scale Data Retrieval in DuckDB
When handling large-scale datasets, particularly those stored in Parquet format, developers often reach for standard SQL patterns like LIMIT and OFFSET to manage API response sizing. However, recent performance analysis indicates that this approach is suboptimal for modern data engineering workflows. By switching to file_row_number for paging, developers can achieve significant performance gains, with testing showing a 2.53x speed improvement on files containing 163 row groups.
The Performance Pitfalls of OFFSET
The primary issue with LIMIT/OFFSET lies in how databases process pagination. As the offset value increases, the database must scan and discard an ever-growing number of rows to reach the target data, leading to linear degradation in performance. In the context of large Parquet files, this overhead is exacerbated by the file's internal structure. DuckDB, optimized for analytical workloads, performs significantly better when utilizing metadata-aware identifiers like file_row_number rather than forcing the engine to perform expensive row-skipping operations.
Data Integrity and Reliability Concerns
Beyond mere speed, the reliance on OFFSET introduces a critical risk to data integrity. In distributed systems or environments where data might be subject to concurrent updates or complex file structures, OFFSET can return inconsistent results or incorrect rows without triggering explicit error messages. This 'silent failure' mode makes debugging exceptionally difficult, as the API may return a valid-looking response that contains duplicated or missing records, compromising the downstream application's logic.
Architectural Constraints and API Design
Modern cloud-native architectures impose strict limits on response sizes, such as the 6 MB threshold for synchronous AWS Lambda responses or the 32 MiB cap for Cloud Run HTTP/1 requests. These constraints force developers to implement robust pagination strategies. When the data source is a large Parquet file, the developer must balance these architectural ceilings with the need for low-latency retrieval. The transition to file_row_number addresses this by allowing for precise, deterministic data access that respects platform limitations while maximizing throughput.
Future Trends in Analytical Querying
As the industry moves toward more efficient data lake architectures, the ability to query 'in-place' using tools like DuckDB is becoming standard. The shift away from legacy SQL patterns toward more engine-specific optimizations—like leveraging row group metadata—represents a maturation in how we build data services. Developers should prioritize query patterns that are 'metadata-aware' rather than relying on generic, high-level SQL commands that may not align with the underlying storage format.
Concluding Insights
In summary, while LIMIT/OFFSET remains a familiar paradigm for web developers, it is ill-suited for high-performance analytical data retrieval from Parquet files. By adopting file_row_number, developers ensure both superior speed and, more importantly, the accuracy of the data being transmitted. For services handling millions of rows, this optimization is not merely a performance tweak but a fundamental requirement for building stable, reliable data APIs.