[SalesForce] tell how many records in a Database.QueryLocator()

According to the documentation, in a batch a Database.QueryLocator can retrieve up to 50 million records.

A maximum of 50 million records can be returned in the
Database.QueryLocator object. If more than 50 million records are
returned, the batch job is immediately terminated and marked as
Failed.

I then cycle through them in batches, default size 200, maximum size 2000. When I run a batch, Salesforce knows how many batches there are, so it must know how many records are in the QueryLocator. But can I find out? The only methods I can see on the QueryLocator are these two:

getQuery() Returns the query used to instantiate the Database.QueryLocator object. This is useful when testing the start method.

iterator() Returns a new instance of a query locator iterator.

I want to know what the total size of the record set is, before I go through all the batches.

Best Answer

Try running a SELECT COUNT() FROM ... query in your start method before calling QueryLocator.