This is a known issue that I've been working on. The wallet is a large module in Bitcoin Core and changing it takes quite a bit of time.

On 08/20/2022 10:16 AM, Felipe Micaroni Lalli via bitcoin-dev wrote:
8.1) Can we "optimize" a huge wallet without moving the funds to a new one? Like a "fsck" or eqv?
You can remove old transactions using the removeprunedfunds RPC. That should  greatly speed up balance calculations and transaction creation.

8.2) Can we improve the cache usage somehow? Putting the entire wallet in memory, for example?
It's already entirely in memory.