Skip to content

Commit

Permalink
Speed grid paging by separating queries into count and data acquisiti…
Browse files Browse the repository at this point in the history
…on steps
  • Loading branch information
jefftrull committed Aug 11, 2012
1 parent 15610a6 commit 112fb23
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions classes/ExtJSStoreRemotingCtrl.cls
Expand Up @@ -96,6 +96,14 @@ public with sharing class ExtJSStoreRemotingCtrl {
// turn grid's page load/sort request into SOQL and return data
@RemoteAction
public static CRUDResponse query(QueryObj qobj) {
CRUDResponse resp = new CRUDResponse();

// construct a count query. Two SOQL statements - one COUNT(), one with LIMIT -
// seems to be faster than one query without LIMIT
String countstr = 'SELECT COUNT() FROM ' + qobj.sobjname;
countstr = String.escapeSingleQuotes(countstr);
resp.total = Database.countQuery(countstr); // ExtJS grid needs the true total here

// construct a query string
String qstr = 'SELECT ' + qobj.fields + ' FROM ' + qobj.sobjname;
if (qobj.sortParams != null) {
Expand All @@ -108,10 +116,10 @@ public with sharing class ExtJSStoreRemotingCtrl {
// SOQL only supports a single direction, so ignoring others. Probably should return error instead.
qstr += ' ' + qobj.sortParams[0].get('direction');
}
Integer reclimit = qobj.start + qobj.recordCount;
qstr += ' LIMIT ' + String.valueOf(reclimit);
qstr = String.escapeSingleQuotes(qstr); // sanitize for SOQL injection
// no LIMIT here or our "total" numbers will be wrong for the grid
// alternatively: do two SOQL calls, one to get the total, one to get requested data
CRUDResponse resp = new CRUDResponse();

Integer current_result = 0;
String[] fieldNames = qobj.fields.split(',');
try {
Expand All @@ -129,7 +137,6 @@ public with sharing class ExtJSStoreRemotingCtrl {
resp.errorMessage = e.getMessage();
return resp;
}
resp.total = current_result;
return resp;
}

Expand Down

0 comments on commit 112fb23

Please sign in to comment.