Query processor ran out of internal resources and could not produce a query plan” - MariaDB
OR does not optimize well; don't use that form.
One of these indexes will be optimal; add both:

INDEX(company_id, period_code, paydate)
INDEX(period_code, company_id, emp_id)

{d '2020-05-01'} can be simply '2020-05-01'
You might find this easier to deal with

    paydate &gt;= '2020-05-01'
AND paydate  &lt; '2020-05-01' + INTERVAL 1 MONTH

COUNT(loan_payment) checks for NULL; perhaps you want simply COUNT(*)?