I am trying to do pagination in Coldfusion, with MySql, to retrieve 20 records at a time. First, I get the total count of records of what I want to retrieve.
<CFQUERY DATASOURCE="#datasource#" USERNAME="#username#" PASSWORD="#password#" NAME="myCount" CACHEDWITHIN="#CreateTimeSpan(0,0,3,0)#">
SELECT COUNT(*) as TotalCount
FROM products
WHERE products.field1 = <cfqueryparam value = "#myfield#" cfsqltype = "cf_sql_integer" maxLength = "2">
AND MATCH(field2) AGAINST(<cfqueryparam value = "#TRIM(string)#" cfsqltype = "cf_sql_char" maxLength = "52"> IN BOOLEAN MODE)
</CFQUERY>
Then I use the following to retrieve 20 records at a time:
<CFQUERY DATASOURCE="#datasource#" USERNAME="#username#" PASSWORD="#password#" NAME="myQuery" result="tmpResult">
SELECT products.field1, products.field2, products.field3, company.field1, company.field2
FROM products JOIN company ON products.field1 = company.field1
WHERE products.field2 = <cfqueryparam value = "#myfield#" cfsqltype = "cf_sql_integer" maxLength = "2">
AND MATCH(field2) AGAINST(<cfqueryparam value = "#TRIM(string)#" cfsqltype = "cf_sql_char" maxLength = "52"> IN BOOLEAN MODE)
ORDER by products.field3 DESC
LIMIT <cfqueryparam value="#start#" cfsqltype="cf_sql_integer">, 20
</CFQUERY>
(Note: the value of #start# will increment by 20 for each page.)
Everything seems to work fine. When I clicked on the first page, the execution time (#tmpResult.ExecutionTime#
) indicated that it took 40 (I assume it is milliseconds) to retrieve 20 records. However, if I click on the last page, which could be record number 40,000, it takes 500 milliseconds to retrieve last 20 records. Going backward it seems that those later pages took a longer time to retrieve.
Why the discrepancy? Shouldn't retrieving the same amount of 20 records take the same amount of time whether is it from the "front" or the "back"? Am I missing something here? Thanks in advance.