Skip to content

Commit

Permalink
perf(QueryBuilder): Performance improvement for large recordsets
Browse files Browse the repository at this point in the history
Particularly noticeable on ACF
Don't inspect query meta data if no rows are returned.
  • Loading branch information
aliaspooryorik authored Apr 19, 2024
1 parent 0a0826c commit a5f2521
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
26 changes: 14 additions & 12 deletions models/Query/QueryUtils.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,10 @@ component singleton displayname="QueryUtils" accessors="true" {
* @return array
*/
public array function queryToArrayOfStructs( required any q ) {
if ( arguments.q.recordCount == 0 ) {
return [];
}

try {
var queryColumns = getMetadata( arguments.q ).map( function( item ) {
return item.name;
Expand All @@ -332,18 +336,16 @@ component singleton displayname="QueryUtils" accessors="true" {
rethrow;
}

return queryReduce(
arguments.q,
function( results, row ) {
var rowData = structNew( "ordered" );
for ( var column in queryColumns ) {
rowData[ column ] = row[ column ];
}
results.append( rowData );
return results;
},
[]
);
var results = [];
arrayResize( results, arguments.q.recordCount );
for ( var row in arguments.q ) {
var rowData = structNew( "ordered" );
for ( var column in queryColumns ) {
rowData[ column ] = row[ column ];
}
results[ arguments.q.currentRow ] = rowData;
}
return results;
}

/**
Expand Down
19 changes: 19 additions & 0 deletions tests/specs/Query/Abstract/PaginationSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,25 @@ component extends="testbox.system.BaseSpec" {
expect( results.results ).toBe( expectedResults );
} );

it( "can handle empty datasets", function() {
var builder = getBuilder();
var expectedResults = [];
var expectedQuery = queryNew( "id", "integer", expectedResults );
builder.$( "count", 0 );
builder.$( "runQuery", expectedQuery );

var results = builder.from( "users" ).paginate( page = 1, maxRows = 0 );

expect( results.pagination ).toBe( {
"maxRows": 0,
"offset": 0,
"page": 1,
"totalPages": 0,
"totalRecords": 0
} );
expect( results.results ).toBe( expectedResults );
} );

it( "can provide a custom paginator shell", function() {
var builder = getBuilder();
builder.setPaginationCollector( {
Expand Down

0 comments on commit a5f2521

Please sign in to comment.