Improving performance in cron jobs can be very time consuming. Even just figuring out where to start can take hours.
I came up with an idea that really helped me to locate potential performance killer just by checking the execution time by using TIMESTAMPDIFF. For example:
1 2 3 4 |
SELECT schedule_id, job_code, executed_at, finished_at, TIMESTAMPDIFF(SECOND, executed_at, finished_at) AS runtime FROM cron_schedule ORDER BY runtime desc LIMIT 10; |
Output example:
1 2 3 4 5 6 7 8 9 10 11 |
+-------------+------------------------------------+---------------------+---------------------+---------+ | schedule_id | job_code | executed_at | finished_at | runtime | +-------------+------------------------------------+---------------------+---------------------+---------+ | 25432321 | custom_customer_sync | 2022-09-13 18:50:07 | 2022-09-13 18:57:28 | 456 | | 25433099 | indexer_update_all_views | 2022-09-13 19:05:07 | 2022-09-13 19:05:13 | 6 | | 25432315 | indexer_update_all_views | 2022-09-13 18:35:07 | 2022-09-13 18:35:11 | 4 | | 25432645 | indexer_update_all_views | 2022-09-13 18:43:06 | 2022-09-13 18:43:09 | 3 | | 25433048 | indexer_update_all_views | 2022-09-13 18:59:06 | 2022-09-13 18:59:09 | 3 | ... +-------------+------------------------------------+---------------------+---------------------+---------+ 20 rows in set (0.01 sec) |
The runtime can be a sign of high memory or CPU usage, therefore, you should probably look at those cron jobs first.