function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
deepak_naikdeepak_naik 

Bulk API with PK-Chunking creates huge sets of batches when query is with where clause and subset of the records needs to be extracted

We have a Java Program which uses Bulk API (with PK Chunking)  to extract records from an Object. The "select query" has a where clause, which should extract a subset of records, to honour the where clause, but we see that huge numer of batches are created on the Salesforce Server side, and many a times, the BULK API limits are hit

For example :
Number of records in the Object Account : 20000 We enable the connection and set the Pk Chunking header as , with chunk size as 2000
 
bulkConnection.addHeader("Sforce-Enable-PKChunking","chunkSize=2000");

Select Query :
SELECT Id, Name FROM Account WHERE CreatedDate >= 2018-07-20T05:00:00.000Z"


As per the query to match the condition, the query should return only 10 records (at most 1 batch), but we see that there are 10 batches created on the Salesforce Server.
Why is there 10 batches that are created, when the number of records to be extracted is only 10 which can fit in 1 batch.
Is there any setting that can be applied, or code tuning that can be done so that only 1 batch is created on the Salesforce Server
Victor Cazacu 4Victor Cazacu 4
I just had the same issue. Expected to create 130 batches and created 13,000 which reached the daily limit. Anyone know what the issue could be?
deepak_naikdeepak_naik
Hi

Its the way how PK Chunking works.
PK chunking splits bulk queries on very large tables into chunks based on the record IDs, or primary keys, of the queried records. Each chunk is processed as a separate batch. Hence your query will be executed against each batch, irrespective whether it returns the records or not.
Solution would be not to use PK Chunking for queries that return a small set or a subset of records

Regds,
Deepak