Unlock the Power of Large Data Sets in Salesforce with PK Chunking – Boost Your Performance Today!

During my preparations for the Data Architect Salesforce certification, I learned about a new functionality introduced in Spring ’15 that facilitates the extraction of large data sets from Salesforce in a more efficient manner. This feature is known as PK Chunking and is of significant importance to developers.


Dealing with large data sets in Salesforce can be challenging for data architects. The traditional method of querying through Bulk API can take up a lot of time and may lead to errors. Fortunately, Salesforce introduced PK Chunking in Spring ’15, which is a simplified method that improves data management and processing speed. In this article, we will explain how PK Chunking works and its advantages for developers and data architects.
What is PK Chunking?

It stands for Primary Key Chunking and allows developers to break up large data sets into smaller chunks based on primary key values. By doing so, the Bulk API can process the data more efficiently and with fewer errors.

How Does PK Chunking Work?

Primary Key Chunking works by splitting large data sets into smaller chunks based on primary key values. The Bulk API processes each chunk individually and combines the results at the end. To use PK Chunking in your Bulk API queries, you need to specify the pkChunking parameter in your query job. Here’s an example implementation in Apex:

Database.Batchable<sObject>, Database.Stateful
public class MyBatch implements Database.Batchable<sObject>, Database.Stateful {
  public Database.QueryLocator start(Database.BatchableContext bc) {
    return Database.getQueryLocator([SELECT Id, Name FROM Account]);

  public void execute(Database.BatchableContext bc, List<sObject> scope) {
    List<Account> accounts = new List<Account>();
    for (sObject sobj : scope) {
    Database.SaveResult[] results = Database.update(accounts, false);

    if (results != null) {
      for (Database.SaveResult result : results) {
        if (!result.isSuccess()) {
          for (Database.Error error : result.getErrors()) {
            // handle the errors here

  public void finish(Database.BatchableContext bc) {
    // perform any post-processing here

  public Iterable<sObject> start(Database.BatchableContext bc) {
    // enable PK Chunking
    Database.QueryLocatorIterator iterator = (Database.QueryLocatorIterator)Database.getQueryLocator([SELECT Id, Name FROM Account]).iterator();
    return iterator;

In this example, we implement a batch class that uses PK Chunking to process a large data set of Account records. We enable PK Chunking by setting the chunk size to 1000 using the setPKChunking method. This allows the Batch class to process the data more efficiently and with fewer errors.

Note that this is just an example implementation, and the specific implementation may vary depending on your specific use case.
Benefits of PK Chunking

There are several benefits to using PK Chunking in your Bulk API queries, including:

  1. Faster processing of large data sets
  2. Reduced likelihood of errors
  3. More efficient resource utilization
  4. Simplified data management

Furthermore, by using PK Chunking, the Bulk API is able to process smaller data chunks in a more efficient manner, which results in faster processing times. This not only simplifies data management but also reduces the chances of errors. Moreover, PK Chunking helps in improving resource utilization. Therefore, it is an excellent solution for organizations that depend heavily on Salesforce data.


As a data architect or developer, dealing with large data sets in Salesforce can be challenging. Fortunately, Salesforce’s PK Chunking feature simplifies data management and improves processing speed. By breaking up large data sets into smaller chunks, the Bulk API can process the data more efficiently and with fewer errors. This feature is especially valuable for organizations that rely heavily on Salesforce data. If you’re a developer who regularly deals with large data sets, be sure to take advantage of this exciting new feature in Spring ’15.

Want To Learn More?

Learn about Use PK Chunking To Extract Large Data Sets From Salesforce or PK Chunking
Read more about Revamp Your Salesforce Testing with the New Assert Apex Class in Winter 23 Release!

Share this article...

Salesforce Mentor, with 10 years Salesforce experience, Hardcore Admin & Guru Developer, Geek, Animal lover, and dog & cat rescuer activist. Lifetime student, Stand-Up comedian wannabe, Photographer, Gamer, Anime lover, and coffee addict. Spreading Salesforce love and teaching beyond my capacity. Aiming to become Tech Architect!

One Ping

  1. Pingback: Permission Sets: Saying Goodbye to Profiles and Revolutionizing User Management! - SalesforceMonday

Leave a Reply

Your email address will not be published. Required fields are marked *