During Apex Batch execution the Total Batches either increases or decreases as the Batch progresses with time.
Batch Class and its Execution:-
~A Batch Class has three methods from the Database. Batchable Interface that needs to be implemented, Start, Execute & Finish.
~The Start and Finish methods execute only once while the Execute methods run for No. Of Records / Batch Size times, creating an equal number of debug logs for each execute.
~Since the Batch’s Start method runs only once its queries all the criteria matching records and stores them in QueryLocator and passes records to Execute methods as per the Batch Size, so there is no possibility that if some records do not match or match the criteria after the Starts execution then its would either be included or excluded. Hence this issue does not have to do with the insert, deletion, or update of records to match the criteria.
~Run the batch job with batch size 1:
~Id batchJobId = Database.executeBatch(new ContactBatch(), 1);
After the batch job finishes check the last debug log, you will see that not all batches had been processed, internal callCount variable in the example class above has a value which is less than 1000: