Code run fast on a cronjob but slow with a do_action or ajax call

In cPanel run hosting environments you might have one PHP version used for your web applications but another one used for WP-CLI.

Checking the PHP version, used by WP-CLI is done by running wp --info. You can check your PHP version in WordPress by going to the Health Check status page. It should be warning you anyway, if you are using an older version than 7.4.

Another reason that might be slowing you down is the presense of hooks and filters, related to operations with an active user session or being done in wp-admin. When you run a WP-CLI command, you don’t have an active user session (is_user_logged_in() returns false) and operation is not considered as run in wp-admin (is_admin() returns false).

Yet another reason – plugins checking for the presense and value of WP_CLI constant could be excluding their operations from WP-CLI, thus burdening the process when you are doing the import through the web interface.

This is not “the ultimate” list of reasons, but definitely some that I would check before digging deeper into profiling the WP-CLI execution.