As the command is doing too many queries to the DB and slowing the server down, we need to optimize how it scrolls through assets.
Scrolling only through the assets that have earnings in the searched period would lower the number of queries done.
For example: for January 2022, running this query
select COUNT(DISTINCT (asset_id)) c from tmp_asset_earnings where (earning_date between '2022-01-01' and '2022-01-31')
returned a number of 13184 assets.