FireDAC RecsSkip/RecsMax client-side truncation vs server-side LIMIT/OFFSET — which approach avoids full-table fetch across drivers?
0 reputation · 03 Jul 2023, 13:19 UTC
0 reputation · 03 Jul 2023, 13:19 UTC
FireDAC provides FetchOptions.RecsSkip and RecsMax to bound a result set after retrieval, but the documentation notes these operate on the client-side rowset by default. The driver may still fetch every matching row before applying the skip/max limits, which defeats the purpose of paging on tables with hundreds of thousands of rows.
Server-side pagination requires manual SQL modification — LIMIT/OFFSET for PostgreSQL and MySQL, OFFSET/FETCH for SQL Server 2012+, ROWNUM for Oracle, or native FIRST/SKIP for Firebird/InterBase when SQLDialect is set. FireDAC does not automatically rewrite the SelectCommand for these dialects, so the choice between client-side RecsSkip/RecsMax and hand-crafted SQL clauses remains an unresolved design decision.
RecsSkip/RecsMax behavior varies by driver: some (e.g., MSSQL with server cursors) may honor skip at the server, others always fetch all rows locally.RecsSkip on a unidirectional cursor (CursorKind = ckForwardOnly) may raise an exception or silently ignore the skip.TFDTable does not support RecsSkip/RecsMax; only TFDQuery / TFDCommand with an explicit SELECT.RowsetSize > 0 combined with RecsMax can cause partial fetches that confuse bookmark navigation and RecordCount.Given these variances, what is the reliable pattern for paging a 100k+ row table across SQLite, PostgreSQL, SQL Server, and Firebird without incurring a full-table fetch? Does FireDAC expose any driver-agnostic hook to inject dialect-specific LIMIT/OFFSET at prepare time, or must each driver be handled with conditional SQL macros? How can the actual fetch behavior be verified for a specific driver version without relying on documentation claims?
37275 reputation · 03 Jul 2023, 21:07 UTC
Use hand-written dialect pagination SQL through TFDQuery, not FetchOptions.RecsSkip or RecsMax. FireDAC does not rewrite SelectCommand to inject LIMIT/OFFSET, FIRST/SKIP, or OFFSET/FETCH, and client-side truncation normally fetches every matching row before applying the limit. For a 100k+ row table across SQLite, PostgreSQL, SQL Server, and Firebird, build parameterized SQL per driver and prepare it with TFDQuery.
Likely explanation: RecsSkip and RecsMax operate on the local rowset after the driver materializes the result. Some drivers with server cursors (for example, MSSQL with CursorKind = ckDefault and server cursors enabled) may push the skip downstream, but that is driver- and cursor-dependent, not guaranteed.
Confirmed facts: FireDAC exposes no driver-agnostic prepare-time hook to inject dialect clauses. TFDTable does not support RecsSkip/RecsMax; only TFDQuery or TFDCommand with an explicit SELECT. RowsetSize > 0 combined with RecsMax can produce partial fetches that corrupt bookmark navigation and RecordCount.
FetchOptions.RecsSkip := 0, RecsMax := 0, and RowsetSize := -1.TFDQuery per page request with parameterized dialect SQL:-- PostgreSQL / SQLite SELECT * FROM big_table ORDER BY id LIMIT :max OFFSET :skip -- SQL Server 2012+ SELECT * FROM big_table ORDER BY id OFFSET :skip ROWS FETCH NEXT :max ROWS ONLY -- Firebird SELECT FIRST :max SKIP :skip * FROM big_table ORDER BY id
:skip and :max as integers through TFDQuery.Params, then Open.WHERE id > :last_id ORDER BY id LIMIT :max, because large OFFSET values degrade on PostgreSQL and SQL Server.Enable FireDAC tracing and inspect the command text sent to the server:
var
Tracer: TFDTracer;
begin
Tracer := TFDTracer.Create(nil);
try
Tracer.Options := [toCommands, toParams, toTime];
FDManager.Tracer := Tracer;
FDQuery1.Open;
// inspect trace log: confirm LIMIT/OFFSET appears and row count equals page size
finally
FDManager.Tracer := nil;
Tracer.Free;
end;
end;Cross-check with the database profiler: PostgreSQL EXPLAIN ANALYZE, SQL Server Extended Events/Profiler, Firebird trace, or SQLite .trace. If the trace shows the full table scan without a limit clause, RecsSkip is not being pushed down.
Which SQL Server version are you targeting? Pre-2012 instances do not support OFFSET/FETCH and require ROW_NUMBER() or TOP instead, which changes the SQL template.
Assumptions: FireDAC 11.x/12.x, TFDQuery with explicit SELECT, and integer parameters. Older FireDAC versions or driver-specific cursor modes may behave differently; mark those for review.
Use comments to ask for clarification. Post a solution as an answer.
7,460 reputation · 03 Jul 2023, 22:41 UTC
A key distinction is that RowsetSize > 0 controls fetch batching, not whether the server scans every matching row. With CursorKind = ckDefault, FireDAC typically materializes the full rowset client-side before applying RecsSkip and RecsMax, so RowsAffected can equal RecsMax even when the wire carried far more rows.
To verify actual behavior, enable TFDTracer and inspect the SQL sent to the server. If no dialect-specific OFFSET or LIMIT clause appears, pagination is client-side. FireDAC has no driver-agnostic prepare hook; inject the clause conditionally using TFDPhysConnection.RDBMSKind before Prepare, then re-check the trace.