How to speed up Mailchimp campaign/recipient/activity import in PHP (currently ~4 hours) [closed]

I’m writing a PHP script that fetches all campaigns, recipients, and their activities from the Mailchimp API into my application.

Mailchimp’s API limits me to 1,000 records per request, so my script paginates and inserts everything into MySQL in chunks. Even with chunking and INSERT IGNORE bulk inserts, the full run still takes about 4 hours for large lists.

Example snippet of what I’m doing:

$offset = 0;
$count = 1000;
do {
    $email_activities = $MailChimp->get("reports/$campaign_id/email-activity", [
        'offset' => $offset,
        'count'  => $count,
    ]);

    // bulk insert recipients + activities
    // ...
    
    $offset += $count;
} while (count($email_activities['emails']) === $count);

This works but is extremely slow for campaigns with hundreds of thousands of recipients/activities.

Question:
What are effective ways to improve the performance of importing this data?

  • Are there Mailchimp features (like webhooks) I should use instead of repeatedly polling the API?

  • Are there PHP/MySQL optimizations (e.g., LOAD DATA INFILE, parallel jobs, queue/worker model) that can handle millions of rows faster?

Any best practices or architectural suggestions for large Mailchimp data imports would be appreciated.