I have a Batch Apex process that queries about 12,000 Contacts - sorted (descending) by a number field. My process ranks these records based on the sort order (record with the highest value in the sorted number field gets a rank of 1, record with the second highest value gets a rank of 2, etc etc). However when reviewing the results, there were incorrect rankings.
I've done a lot of logging/debugging to determine what is going wrong. It appears that the 12,000 Contacts aren't queried as a single group. Instead, I show that 10,000 (exactly) records are sorted correctly and processed in the correct order. And that the other 2,000 records are sorted correctly and processed in the correct order. BUT, if I put the two lists together, the list of 12,000 is not sorted correctly. Said another way, you would think that the final item in the list of 10,000 would be larger than the first item in the list of 2,000, but this is not the case. It's as if the query takes the first 10,000 Contacts it finds, sorts them, and provides them for processing, and then takes the other 2,000, sorts them, and provides them for processing.
Can you provide some insight here? Is it not valid to sort an entire recordset in a batch query? Or is there a way to do it?
Thanks Chris