Improve wp query perfomance on large db

The shop I’m looking after also has a lot of large queries and for us, the solution was a WP optimised hosting provider with litespeed caching and sufficient resources (RAM) to handle the query.

And for queries which are always the same, I also use transients. With transients, the query runs only once and is then stored until the transient expires. See https://developer.wordpress.org/apis/handbook/transients/. The first query will still take time; but subsequent visitors can use the stored query result.